如何通过AppleScript自动输入Mac密码,实现SelfControl番茄工作流自动化?
解决AppleScript自动输入SelfControl密码的问题
你的脚本核心问题在于未精准定位密码输入框和缺少必要的系统权限,以下是修正后的方案:
关键问题分析
- 辅助功能权限缺失:System Events需要获得「辅助功能」授权才能控制其他应用,否则无法发送按键操作。
- 输入框定位不准确:原脚本直接给SelfControl进程发送按键,可能密码输入框尚未加载或未获得焦点,导致输入无效。
- 固定延迟不可靠:
delay 15的固定等待时间无法适配不同电脑的加载速度,容易出现时机偏差。
修正后的完整代码
tell application "WebPomodoro" activate end tell set input to text returned of (display dialog "Ingresa un número para el Pomodoro:" default answer "" with icon note buttons {"Continue"} default button "Continue") try set inputNumber to (input as integer) on error display dialog "El texto ingresado no es un número válido." error number -128 -- 错误后退出脚本 end try set total_time to 0 repeat with interval from 1 to inputNumber set total_time to total_time + 25 if interval mod 4 = 0 then set total_time to total_time + 25 else set total_time to total_time + 5 end if end repeat set hours to (total_time div 60) set minutes to (total_time mod 60) display dialog (hours as string) & " horas y " & (minutes as string) & " minutos" -- 启动SelfControl并等待窗口加载 tell application "SelfControl" activate end tell tell application "System Events" -- 等待SelfControl主窗口出现 repeat until exists window 1 of process "SelfControl" delay 0.5 end repeat -- 等待密码输入框加载完成 repeat until exists text field 1 of window 1 of process "SelfControl" delay 0.5 end repeat -- 聚焦密码输入框 set focused of text field 1 of window 1 of process "SelfControl" to true -- 输入密码并确认 keystroke "你的Mac密码" keystroke return end tell
必要设置步骤
授权辅助功能权限:
打开「系统设置」→「隐私与安全性」→「辅助功能」,找到你运行该脚本的应用(比如「脚本编辑器」或「终端」),勾选允许其控制你的电脑。如果修改后仍无法运行,重启应用再试。安全优化(可选但推荐):
不要在脚本中硬编码密码,建议通过钥匙串存储并读取:-- 从钥匙串读取密码的代码,替换原硬编码行 set myPassword to do shell script "security find-generic-password -a " & quoted form of (do shell script "whoami") & " -s 'SelfControlAuth' -w"需先打开「钥匙串访问」,添加一个通用密码:
- 账户名:当前用户名(可通过
whoami命令查看) - 名称:设为
SelfControlAuth - 密码:你的Mac登录密码
- 账户名:当前用户名(可通过
内容的提问来源于stack exchange,提问作者Richard
相关产品推荐
相关产品推荐

