A Quicker Way to Extract Application Icons Using AppleScript
Update: Check out MacTipper’s comment below – it pretty much renders unnecessary much of this post! And then head over to The MacTipper Blog for more excellent OS X tips.
In a previous post, I wrote up 10 shareware apps that I think most Mac users would benefit from knowing about. To dress up the post, I wanted to display each application’s icon beside its write up. I’ve written about how to create and change your own application icons, and the process of extracting icons for publishing on the web isn’t much different. Here’s how I do it.
Step one is to open up your Applications folder and select the app whose icon you want to extract. Open the information window for the app (press Command + I or choose “Get Info” from the File menu). Click once on the app’s icon in the upper left corner of the info pane and a light blue glow will surround it (signifying that it has been selected – see right). Copy the icon to the clipboard.
Once your icon is in the clipboard, open up Preview. Now press Command + N (or select ‘New From Clipboard’ from the File menu) and your icon will open up in a new window.
Icons in OS X are a special .icn format. Each .icn file is made up of several .tiff files scaled to various sizes (this is so that no matter how big you view an application’s icon – up to 512 pixels – it will remain crisp). Since .tiff files are spottily supported by web browsers, it’s best to convert the image to png format. In the right panel, select the icon which is closest in size to the image you would like to use for the web and select ‘Save as…’ from the File menu (alternatively, press Command + Shift + S). Rename the image and select PNG from the drop down save menu. Simple as that you have your icon on a transparent background ready for use on your site.
Automating the Process with AppleScript
If you need to grab a number of app icons, then above method can quickly become tedious. Select app, get info, highlight icon, copy icon, close info window, launch preview, create new document… I’ve written a small script that automates this process in the majority of cases:
tell application "Finder" activate try set theApp to selection as alias set theIcons to name of every item of folder ¬ ((theApp as string) & "Contents:Resources:") ¬ whose name ends with ".icns" set iconNum to number of items in theIcons if iconNum is less than 5 then repeat with eachIcon in theIcons open ((theApp as string) & ¬ "Contents:Resources:" & eachIcon) end repeat else open information window of theApp end if on error activate me display dialog "Something went wrong.¬ Make sure an application is selected in¬ Finder." with icon 2 end try end tell
This script queries the hidden Resources package buried in AppName/Contents/Resources (if you want to have a look around in here, right click on an app icon and select “Show Package Contents” from the contextual menu). This is where the application’s icon is stored. The script returns all the files that end in “.icns.” If the number of .icns files is less than 4, the script will open them up in Preview. From here, just select the one you want and save it as a PNG. On the other hand, if the script finds more than 4 icons, it will open the Information Window for the application and you can extract the icon manually using method outlined above.
4 is an arbitrary number – you can set it to whatever you want. But setting some number is necessary to stop applications which have 100+ icons from flooding your screen with Preview windows (I learned this the hard way). Apps that will do this are the ones that can open a large number of file types and include a distinct icon for each of them. Setting a limit number, whatever it is, will save you from a headache if you run the script on an application with too many .icns files.
Make the Script Even More Useful
I run the majority of my scripts from the script launcher FastScripts, but I decided that the most useful place for this one would be imbedded in Finder’s toolbar. To accomplish this, select “Application” from the Format menu when saving your script. Once you have it saved, open a finder window, right click on the toolbar and select “Customize Toolbar…” Now drag your AppleScript application to the toolbar and click “Done.” From now on, any application’s icon is just a click away.
As you’ll notice in the screenshot above, I added an icon I made to the script itself so that it looks good in the toolbar (and I can tell what it is). It’s pictured to the left – nice, soothing Helvetica! You can download the .icns file and use it for your Open Icon application if you like. (To change an app’s icon, paste your new icon onto its Information Window icon. If you’re not sure how to set application or file icons, this post goes into the process in some detail.)
That’s all for now. I hope this little script helps you out.
August 25th, 2008 at 12:24 am
A much easier way to copy icons is to just select the app, hit Cmd-C, switch over to Preview and hit Cmd-N.
One could automate this with interface scripting if you wanted to:
tell application "System Events"tell process "Finder"
keystroke "c" using command down
end tell
end tell
tell application "Preview"
activate
tell application "System Events"
keystroke "n" using command down
end tell
end tell
MacTipper
http://www.mactipper.com/
August 25th, 2008 at 12:34 am
MacTipper:
Haha – how is it that I discover the most convoluted way to do something and then spend an hour writing a script to automate it?! Excellent (and embarrassing?) comment! Touché.
August 25th, 2008 at 5:39 pm
In case you only need to extract the application icon, you can also read the application’s Info.plist file, which contains the file name of the corresponding ICNS file stored in the app’s Resources folder:
set plistpath to “/Applications/TextEdit.app/Contents/Info”
set command to “defaults read ” & quoted form of plistpath & ” CFBundleIconFile”
set filename to (do shell script command)
But sometimes the “.icns” suffix is missing, so you need to check this.
I also wrote a free AppleScript to do the conversion. It’s called iconoodle :-)
http://bbs.macscripter.net/viewtopic.php?id=25281
Best regards from rainy Germany!
August 25th, 2008 at 10:28 pm
Martin:
Thanks for your input! It never dawned on me to look in the application’s preferences file, but I suddenly suspect that a wealth of other useful actions could be performed starting from there.
I took a look at your script and will definitely give it a run. Outstanding piece of work!
And may His Noodly Appendage touch all of our scripts!
August 26th, 2008 at 12:03 am
Peter: And may His Noodly Appendage touch all of our scripts!
I had to laugh so hard when I read this, I almost spilled coffee over my PowerBook screen :)
Just a note: Unfortunately only Cocoa apps have a Info.plist file, Carbon apps don’t. For example it doesn’t work for MS Office 2004 apps. Waiting for more great articles!
August 24th, 2010 at 5:29 am
I appreciate the tips here. Unfortunately I am only a bare middleweight in Applescript and just a bare beginner in terminal. So I followed the first example but missed the joke as it was supposed to get easier.
My needs are: I built a database to track my ToDos. I have set up containers or menu objects that will launch my favorite apps, apps that I normally use in the to do events (Safari, iCal, etc.)
I was hoping that it might be possible to use terminal or AS to grab the application icon, probably the smaller number three version, once I set the button to am app.
I was thinking of setting the db up to do this in case I distribute it, I can do it pretty easily manually already.
Example; I set a blank button to Safari. Result, selecting Safari opens safari, but the button now also shows the small Safari icon on it. Most of the solutions so far open a whole list of .icn files. I want to select just the one. THen I can figure out what to do with it in my db project, so long as I have it selected and maybe copied to the clipboard.
I hope all that made some sense to someone?
Much thanks
David