2017-02-03 57 views
0

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

是否有任何解决方法,因此我仍然可以在所有桌面上获得打开的窗口列表,而无需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一起破解这个。这可能不是我想要做的最有效的方法。

回答

0

貌似我得到这个与这种丑恶bash工作 - applescript破解

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 

其中PID 818属于Finder过程等。