Replacing app icons using Hazel1329961200
Here is another script I cooked up recently. I’m particularly proud of this one; it is quite clever and useful.
Use case (an edgy one, I’m sure): I use Max Rudberg’s Obsidian Menu Bar. It’s a lovely modification which basically makes OS X menu bar black. Max’s installer comes with quite a few icons for the most popular services and apps, but it doesn’t contain all of them. And I don’t want to bug the poor guy.
So I ended up creating this rule in Hazel:

Very simple, isn’t it? Whenever I want to customize an app, I add its name as a sub rule. And before I do that, I need to place customized resources in a special folder:

It is important that each application has its own folder, named EXACTLY after the app. See “Day One Agent” on top? That’s because Day One’s dev is a sneaky man: the app which has the menubar ribbon icon is actually buried within the main app’s package. A bit of an exception, which the AppleScript for processing icons handles perfectly. Here goes:
-- Enter your OS X username and password for SmoothScriptOperation™.
set _user to "yourosxusername"
set _pass to "yourpassword"
-- Set paths to your home folder and Applications folder.
set _home to POSIX path of (path to home folder)
set _apps to POSIX path of (path to applications folder)
-- Set the folder where you keep app icons sorted into folders named JUST like your apps (minus .app extension of course).
set _customizing to "Customizing/Apps/"
-- You can use this for for testing purposes. Otherwise Hazel will pass the app into theFile variable.
-- set theFile to (POSIX file _apps as text) & "Dropbox.app"
-- Ask Finder figure out the name of the app and remove ".app" extension.
tell application "Finder" to set _app to text 1 thru -5 of (get name of file theFile)
-- A small exception for Day One app. You can add more.
if _app is "Day One Agent" then
set _pre to "Day One.app/Contents/Library/LoginItems/"
else
set _pre to ""
end if
-- Define paths for script to operate on.
set _appath to _apps & _pre & _app & ".app/Contents/Resources"
set _respath to _home & _customizing & _app & "/"
-- This is where the action happens. A script that quits the processed app and copies customized resources to where they should be.
try
do shell script "killall " & quoted form of _app
end try
try
do shell script "cp -R" & space & quoted form of _respath & space & quoted form of _appath user name _user password _pass with administrator privileges
end try
-- Give'em a second to breathe…
delay 1
-- Relaunch the app.
tell application _app to launch
-- Display a Growl notification.
tell application id "com.Growl.GrowlHelperApp"
register as application "Hazel Notifications" all notifications {"Info", "Error"} default notifications {"Info", "Error"} icon of application "HazelHelper.app"
notify with name "Info" title _app description "Icons replaced successfully." application name "Hazel Notifications" priority 0 icon of application _app
end tell
You can use this to replace menubar icons, app icons, NIBs — whatever is in your application.app/Contents/Resources folder of choice. Of course as long as it doesn’t break the app. Make backups before playing!