【问题标题】:Is there any automation script to uncheck "Require Password" to never after sleep and turn off sleep in mac?是否有任何自动化脚本可以在睡眠后取消选中“需要密码”并在 Mac 中关闭睡眠?
【发布时间】:2021-08-30 07:11:26
【问题描述】:

System Preference Settings

手动步骤:

  1. 打开系统偏好设置
  2. 点击“安全和隐私”
  3. 点击“常规”标签
  4. 取消选中“需要密码”选项
  5. 禁用“屏幕锁定”
  6. 关闭“系统偏好设置”

【问题讨论】:

    标签: macos applescript


    【解决方案1】:

    示例 AppleScript 代码,如下所示,在 ma​​cOS Catalina 下的 Script Editor 中进行了测试ma​​cOS Big Sur系统偏好设置 中的 语言和地区 设置设置为 English (US) - Primary strong> 并且为我工作没有问题1

    • 1 假设 系统偏好设置 > 安全和隐私 > 隐私中的必要和适当的设置已设置/根据需要解决。

    示例 AppleScript 代码

    --  # Check to see if System Preferences is 
    --  # running and if yes, then close it.
    --  # 
    --  # This is done so the script will not fail 
    --  # if it is running and a modal sheet is 
    --  # showing, hence the use of 'killall' 
    --  # as 'quit' fails when done so, if it is.
    --  #
    --  # This is also done to allow default behaviors
    --  # to be predictable from a clean occurrence.
    
    if running of application "System Preferences" then
        try
            tell application "System Preferences" to quit
        on error
            do shell script "killall 'System Preferences'"
        end try
        delay 0.1
    end if
    
    --  # Make sure System Preferences is not running before
    --  # opening it again. Otherwise there can be an issue
    --  # when trying to reopen it while it's actually closing.
    
    repeat while running of application "System Preferences" is true
        delay 0.1
    end repeat
    
    --  # Open System Preferences to the General 
    --  # tab of the Security and Privacy pane.
    
    tell application "System Preferences"
        activate
        reveal anchor "General" of ¬
            pane id "com.apple.preference.security"
    end tell
    
    tell application "System Events"
        tell application process "System Preferences"
            tell its window 1
                --  # Wait until the 'Require password' check box exists.
                set i to 0
                repeat until exists checkbox 1 of tab group 1
                    delay 0.1
                    set i to i + 1
                    if i ≥ 30 then return
                end repeat
                --  # If the target check box is unchecked then quit.
                if value of checkbox 1 of tab group 1 = 0 then
                    tell application "System Preferences" to quit
                    return
                end if
                --  # Else click the target check box.
                click checkbox 1 of tab group 1
                --  # Wait until the 'Enter the password' sheet exists.
                set i to 0
                repeat until exists sheet 1
                    delay 0.1
                    set i to i + 1
                    if i ≥ 30 then return
                end repeat
                --  # You can uncomment the 'set value of text field 1' and set the password 
                --  # and uncomment 'click button "OK"' to have the rest fully automated, or
                --  # leave them commented, enter your password, press enter and the rest is
                --  # automated. There is also a way to retrieve your password from your
                --  # Keychain, however, that's for you to research and implement.
                tell sheet 1
                    -- set value of text field 1 to "PASSWORD"
                    -- click button "OK"
                    set i to 0
                    repeat until exists button "Turn Off Screen Lock"
                        delay 0.5
                        set i to i + 1
                        if i ≥ 30 then return
                    end repeat
                    click button "Turn Off Screen Lock"
                end tell
            end tell
        end tell
    end tell
    
    tell application "System Preferences" to quit
    

    注意:示例 AppleScript 代码 就是这样,没有任何包含的错误处理不包含任何适当的额外错误处理。用户有责任根据需要或需要添加任何错误处理。查看AppleScript Language Guide 中的try statementerror statement。另请参阅Working with Errors。此外,在适当的情况下,可能需要在事件之间使用delay 命令,例如delay 0.5延迟设置得当。

    【讨论】:

    猜你喜欢
    • 2013-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-16
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    • 2019-03-02
    相关资源
    最近更新 更多