How do I get Finder to recognize new custom icon?

Posted on 1997-02-26
Last Modified: 2013-12-26
I've got an application that sets up the custom icon resources. It looks like the resources are getting put in the right places and other applications can see that they are there.

One problem: the Finder doesn't update the display of the file. I'm guessing that there is some kind of caching mechanism that is preventing the update. I tried twiddling the modification dates of the files and using ResEdit to flip some of the Finder flags, but nothing seems to work.

Anybody out there have any clues or code snippets?

Thanks in advance,

Craig Hockenberry
Question by:craigh022697

Expert Comment

ID: 1291387
One trick you need to be aware of is that the caching is based on the FOLDER that the file lives in.  You should try changing the modification date of the directory that contains the file.  Be careful to only change the mod date (ie, read the original details, then change the mod date, then write them back).

This usually works, although if you are trying to change standard icons in an application, it usually takes a desktop rebuild.

Author Comment

ID: 1291388
No, this won't work (at least on my 7.5.5 system). I'm trying to add _custom_ icons (not standard icons .. there are no BNDL issues). I've found that I need to send the Finder some AppleEvents and let it set the custom icon through it's normal mechanism (via the cache). I'm not sure what the correct AppleEvents are though...

Expert Comment

ID: 1291389
Hmmm.  I was not talking about custom icons specifically.  Any change you make to a folder usually needed to have the folder touched.

Perhaps what you are thinking of is the 'fupd' event.  I am not sure whether the direct object needs to be the file or the enclosing folder.
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.


Author Comment

ID: 1291390


Author Comment

ID: 1291391
Hit the submit button on the previous one before I typed anything.

Here's what I'm doing...

The following code is distilled from Greg Anderson's article in Develop #10:

      osErr = GetFinder (&finderPSN);
      osErr = AECreateDesc(typeProcessSerialNumber, &finderPSN, sizeof(ProcessSerialNumber), &target);
      osErr = AECreateAppleEvent(kAECoreSuite, kAEGetData, &target, kAutoGenerateReturnID, kAnyTransactionID, &ae);

#ifdef IT_WORKS      
      osErr = AECreateDesc(typeType, &selectionProperty, sizeof(DescType), &specDesc);
      osErr = CreateObjSpecifier(cProperty, &nullDesc, formPropertyID, &specDesc, false, &specifier); // TEMPORARY - disposeInputs parameter should be true
      osErr = AECreateDesc(typeType, &iconProperty, sizeof(DescType), &keyData);
      osErr = CreateObjSpecifier(cProperty, &specifier, formPropertyID, &keyData, false, &directSpecifier); // TEMPORARY - disposeInputs parameter should be true
      osErr = AEPutParamDesc(&ae, keyDirectObject, &directSpecifier);

#else // IT DOESN'T WORK

      osErr = AECreateDesc(typeFSS, file, sizeof(FSSpec), &specDesc);
      osErr = CreateObjSpecifier(cItem, &nullDesc, formPropertyID, &specDesc, false, &specifier); // TEMPORARY - disposeInputs parameter should be true
      osErr = AECreateDesc(typeType, &iconProperty, sizeof(DescType), &keyData);
      osErr = CreateObjSpecifier(cProperty, &specifier, formPropertyID, &keyData, false, &directSpecifier); // TEMPORARY - disposeInputs parameter should be true
      osErr = AEPutParamDesc(&ae, keyDirectObject, &directSpecifier);

      osErr = AESend(&ae, &aeReply, kAEWaitReply, kAENormalPriority, kAEDefaultTimeout, NULL, NULL); // TEMPORARY - (kAENoReply |  kAENeverInteract)
      osErr = AEGetParamDesc(&aeReply, keyAEResult, typeWildCard, &result);

The code from the article shows how to change the icon of the current Finder selection. I want to use a FSSpec instead of the object specifier (he says you can do that in the article). I haven't been able to get this to work (and I've tried _many_ different combinations).

I'm also aware of the 'fupd' event. I've not been successful with that either (from a folder or a file):

      osErr = GetFinder (&finderPSN);
      osErr = AECreateDesc(typeProcessSerialNumber, &finderPSN, sizeof(ProcessSerialNumber), &target);
      osErr = AECreateAppleEvent(kAEFinderSuite, kAEUpdate, &target, kAutoGenerateReturnID, kAnyTransactionID, &ae);
      osErr = AECreateDesc(typeFSS, file, sizeof(FSSpec), &specDesc);
      osErr = AEPutParamDesc(&ae, keyDirectObject, &specDesc);

      osErr = AESend(&ae, &aeReply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, NULL, NULL); // TEMPORARY - (kAENoReply |  kAENeverInteract)

This Apple Events stuff is really hairy... I've read shitloads of documentation and I still don't have a solid grasp of the concepts. And I thought the ScriptEditor was a pain in the ass...


Expert Comment

ID: 1291392
Don't know if this will help but I found it while surfing around looking for applescript information.  You could send similar  events to the finder to get it to change the icon of said thing.

This is from:


The icon bitmap of a file is available through ordinary file system calls, but there are a couple of
different cases to contend with: the icon might be stored in the desktop database, or it could be a custom
icon stored in the resource fork of the file. Some files are "special," and only the Finder really knows what
their icon bitmap should be. The simplest way to get the exact icon bitmap for a file is to ask the Finder
what it is. Once again, Get Data and Set Data are the events to use.

The result of a Get Data event that specifies the icon property of some object is an AERecord that
contains the entire icon family for the item's icon. The record contains parameters whose key is the same
as the individual resources of an icon family (for example, 'ICN#' and 'icl8'); the data stored in these
parameters is identical to the data found in a resource of the same type. A Set Data event takes a record
in the same format, or an empty list if the intention is to remove the custom icon.

Listing 4 shows how to remove the custom icon from every item in the selection. Note that the specifier
"icon of selection" is equivalent to the more complex specifier "icon of every item of selection."

Listing 4 shows how to remove the custom icon from every item in the selection. Note that the specifier
"icon of selection" is equivalent to the more complex specifier "icon of every item of selection."

Listing 4. Removing custom icons from the selection

// tell application "Finder"
//      set icon of selection to empty
// end tell
TAEvent   ae;

TDescriptor target = GetAddressOfFinder();
ae.MakeAppleEvent(kAECoreSuite, kAESetData, target);

// Make an object specifier for "icon of selection" and put it into the
// direct object of the event.
TDescriptor directObjectSpecifier;
TDescriptor selectionSpecifier;
TDescriptor keyData;
TDescriptor nullDescriptor;

selectionSpecifier.MakeObjectSpecifier(cProperty, nullDescriptor,
   formPropertyID, keyData, true);
directObjectSpecifier.MakeObjectSpecifier(cProperty, selectionSpecifier,
   formPropertyID, keyData, true);
ae.PutDescriptor(keyDirectObject, directObjectSpecifier);

// Obviously, a Set Data event needs data. In the case of this sample,
// the data we want is "empty," which is represented by an empty list.
TDescriptor emptyList;

ae.PutDescriptor(keyAEData, emptyList);

// Send the event and dispose of it once it has been sent.
TAEvent   reply;

ae.Send(&reply, kAENoReply);

The sample application Finder Tricks on the CD has a feature that changes the icons of all the items in the
frontmost Finder window -- each item is given some other item's icon. Other than serving as a useful
example of how to send events to the Finder, this sample doesn't have much utility, although it does do an
admirable job at messing up the appearance of Finder windows.

An application can change an item's icon by writing the custom icon directly into the appropriate resources
in the file and then setting the "custom icon bit" using the file system, instead of sending an event to the
Finder -- but the change won't take effect right away. The reason for the delay is that the Finder isn't
notified when the contents of the disk change, so it must periodically poll the file system to find out
whether it needs to redraw any items in its open windows. Polling happens only every now and again, so that
the Finder doesn't eat up every spare CPU cycle on the machine when it's just sitting idle in the

Author Comment

ID: 1291393
This code is the same as the code from the Develop article. I understand this code .. the problem is that I haven't been successful at changing the selection property (the currently selected item on the finder desktop) to a file specified by my application (a FSSpec).

I've gotten some hints from the author of the article and am trying them out now.

Expert Comment

ID: 1291394
This may sound basic, but are you restting the "inited" bit in the finder flags ?

localError := FSPGetFInfo(localSpec, localInfo);
if localError = noErr then
  if BAND(localInfo.fdFlags,kHasBeenInited) <> 0 then
      localInfo.fdFlags := localInfo.fdFlags - kHasBeenInited;
     localError := FSPSetFInfo(lcoalSpec,localInfo);

and as correctly pointed out in the comments, you then need to change the modification date of the enclosing folder to force the finder to notice it.


Author Comment

ID: 1291395


Accepted Solution

atlas earned 50 total points
ID: 1291396
the following snippet is what I use to force the Finder to update. It does this by changing the modification date of the parent ID of the file, the Finder will now update the contents.
Pass this routine an FSSpec to the file to be updated.

void ForceFinderUpdate(FSSpecPtr myFSSPtr)
      OSErr                  lErr;
      CInfoPBRec            lCBlk;
      if (myFSSPtr->parID != 1)                                          
            lCBlk.dirInfo.ioNamePtr = 0L;
            lCBlk.dirInfo.ioVRefNum = myFSSPtr->vRefNum;            
            lCBlk.dirInfo.ioDrDirID = myFSSPtr->parID;
            lCBlk.dirInfo.ioFDirIndex = 0;
            lCBlk.dirInfo.ioCompletion = 0;      
            lErr = PBGetCatInfo(&lCBlk, FALSE);
            lCBlk.dirInfo.ioDrDirID = myFSSPtr->parID;
            lErr = PBSetCatInfo(&lCBlk, FALSE);
            lErr = FlushVol(nil, myFSSPtr->vRefNum);


Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Expand macro to ask for filename column 8 37
Looking for a specific application/software 2 109
ffmpeg - "rtsp://...... Operation not permitted" 4 80
haveThree challenge 22 118
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question