首页 文章

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

提问于
浏览
8

我想编写鼠标按钮来显示/隐藏Finder . 我编写了以下AppleScript并将其绑定到我的鼠标按钮:

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块使用System Events获取脚本的名称并将其隐藏 . 有没有更容易/更快的方法来获取最前面的应用程序 before 脚本启动的名称? (即激活脚本时处于活动状态的应用程序)

1 回答

  • 5

    运行缓慢的原因是我将AppleScript保存为应用程序 . 这使应用程序仅限PPC,因此必须在Rosetta下运行 . 如果选择Application Bundle,它将构成一个通用应用程序 .

相关问题