在 OS X 10.10 (Yosemite) 之前,我可以通过告诉“当前应用程序”激活来确保 applescript 对话框获得焦点:
tell current application
activate
set output to (do shell script "printf '" & hostsLine & commentString & "' >> /private/etc/hosts" with administrator privileges)
end tell
这会将密码对话框置于前面进行输入。在 Yosemite 中,该对话框不再带有焦点,您必须先单击它才能输入。
在堆栈交换和其他地方搜索尚未提供任何解决方案/解决方法。
有谁有在 OS X 10.10 中工作的方法吗?
更新 1:如果其他人遇到此问题,这似乎是一个新的沙箱问题。非密码对话框正确获得焦点。事实上,“具有管理员权限”密码对话框之前的非密码对话框将导致密码对话框也具有焦点。我在测试时添加了一个对话框来确认 Safari 仍然是最前面的应用程序,并发现了这一点。作为解决方法,我添加了一个超时为 1(秒)的前置对话框,直到找到更好的解决方案。
tell me -- alternate method: tell current application
activate
display dialog "Trying to get focus…" with title "Can't focus in Yosemite" buttons {"Cancel", "Idle"} cancel button "Cancel" giving up after (1)
set output to (do shell script "printf '" & hostsLine & commentString & "' >> /etc/hosts" with administrator privileges)
end tell
有趣的是,BBedit 的经过身份验证的保存帮助程序脚本(适用于应用程序的 MAS 版本)使用“具有管理员权限”的密码对话框。但它在优胜美地得到了正确的聚焦。本机应用程序对 applescript 的调用与应用程序执行此操作的 Applescripttell 命令之间有什么不同吗?
on documentShouldFinalizeAuthenticatedSave(theDocument, tempFilePath, destinationPath)
-- on input: tempFilePath points to the contents of the document written to a temp file, ready to move to the destination; destinationPath is where the file should be copied.
-- on exit: if the operation succeeded, delete the temp file (or else the application will assume the operation failed) and return YES for success
-- this is pretty straightforward: "cp tmpFilePath destinationPath"
do shell script "cp" & " " & quoted form of tempFilePath & " " & quoted form of destinationPath with administrator privileges
-- now remove the temp file, this indicates to the application that we did the work
do shell script "rm" & " " & quoted form of tempFilePath
return true
end documentShouldFinalizeAuthenticatedSave
更新 2:如果有人好奇,我妻子的 Safari 帮助程序脚本的这一部分可以阻止垃圾邮件弹出广告网站,例如具有模式对话框的 mackeeper.zeobit.com。编辑 /etc/hosts 有点太复杂/令人畏惧,她想要一个交钥匙解决方案。如果有兴趣的话我可以发布整个脚本。在 10.9 下工作正常,但在 10.10 中密码问题的焦点很烦人。