1

I tried to use the following AppleScript to get the PIDs of all the windows (including ones that are minimized). This script doesn't get the PIDs of windows on other desktops.

Is there any workaround for this so I can still get a list of opened windows across all desktops without having to activate individual processes and checking if they have windows?

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, I'm not too proficient with AppleScript. I've managed to hack this together using StackOverflow. This might not be the most efficient way to do what I'm trying to do.

Alok Mysore
  • 606
  • 3
  • 16

1 Answers1

1

Looks like I got this to work with this ugly 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

This prints something like

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

Where PID 818 belongs to the Finder process and so on.

Alok Mysore
  • 606
  • 3
  • 16