OSX applescript-检查焦点中的应用程序是否不是finder,否则不要执行命令

OSX applescript-检查焦点中的应用程序是否不是finder,否则不要执行命令,applescript,Applescript,我对applescript有些生疏;试图找出当前焦点中的应用程序是否为finder,如果是,程序将退出。否则,它应该执行一个命令 问题是我找不到一种方法来检查focus中的当前应用程序是否为finder。我在网上搜索过,有一些例子展示了如何使应用程序聚焦,但我只想检索有聚焦的应用程序的名称;然后检查脚本是应该退出还是继续。像这样的东西;虽然我没有命令检查焦点中的应用程序 tell application "System Events" # check if finder is the p

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

问题是我找不到一种方法来检查focus中的当前应用程序是否为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

这里有一种方法可以检查Finder是否位于最前面:

示例AppleScript代码:



注意:示例AppleScript代码仅此而已,不包含任何适当的错误处理。用户有责任根据需要添加任何适当的错误处理。请查看中的语句和语句。另请参见。

这里有一种方法可以检查并查看查找器是否位于最前面:

示例AppleScript代码:



注意:示例AppleScript代码仅此而已,不包含任何适当的错误处理。用户有责任根据需要添加任何适当的错误处理。请查看中的语句和语句。另请参见。

这是您想要的语法:

tell application "System Events"
    # check if finder is the process on focus
    if frontmost of process "Finder" is true then
        display dialog ("Finder is in front")
    else
        display dialog ("Finder is not in front")
    end if
end tell
如果Finder没有运行,这将抛出一个错误,我相信您知道。这似乎是不太可能的情况,但如果是您担心的问题,您可以改变代码:

tell application "System Events"
    # check if finder is the process on focus
    set frontProcessName to name of first process whose frontmost is true
    if frontProcessName is "Finder" then
        display dialog ("Finder is in front")
    else
        display dialog ("Finder is not in front")
    end if
end tell

这是您想要的语法:

tell application "System Events"
    # check if finder is the process on focus
    if frontmost of process "Finder" is true then
        display dialog ("Finder is in front")
    else
        display dialog ("Finder is not in front")
    end if
end tell
如果Finder没有运行,这将抛出一个错误,我相信您知道。这似乎是不太可能的情况,但如果是您担心的问题,您可以改变代码:

tell application "System Events"
    # check if finder is the process on focus
    set frontProcessName to name of first process whose frontmost is true
    if frontProcessName is "Finder" then
        display dialog ("Finder is in front")
    else
        display dialog ("Finder is not in front")
    end if
end tell

无论是否有AppleScript字典,每个应用程序都有一个
frontmost
属性

因此,您可以简单地编写(
系统事件
不需要)


无论是否有AppleScript字典,每个应用程序都有一个
frontmost
属性

因此,您可以简单地编写(
系统事件
不需要)


为什么要调用系统事件来做这件事?我想是习惯吧。为什么要调用系统事件来做这件事?我想是习惯吧。有人知道OSX自动化的Javascript中的等价物是什么吗?有人知道OSX自动化的Javascript中的等价物是什么吗?