AppleScript:从应用程序隐藏/获取进程名称

时间:2011-05-06 15:40:29

标签: process applescript hide pid

我想隐藏最前面的应用。 我知道你可以使用语法隐藏进程:

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

我知道如何获得最前面的应用程序:

(path to frontmost application as string)

但是如何将两个命令连接在一起?

这不起作用:

tell application "System Events"
    set visible of process (path to frontmost application as string) to false
end tell

2 个答案:

答案 0 :(得分:5)

试试这个。

tell application "System Events"
    set frontProcess to first process whose frontmost is true
    set visible of frontProcess to false
end tell

你必须要小心,因为在某些方面,当你运行脚本时,脚本是最前面的,所以你可能最终隐藏脚本而不是你所针对的应用程序。我检查最前面进程的名称,如果它与脚本或“applescript runner”的名称匹配,那么你需要隐藏它,然后再次运行命令,你将实际得到你所针对的应用程序。这很棘手。

答案 1 :(得分:1)

由于大多数应用程序会在你按Cmd-H时隐藏,你也可能会走这条路。

tell app "System Events" to keystroke "h" using command down
相关问题