macos - AppleScript 以最前面的应用程序为目标

标签 macos applescript

我想对鼠标按钮进行编程以显示/隐藏 Finder。我编写了以下 AppleScript 并将其绑定(bind)到我的鼠标按钮:

tell application "System Events"
    --When this script is run,
    --  the frontmost application will be this script itself
    --Get the name of this script as it is running and hide it,
    --  so that the previous frontmost application is in front again
    set theName to name of the first process whose frontmost is true
    set visible of process theName to false

    set theName to name of the first process whose frontmost is true
end tell

if theName is "Finder" then

    tell application "System Events"
        set visible of process "Finder" to false
    end tell


else

    tell application "Finder"
        activate
    end tell

end if

这有效,但速度相当慢。运行大约需要 2 秒。
我希望它更快。第一个 tell block 使用系统事件来获取脚本的名称并将其隐藏。是否有更简单/更快的方法来获取最前面的应用程序的名称之前 脚本开始? (即激活脚本时处于事件状态的应用程序)

最佳答案

运行时间慢的原因是我将 AppleScript 保存为应用程序。这使得应用程序只有 PPC,因此它必须在 Rosetta 下运行。如果您选择 Application Bundle,它将成为一个通用应用程序。

关于macos - AppleScript 以最前面的应用程序为目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1146472/

相关文章:

linux - 如何在 Linux 上控制光标?

python - 从 python 和 launchd 启动 osascript

swift - 如何获取 Safari 中每个窗口的每个选项卡中所有打开的 URL?

dictionary - 如何使用 AppleScript 在 Mac OS X 词典中查找单词?

applescript - 如何使用 AppleScript 访问大纲控件特定行的文本?

java - Eclipse 首选项窗口左侧 Pane 中的标题在 Mac OS X 中不显示

css - 替换视网膜就绪设备的背景图像

c++ - 如何修复 C++ 中的回溯行号错误

c - 别名、变量和文件名

applescript - 为什么Applescript 在后台Shell 作业后不退出?