首页 > 解决方案 > OSX applescript - 检查焦点应用程序是否不是finder,否则不执行命令

问题描述

我对applescript有点生疏了;试图找出当前焦点应用程序是否是finder,如果是,程序将退出。否则,它应该执行一个命令。

问题是我找不到检查当前焦点应用程序是否是finder的方法。是否在线搜索并且有示例显示如何使应用程序成为焦点,但我只想检索具有焦点的应用程序的名称;并检查脚本是否应该退出或继续。像这样的东西;虽然我没有命令检查焦点应用程序。

tell application "System Events"
    # check if finder is the process on focus
    if "Finder" is in focus then
        display dialog ("Finder is in front")
    else
        display dialog ("Finder is not in front")
    end if
end tell

标签: applescript

解决方案


每个应用程序,不管它是否有一个 AppleScript 字典,都有一个frontmost属性。

所以你可以简单地写(System Events不需要)

if frontmost of application "Finder" then
    display dialog ("Finder is in front")
else
    display dialog ("Finder is not in front")
end if

推荐阅读