Using the Unix command line in Mac OS X to manipulate images
Anytime I find myself doing a repetitive task, it is crucial that I discover small tricks and workarounds to make my daily routine as efficient as possible. I have long been a Linux user, so naturally I lean towards opening up the Terminal and using the familiar bash shell environment that I have mastered on many different operating systems. I’m really happy that Apple decided to build Mac OS X on top of a Unix, as it immediately opened the door for any Unix user to jump right in and feel at home within the Macintosh community. Ok – well, maybe not feel “at home”, but it is quite comforting to be navigating my MacBook Pro’s filesystem with my keyboard. Enough of that, lets get into my latest use of the command line in Mac OS X.
So first, I present to you our dilemma:
When writing a review on a Mac OS X Application, we must manually extract an icon from the .app and then convert it to the jpeg format. Oh, and by the way, we only post images on the front page that have dimensions of 112×112.
And now the solution:
Open Terminal.app, Type the following (if your Stickies.app is not in the Applications folder you’ll need to change the first command accordingly):
cp Stickies.icns ~/Desktop
sips -Z 112x112 -s format jpeg ./Stickies.icns --out ./Stickies.jpg
If everything went according to plan you should have a lovely, well-scaled jpeg version of the Stickies icon on your desktop.
Now, because sips is affectionately called the scriptable image processing system, lets make a script to do this for us. Keep in mind, this script could be considered overkill, but its a good exercise in harnessing some of the stuff I’ve learned in the Linux/Unix world in Mac OS X.
Download this file (yankicn.sh.txt).
Rename it to yankicn.sh, and move it into your “Home” folder (accessible by hitting Apple-Shift-H).
Open the terminal and type:
chmod +x yankicn.sh
Now use it by typing:
./yankicn.sh -a /Applications/Stickies.app/
And get even more clever, and change the size and format.
./yankicn.sh -a /Applications/Stickies.app -s 128x128 -f png
In both scenarios you will have a converted image on your desktop.
For an applescript-ish way of manipulating images, out this page: Mac OS X Hints. Note that this applescript is not doing the same thing my shell script does. But it is certainly a starting point.