get all process IDs of visible or "windowed" Apps

L3nny5
New Contributor III

Hey,

I'm looking for a better way to get all process IDs of all opened windows from a user then using this Applescript:

 

osascript -e 'tell application "System Events" to get unix id of every application process whose visible is true and name is not "Finder"'

 

I don't want to use AppleScript or not using System Events, because it needs to be "allowed" by the user or you need to create a PPPC profile, which i want to avoid.

Any idea?

1 ACCEPTED SOLUTION

L3nny5
New Contributor III

After hours of not getting a correct PPPC profile and still getting the **bleep** pop up, I found another solution for this problem:

@franton together with @talkingmoose found a nice way to not use osascript to get (almost) the same result. 

Almost, as in they are not using the pids but the process names. Which i guess doesn't make a big difference at the end.

Check the script here: https://github.com/franton/Mac-Patcher-and-Upgrader/blob/e78f94a8610a4e588ee5671b7383cb9dfeac7903/Ja... 

 

################################
#
# Close all running applications
#
################################

	# Find all applications with osascript and process them into an array.
	# Big thanks to William 'talkingmoose' Smith for this way of parsing lsappinfo
	runningapps=($( /usr/bin/lsappinfo list | /usr/bin/grep -B 4 Foreground | /usr/bin/awk -F '\\) "|" ASN' 'NF > 1 { print $2 }' ))

	# Process the new array of apps and gently kill them off one by one.
	# Obviously we don't want to kill a few apps we don't routinely update.
	for app ($runningapps)
	do
		[[ "$app" =~ ^(Finder|Progress|Google Chrome|Safari|Self Service|Terminal)$ ]] && continue
		/usr/bin/pkill "$app"
	done
fi

View solution in original post

5 REPLIES 5

mm2270
Legendary Contributor II

I'm not sure if there's a better way to do this other than the osascript method. But I'm posting here in case anyone figures something out. I could use it myself at times.

jhuls
Contributor III

I might be interested in this as well. Have you tried to do a PPPC profile for this? I seem to have mixed results with them working as expected.

L3nny5
New Contributor III

After hours of not getting a correct PPPC profile and still getting the **bleep** pop up, I found another solution for this problem:

@franton together with @talkingmoose found a nice way to not use osascript to get (almost) the same result. 

Almost, as in they are not using the pids but the process names. Which i guess doesn't make a big difference at the end.

Check the script here: https://github.com/franton/Mac-Patcher-and-Upgrader/blob/e78f94a8610a4e588ee5671b7383cb9dfeac7903/Ja... 

 

################################
#
# Close all running applications
#
################################

	# Find all applications with osascript and process them into an array.
	# Big thanks to William 'talkingmoose' Smith for this way of parsing lsappinfo
	runningapps=($( /usr/bin/lsappinfo list | /usr/bin/grep -B 4 Foreground | /usr/bin/awk -F '\\) "|" ASN' 'NF > 1 { print $2 }' ))

	# Process the new array of apps and gently kill them off one by one.
	# Obviously we don't want to kill a few apps we don't routinely update.
	for app ($runningapps)
	do
		[[ "$app" =~ ^(Finder|Progress|Google Chrome|Safari|Self Service|Terminal)$ ]] && continue
		/usr/bin/pkill "$app"
	done
fi

View solution in original post

franton
Valued Contributor II

Hi,

Yeah this one is all @talkingmoose  😉

L3nny5
New Contributor III

@franton 

just noticed that in your comment your in front of the code you're still talking about osascript, which I guess you were using before. Good thing for me, that was the keyword how I found it 😄 So sometimes forgetting something can be a benefit ‌😄