通过 ID 将应用程序带到前面

Bring application to front by ID

我想使用 AppleScript 将应用程序置于最前面。如果我运行下面的脚本

tell application "System Events"
  tell process id 916
    activate
  end tell
end tell

进程没有出现。相反,只有当前最前面的应用程序的活动 window 失去焦点,但该应用程序保持在前面。

甚至可以使用进程 ID 而不是应用程序名称来执行此操作吗?我已经在 Mac OS X 10.6.8 和 10.7.5 上试过了。

我正在寻找一个简单的 AppleScript 解决方案。它不应使用任何 shell 命令或任何其他解决方案。我想使用进程 ID 号,因为我可能有 运行 同一应用程序的多个实例(在同一文件位置)。

set processID to 432--currently firefox for me
tell application "System Events" to set a to file of 1st item of (processes whose unix id = processID)
activate application (a as alias as string)

这使用了应用程序文件的路径,这显然是必需的(不仅仅是名称)。 我有另一个使用 do shell script 的答案;如果你愿意,我可以补充。

我找到了以下解决方案:

tell application "System Events"
    set myProcesses to every process whose unix id is myPocessID
    repeat with myProcess in myProcesses
        set the frontmost of myProcess to true
    end repeat
end tell

Foo 的回答也有效:

tell application "System Events"
    set frontmost of every process whose unix id is myProcessID to true
end tell