List the running apps with scripting terminology

foundation
asobjc

(Jonas Whale) #1

On my Mac, the following script returns 80 apps including “root services”.
Is there a simple way to restrict the list to apps that owns a scripting dictionary?

use framework "Foundation"
use framework "AppKit"

set theWorkspace to current application's NSWorkspace's sharedWorkspace()
set appList to theWorkspace's runningApplications()

set scriptableList to current application's NSMutableArray's array()
repeat with anApp in appList
	try
		set {theResult, theError} to ((anApp's bundleURL())'s getResourceValue:(reference) forKey:(current application's NSURLApplicationIsScriptableKey) |error|:(missing value))
		if theResult as boolean then (scriptableList's addObject:(anApp's localizedName()))
	end try
end repeat
scriptableList as list


(Shane Stanley) #2

You’re actually storing the scriptability flag in the variable theError. Try this:

use framework "Foundation"
use framework "AppKit"

set theWorkspace to current application's NSWorkspace's sharedWorkspace()
set appList to theWorkspace's runningApplications()

set scriptableList to current application's NSMutableArray's array()
repeat with anApp in appList
	try
		set {theResult, isScriptable, theError} to ((anApp's bundleURL())'s getResourceValue:(reference) forKey:(current application's NSURLApplicationIsScriptableKey) |error|:(reference))
		if isScriptable as boolean then (scriptableList's addObject:(anApp's localizedName()))
	end try
end repeat
scriptableList as list


(Nigel Garvey) #3

Another solution might be:

tell application "System Events" to set scriptableList to name of application processes whose has scripting terminology is true

(Jonas Whale) #4

Sometimes I’m blind…

:wink:


(Jonas Whale) #5

Yep. That’s what I was using still now.

:wink:


(Shane Stanley) #6

They’re subtly different, at least here, in that System Events seems to be returning the names of the applications, whereas the ASObjC code is returning the process names as they appear in Activity Monitor.

That looks like a bug in System Events to me, but it might actually be more useful.