The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   OS X Developer (http://hintsforums.macworld.com/forumdisplay.php?f=27)
-   -   Set File Icon on Command Line (http://hintsforums.macworld.com/showthread.php?t=70769)

mankoff 04-09-2007 04:07 PM

Set File Icon on Command Line
 
Hi,

I know how to set custom folder icons via the command line, but have not been able to figure out how to do this for files. Does anyone know?

ActiveMan 04-11-2007 04:04 PM

Custom icons
 
In both cases the approach is similar:

To set a file custom icon activate the C file attribute:

$ SetFile -a C mydoc.txt

And the icon must be write into the resource fork.

To set a folder custom icon activate the folder custom icon:

$ SetFile -a C myfolder

and create a Icon? file within the folder with the icon stored in the resource fork:

$ cat Icon?/..namedfork/rsrc

mankoff 04-11-2007 05:04 PM

Quote:

Originally Posted by ActiveMan (Post 371779)
To set a file custom icon activate the C file attribute:

$ SetFile -a C mydoc.txt

And the icon must be write into the resource fork.

But with this syntax, where do I specify the image? How do I write it into the resource fork? Just "cp icon.tiff mydoc.txt/rsrc/" ?

ActiveMan 04-12-2007 04:10 PM

Hello. Try using ResKnife utility to edit the resource fork of the Icon? file. Before editing the Icon? file you can copy the icon? file to another file:

$ ditto Icon? myicon
$ SetFile -a v myicon

mankoff 04-13-2007 09:12 AM

The Icon? file is for folders, not documents.

ActiveMan 04-14-2007 05:45 AM

Yes, files can have a custom icon in the resource fork with an entry of type icns. If a file has not a custom icon, finder shows the default icon according to its extension, creator code, or type code.

biovizier 04-14-2007 08:36 AM

You can use 'sips' to give an image file a custom icon of itself ('sips' acts on the original file so it might be safer to work on a copy):
Code:

/bin/cp imagefile.jpg donorfile.jpg
/usr/bin/sips -i donorfile.jpg

If you just wanted to give an image file a thumbnail custom icon of itself, you can stop there. Otherwise, if you have the "Developer" / "X Code" tools installed, you can use 'DeRez' and 'Rez' to manipulate the resources to copy the newly created 'icns' resource to a file:
Code:

/Developer/Tools/DeRez -only icns donorfile.jpg > tempicns.rsrc
Then copy the the temp file into the icns resource of the recipient file, and set the custom icon file attribute:
Code:

/Developer/Tools/Rez -append tempicns.rsrc -o recipientfile.xyz
/Developer/Tools/SetFile -a C recipientfile.xyz

Finally, restart the "Finder" to view the changes:
Code:

/usr/bin/osascript -e 'tell application "Finder" to quit' -e 'delay 2' -e 'tell application "Finder" to activate'

ActiveMan 04-15-2007 11:21 AM

Right answer. BTW, do you know a command line to list the resources of a file.

mankoff 04-18-2007 12:36 PM

Perfect! That is exactly what I was looking for, and very clearly explained.

Thank you,

-k.

mankoff 04-19-2007 11:38 AM

RezDet in the /Developer/Tools folder should do this. Looks like RezWack might too.

ActiveMan 04-22-2007 10:34 AM

Hello, thank you, but apparently RezDet is not longer avaiable on OS X:

Code:

$ RezDet .
".":
File "."; ### RezDet - Encountered error -43. (GetSysErrText not available in BSD environment. Sorry.) - Fatal I/O error.


mankoff 04-22-2007 01:23 PM

Works on files for me, not folders.

2pac 04-22-2007 06:04 PM

it works both (on files and folders) to me. really good manual for newbies. Thanks

Lutin 08-10-2009 09:08 AM

The instruction on post #7 works for me.

Once I have added an icon to my file, I need to host it on a ftp server.
In the transfer, rsrc fork are not preserved, and when it's finally downloaded, the icon is not visible.

So, I tried to flatten the file first, with RezWack
Code:

/Developer/Tools/RezWack -d myFile -r myFile -o myNewFlatFile
As soon as I do this, the custom icon is not visible anymore.

Any hint on how to do this? (Give a file a custom icon, and have it preserved when being transferred over ftp. The server is an XServe, if it has any incidence).

hayne 08-10-2009 11:04 AM

Quote:

Originally Posted by Lutin (Post 546314)
So, I tried to flatten the file first, with RezWack
Code:

/Developer/Tools/RezWack -d myFile -r myFile -o myNewFlatFile
As soon as I do this, the custom icon is not visible anymore.

Did you run some command to undo the effect of the RezWack? You need to.

Lutin 08-10-2009 11:40 AM

Quote:

Originally Posted by hayne (Post 546332)
Did you run some command to undo the effect of the RezWack? You need to.

Hayne, I am not sure what you mean.
Could you elaborate?
I was under the impression RezWack would combine a data fork and rsrc fork into one file, allowing to not loose any part when being manipulated by a program that doesn't respect forks.

hayne 08-10-2009 02:29 PM

Quote:

Originally Posted by Lutin (Post 546345)
I was under the impression RezWack would combine a data fork and rsrc fork into one file, allowing to not loose any part when being manipulated by a program that doesn't respect forks.

Yes - but when you actually want the file to be used (for an icon), you need the resource fork back the way it was before.
I.e. RezWack produces something like an archive format - not something that can be used by software that expects there to be an actual resource fork.

Lutin 08-10-2009 05:48 PM

Ok, I see. Thank you for the clarification. Now I understand that it's not what I need.

Is there any way to set a custom icon to a file, upload this file to a webserver and still have the custom icon attached to the file and visible by users when they have downloaded the file?

In my case, I need to set up a dmg to have a custom icon. If needed, the server can be an XServe.

hayne 08-10-2009 07:11 PM

Quote:

Originally Posted by Lutin (Post 546386)
In my case, I need to set up a dmg to have a custom icon.

There are various 3rd-party utilities aimed at developers that handle DMG attributes like this.

Lutin 08-11-2009 02:37 AM

Hayne, once again thank you for your reply.
You are of great value to this forum.

I do not want to customize the inside of the DMG (like the icon of the Volume mount once open, or the background), but really the icon of the DMG file (what you see in the finder when you have just downloaded it).

I want to customize the icon of this file like you would do by showing the Get Info panel for the DMG file, and then drag and drop an image onto the icon.
This part is working, but does not "stick" when you transfer the file onto a ftp server.

If the tools you are thinking about allow this, could you point me to one? The only ones I could find only do what I described in the first part of this message.

hayne 08-11-2009 10:28 AM

I don't know if any of the tools handle custom icons for the DMG itself, nor whether it is possible to have such a custom icon transfer automatically from a server. This might be possible if you used some file sharing protocol that supported resource forks.

Ann_at 08-17-2009 10:24 AM

Sorry for hijacking your thread. But it refers to what you said in the beginning:

Quote:

Originally Posted by mankoff (Post 371291)
Hi,

I know how to set custom folder icons via the command line, but have not been able to figure out how to do this for files. Does anyone know?

This interests me.
How do you set a custom folder icon via the command line?

one possibility should be to copy the Icon? file into the folder and then run

sudo /Developer/Tools/SetFile -a C FolderName

Is this really all? Any other way to do this?

kind regards, Ann

jimmytaker 12-15-2013 06:47 PM

Quote:

Originally Posted by biovizier (Post 372418)
You can use 'sips' to give an image file a custom icon of itself ('sips' acts on the original file so it might be safer to work on a copy):
Code:

/bin/cp imagefile.jpg donorfile.jpg
/usr/bin/sips -i donorfile.jpg

If you just wanted to give an image file a thumbnail custom icon of itself, you can stop there. Otherwise, if you have the "Developer" / "X Code" tools installed, you can use 'DeRez' and 'Rez' to manipulate the resources to copy the newly created 'icns' resource to a file:
Code:

/Developer/Tools/DeRez -only icns donorfile.jpg > tempicns.rsrc
Then copy the the temp file into the icns resource of the recipient file, and set the custom icon file attribute:
Code:

/Developer/Tools/Rez -append tempicns.rsrc -o recipientfile.xyz
/Developer/Tools/SetFile -a C recipientfile.xyz

Finally, restart the "Finder" to view the changes:
Code:

/usr/bin/osascript -e 'tell application "Finder" to quit' -e 'delay 2' -e 'tell application "Finder" to activate'

This is true, but it has one disadvantage - the icon is set with a low resolution only. So e.g. if you right click and choose quick look it will look pixelated even if the image you used had a high resolution. To battle this, I dug deeper and the following script sets a real .icns file and works for me:

Code:

#! /usr/bin/env python

from AppKit import *
import sys

NSWorkspace.sharedWorkspace().setIcon_forFile_options_(NSImage.alloc().initByReferencingFile_(sys.argv[2]), sys.argv[1], 0)

usage would be:

Code:

setIcon recipientfile.xyz icon.icns


All times are GMT -5. The time now is 05:51 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.