Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set icon on file or directory using CLI on OS X?

To set an icon on a file or directory is straight forward using the "Get Info" dialog in Finder.

  1. copy image from e.g. Preview
  2. open "Get Info" on file or directory
  3. press TAB to select the icon
  4. paste Cmd-V

But how do you do this using the command line?

like image 623
Gregory Vincic Avatar asked Dec 03 '11 23:12

Gregory Vincic


People also ask

How do I change a file icon in Mac OS X?

On your Mac, select the file or folder. Choose File > Get Info in the menu bar. At the top of the Info window, select the small custom icon.

Can you change the folder icon on a Mac?

Right-click a folder > select Properties > Customize > Change icon > choose from the available icons > and click OK to apply your choice. Alternatively, choose Browse to search a specific custom icons folder. You can follow these same steps to change folder icons in Windows 11.

How do I change a folder icon?

Right-click the folder that you want to create a shortcut to, and then click Create Shortcut. Right-click the shortcut you created, click Properties, and then click Change Icon. Click the picture of the icon you would like the shortcut to use, click OK, and then click OK.


1 Answers

Here is a bash script "setIcon.sh" for it

#!/bin/sh
# Sets an icon on file or directory
# Usage setIcon.sh iconimage.jpg /path/to/[file|folder]
iconSource=$1
iconDestination=$2
icon=/tmp/`basename $iconSource`
rsrc=/tmp/icon.rsrc

# Create icon from the iconSource
cp $iconSource $icon

# Add icon to image file, meaning use itself as the icon
sips -i $icon

# Take that icon and put it into a rsrc file
DeRez -only icns $icon > $rsrc

# Apply the rsrc file to
SetFile -a C $iconDestination

if [ -f $iconDestination ]; then
    # Destination is a file
    Rez -append $rsrc -o $iconDestination
elif [ -d $iconDestination ]; then
    # Destination is a directory
    # Create the magical Icon\r file
    touch $iconDestination/$'Icon\r'
    Rez -append $rsrc -o $iconDestination/Icon?
    SetFile -a V $iconDestination/Icon?
fi

# Sometimes Finder needs to be reactivated
#osascript -e 'tell application "Finder" to quit'
#osascript -e 'delay 2'
#osascript -e 'tell application "Finder" to activate'

rm $rsrc $icon
like image 103
Gregory Vincic Avatar answered Nov 28 '22 16:11

Gregory Vincic