Solved

How do I get Finder to recognize new custom icon?

Posted on 1997-02-26
10
262 Views
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
0
Comment
Question by:craigh022697
10 Comments
 

Expert Comment

by:tristero
Comment Utility
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.
0
 

Author Comment

by:craigh022697
Comment Utility
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...
0
 

Expert Comment

by:tristero
Comment Utility
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.
0
 

Author Comment

by:craigh022697
Comment Utility

0
 

Author Comment

by:craigh022697
Comment Utility
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);
#endif      

      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...

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 1

Expert Comment

by:irac
Comment Utility
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:
http://devworld.apple.com/ngs/lpp/adrpub/docs/dev/techsupport/develop/issue20/20anderson.html

GETTING AND SETTING CUSTOM ICONS

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);
target.Dispose();

// 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;

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

// 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;

emptyList.MakeEmptyList();
ae.PutDescriptor(keyAEData, emptyList);
emptyList.Dispose();

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

ae.Send(&reply, kAENoReply);
ae.Dispose();

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
background.
0
 

Author Comment

by:craigh022697
Comment Utility
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.
0
 

Expert Comment

by:raine
Comment Utility
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
    begin
      localInfo.fdFlags := localInfo.fdFlags - kHasBeenInited;
     localError := FSPSetFInfo(lcoalSpec,localInfo);
    end;

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.


    end;
0
 

Author Comment

by:craigh022697
Comment Utility

0
 
LVL 1

Accepted Solution

by:
atlas earned 50 total points
Comment Utility
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);
            GetDateTime(&lCBlk.dirInfo.ioDrMdDat);
            lCBlk.dirInfo.ioDrDirID = myFSSPtr->parID;
            lErr = PBSetCatInfo(&lCBlk, FALSE);
            lErr = FlushVol(nil, myFSSPtr->vRefNum);

}
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Powershell based monitoring system 2 127
ORA-01403: no data found 43 82
Named range not carried over 10 57
move a line in eclipse 3 60
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now