如何确保 OS X 10.10 (Yosemite) 中的 Applescript 对话框焦点? [英] How to ensure Applescript dialog focus in OS X 10.10 (Yosemite)?

查看:29
本文介绍了如何确保 OS X 10.10 (Yosemite) 中的 Applescript 对话框焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 OS X 10.10 (Yosemite) 之前,我可以通过告诉当前应用程序"激活来确保 Applescript 对话框具有焦点:

Before OS X 10.10 (Yosemite), I could ensure an applescript dialog had focus by telling the "current application" to activate:

tell current application
        activate
        set output to (do shell script "printf '" & hostsLine & commentString & "' >> /private/etc/hosts" with administrator privileges)
end tell

这会将密码对话框带到最前面以供输入.该对话框不再以焦点显示在 Yosemite 中,您必须先点击它才能输入.

This would bring the password dialog to the front to type in. The dialog no longer appears with focus in Yosemite and you have to click on it before you can type.

在堆栈交换和其他地方搜索没有提供任何解决方案/变通方法.

Searching on stack exchange and elsewhere hasn't provided any solutions/workarounds.

有人有在 OS X 10.10 中工作的方法吗?

Does anyone have a way that works in OS X 10.10?

更新 1:如果其他人遇到此问题,这似乎是一个新的沙盒问题.非密码对话框正确获得焦点.实际上,具有管理员权限"密码对话框之前的非密码对话框会导致密码对话框也有焦点.我添加了一个对话框来确认 Safari 在测试时仍然是前端应用程序并发现了这一点.作为一种解决方法,我添加了一个超时为 1(秒)的前面的对话框,直到找到更好的解决方案.

UPDATE 1: In case anyone else runs into this problem, it appears to be a new sandboxing issue. A non-password dialog box gets focus properly. In fact, a non-password dialog before the "with administrator privileges" password dialog will cause the password dialog to also have focus. I added a dialog to confirm Safari was still the front app while testing and discovered this. As a workaround, I have added a preceding dialog with timeout of 1 (second) until a better solution can be found.

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 的 Authenticated Save 帮助程序脚本(适用于应用程序的 MAS 版本)使用具有管理员权限"密码对话框.但它在优胜美地得到了正确的聚焦.原生应用调用 applescript 与应用的 Applescript tell 命令之间有什么不同?

Interestingly, BBedit's Authenticated Save helper script (for the MAS version of the app) uses a "with administrator privileges" password dialog. But it gets focus properly in Yosemite. Something different between a native app call to applescript vs. an Applescript tell command for an app to do so?

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 中密码问题的焦点很烦人.

UPDATE 2: If anyone is curious, this part of a Safari helper script for my wife to block spammy popup ad sites like mackeeper.zeobit.com that have modal dialog boxes. Editing /etc/hosts was a little too complicated/daunting and she wanted a turnkey solution. I can post the whole script if there's interest. It works fine under 10.9, but the focus for password issue is annoying in 10.10.

推荐答案

似乎是 10.10.x 中的一个错误,很可能是由于沙盒.已在 10.11 (El Cap) 中修复,因为未修改的脚本再次从 activate 命令获得焦点,就像在 10.9 和更早版本中一样.

Appears to be a bug in 10.10.x most likely due to sandboxing. Fixed in 10.11 (El Cap) as the unmodified script gets focus from the activate command once again like it did in 10.9 and earlier versions.

这篇关于如何确保 OS X 10.10 (Yosemite) 中的 Applescript 对话框焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆