通过 ID 提交申请

问题描述 投票:0回答:3

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

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

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

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

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

applescript
3个回答
2
投票

我找到了以下解决方案:

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

1
投票
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
;如果你愿意的话我可以添加。


0
投票

正确答案位于https://stackoverflow.com/a/22951788/3008405 例如,

tell application "System Events"
  set PID to unix id of first process whose frontmost is true
  activate
  log choose from list {"Foo", "Bar"} with prompt "Prompt" with title "Title"
  set frontmost of the first process whose unix id is PID to true
  end tell

最初的

set
保存PID,最后的
set
恢复到最前面。

当同一应用程序有多个实例时,非 PID 解决方案会失败。

© www.soinside.com 2019 - 2024. All rights reserved.