Is there a way to fetch the icon file path of an application using AppleScript?
-
06-02-2021 - |
Question
I'm currently writing an Alfred workflow in which I want to retrieve the icon file path linked to an application. Is there any way to do so natively using Apple Script ?
tell application "Finder"
set appPath to POSIX path of (path to application "Terminal")
set resourcesPath to appPath & "Contents/Resources"
set contentFiles to (list folder resourcesPath)
end tell
Doing that I managed to get a list of file names but now I don't really know how to filter the .icns
file.
Solution
I'm not familiar with Alfred however as far as AppleScript and getting an Application Bundle's Icon's pathname is typically not hardcoded, so it's not like you can get its path with a single command. Additionally the value of CFBundleIconFile
can be with and without the .icns
extension, so when building the fully qualified pathname of the Application Bundle's Icon you'll need to take that into consideration as you write your code.
As an example, using the built-in /Applications/Calculator.app
the /Applications/Calculator.app/Contents/Info.plist
shows that the Icon file is "Calculator.icns" but doesn't give the path, although it will usually be within the Resources folder within the Application Bundle and in this case it's, /Applications/Calculator.app/Contents/Resources/Calculator.icns
. So you'd have to get the value of CFBundleIconFile
from the App's .plist
file and check whether or not it has the .icns
extension and test to see if it's in the default location, which it normally is but not always.
So in Terminal you'd query using defaults
, e.g.:
defaults read /Applications/Calculator.app/Contents/Info.plist CFBundleIconFile
In AppleScript you could set the results to a variable and go from there, e.g.:
set appIcon to do shell script "defaults read /Applications/Calculator.app/Contents/Info.plist CFBundleIconFile"
It returns: Calculator.icns
Where as,
defaults read /Applications/Utilities/Terminal.app/Contents/Info.plist CFBundleIconFile
Returns: Terminal
Yet the FQP is /Applications/Utilities/Terminal.app/Contents/Resources/Terminal.icns
OTHER TIPS
Try:
do shell script "find " & quoted form of POSIX path of (path to application "Terminal") & " -iname \"*.icns\";"