【问题标题】:Get PIDs of all open windows on MacOS获取 MacOS 上所有打开窗口的 PID
【发布时间】:2017-06-20 11:09:28
【问题描述】:

我尝试使用以下AppleScript 来获取所有窗口(包括最小化的窗口)的PIDs。此脚本无法获取其他桌面上窗口的PIDs。

是否有任何解决方法,这样我仍然可以获得所有桌面上打开的窗口的列表,而无需 activate 单个进程并检查它们是否有窗口?

tell application "System Events"
    repeat with proc in (every process)
        if exists(first window of proc) then
            set pid to unix id of proc
            log pid
        end if
    end repeat
end tell

PS,我对 AppleScript 不太精通。我已经设法使用 StackOverflow 将其破解。这可能不是我想做的最有效的方法。

【问题讨论】:

    标签: macos applescript macos-sierra osascript


    【解决方案1】:

    看起来我可以使用这个丑陋的bash-applescript hack

    osascript -e "tell application \"System Events\" 
        repeat with proc in (processes where background only is false)
            set pname to name of proc
            log pname
        end repeat
    end tell" 2>&1 |
    while read line
    do
        echo "process " $line
        pgrep $line
    done
    

    这会打印出类似的东西

    process  Finder
    818
    process  Google Chrome
    3730
    3734
    3740
    5838
    process  iTerm2
    3750
    4210
    process  Sublime Text
    3822
    

    其中PID818属于Finder进程等等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      • 2021-07-24
      • 2014-08-27
      • 1970-01-01
      • 2013-08-29
      相关资源
      最近更新 更多