Jump to content
alfredclough

[SOLVED] How to load an Application's Icon with Applescript

Recommended Posts

I have a workflow where I want to provide a list of applications so that an application can be selected and the workflow continue. I want a list of the current running applications so I'm using a Script Filter. My question is how can I load the icon of the app to show it in the list rather than the icon of the workflow? I can't seem to find out how to programmatically load it. 

 

Here is the AppleScript I'm using with the Script Filter:

 

on run argv
	tell application "System Events"
		set runningProcesses to (name of every process whose background only is false)
	end tell
	set alfredJSON to "{\"items\": ["
	repeat with appName in runningProcesses
		--set appName to name of runningProcess
		set alfredJSON to alfredJSON & "{\"uid\": \"" & appName & "\",
		\"type\": \"default\", \"title\": \"" & appName & "\",\"subtitle\": \"\", \"arg\": \"" & appName & "\",\"autocomplete\": \"insert passage\"},"
	
	end repeat
	set alfredJSON to alfredJSON & "]}"
	return alfredJSON
end run

 

Edited by vitor
Marked as solved

Share this post


Link to post

Instead of trying to get the icon with AppleScript, leverage what Alfred already gives you for free. In the JSON, set icon: { "type": "fileicon", "path": "{{path_to_app}" }.

 

But in the future, when asking help with a Workflow please upload it somewhere. See the Reporting Problems with Workflows topic, as it gives a nice overview on how to build an effective report.

Share this post


Link to post
18 minutes ago, vitor said:

Instead of trying to get the icon with AppleScript, leverage what Alfred already gives you for free. In the JSON, set icon: { "type": "fileicon", "path": "{{path_to_app}" }.

 

I tried that and I can build the JSON when I run the AppleScript in Apple's script editor, but when I tried to run it using Alfred, I get the message: execution error: System Events got an error: Can’t get path of file of application process "Safari"

 

Here's the AppleScript I'm running:

on run argv

tell application "System Events"
	set runningProcesses to (every process whose background only is false)
end tell
set alfredJSON to "{\"items\": ["
repeat with runningProcess in runningProcesses
	set appName to name of runningProcess
	set appPath to path of file of runningProcess
	set alfredJSON to alfredJSON & "{\"uid\": \"" & appName & "\",
		\"type\": \"default\", \"title\": \"" & appName & "\",\"subtitle\": \"\", \"arg\": \"" & appName & "\",\"autocomplete\": \"insert passage\", \"icon\": { \"type\": \"fileicon\", \"path\":\"" & appPath & "\" }},"
end repeat

set alfredJSON to alfredJSON & "]}"
return alfredJSON

end run

Here's a link to the workflow. I'm using Alfred 3.6 and Mac OS 10.13.3

Share this post


Link to post

You don’t need the on run. You’ll also need to make sure you’re using POSIX paths. Try this:

tell application "System Events"
	set runningProcesses to (every process whose background only is false)
end tell
set alfredJSON to "{\"items\": ["
repeat with runningProcess in runningProcesses
	set appName to name of runningProcess
	set appPath to POSIX path of (file of runningProcess as alias)
	set alfredJSON to alfredJSON & "{\"uid\": \"" & appName & "\",
		\"type\": \"default\", \"title\": \"" & appName & "\",\"subtitle\": \"\", \"arg\": \"" & appName & "\",\"autocomplete\": \"insert passage\", \"icon\": { \"type\": \"fileicon\", \"path\":\"" & appPath & "\" }},"
end repeat

set alfredJSON to alfredJSON & "]}"
return alfredJSON

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×