Applescript 锁定屏幕

Applescript 锁定屏幕

我需要一点 Applescript 来锁定屏幕,当我单击菜单栏中的钥匙串图标并选择“锁定屏幕”时得到的行为相同。

我找到了一种激活屏幕保护程序的方法,但这并不是我真正需要的。

答案1

您是否意识到在“安全首选项”窗格中您可以要求输入密码才能从屏幕保护程序唤醒,但默认情况下不需要身份验证?

然后您可以使用自己的方法或以下 AppleScript 激活屏幕保护程序:

do shell script "open /System/Library/Frameworks/ScreenSaver.framework/Versions/A/Resources/ScreenSaverEngine.app"

您还可以添加钥匙串锁并使用我找到的这个 AppleScript这里

activate application "SystemUIServer"
tell application "System Events"
    tell process "SystemUIServer"
        repeat with i from 1 to number of menu bar items of menu bar 1
            tell menu bar item i of menu bar 1
                click
                try
                    if name of menu item 1 of front menu is "Lock Screen" then
                        click menu item "Lock Screen" of front menu
                        exit repeat
                    end if
                end try
            end tell
        end repeat
    end tell
end tell

此脚本需要选中通用访问-->启用辅助设备访问。

答案2

别担心,只需使用tell application "Finder" to sleep

答案3

这是一个解决方案来自 Mac OS X Hints,但似乎需要先安装 JackSMS,不管它是什么。然后你可以做

tell application "JackSMS" to set lock screen to true
tell application "JackSMS" to set lock screen to false

答案4

这是我的一个代码。它使用系统事件发送锁定屏幕命令的默认按键组合,适用于所有版本的 OSX。甚至 Mojave!虽然它会提示为应用程序提供额外的安全权限,但在较新的 OSX 版本中

tell application "System Events" to keystroke "q" using {control down, command down}

相关内容