I’ve been looking for a way to force the Finder to update its icon
cache for freshly mounted volumes, as mentioned previously here:

http://groups.google.com/group/macfuse/browse_thread/thread/5bac2bdd372ae26d

Amit helpfully suggested that the Finder could be relaunched, but I
was hoping to find a less invasive technique. This is a summary of
what I’ve found. I hope it can be usefully to someone else.

The only solution I have working so far is to use AppleScript to call
the “update” command in the Finder dictionary, which is documented
thus: “Update the display of the specified object(s) to match their on-
disk representation”.

Calling this command with an alias to a volume whose icon is out of
date has the desired effect (the icon is updated). For example:

tell application "Finder" to update ¬
  alias "Continuity_HD:Volumes:mountpoint"

Making the same call from a Cocoa application is a little trickier.

- (void)updateFinderIcon:(NSString*)mountedVolume
{
  NSString *source = [NSString stringWithFormat:
    @"tell application \"Finder\" to update alias \
    ((POSIX file \"[EMAIL PROTECTED]") as string)", mountedVolume];
  NSAppleScript *scriptObject =
    [[NSAppleScript alloc] initWithSource:source];
  [scriptObject executeAndReturnError:nil];
}

Some explaination is in order. In the AppleScript above (POSIX file
“mountedVolume”) translates a POSIX path give in ‘mountedVolume’ into
an AppleScript style file handle. Unfortunately, it doesn’t actually
transform into a file handle, but instead into an undocumented (even
by AppleScript standards) “furl” class object which the “update”
command does not understand. So I cast the furl to a string, and then
the string to a file alias.

There are some other problems with the “POSIX file” command in
AppleScript. For example, it is known to not work for POSIX paths that
include a colon. Additionally, it might be possible to maliciously
format a mountPoint that would do Bad Things when passed into this bit
of AppleScript and executed. You may wish to validate or sanitize it
somehow. Also, you can save the return and error values from calling
executeAndReturnError:, if they’re important to you.

If you want to update the icon when the volume is mounted, then you
can register an object to receive notifications when volumes are
mounted:

[[[NSWorkspace sharedWorkspace] notificationCenter]
    addObserver:self
    selector:@selector(volumeDidMount:)
    name:NSWorkspaceDidMountNotification object:nil];

My implementation for volumeDidMount: looks like this:

- (void)volumeDidMount:(NSNotification*)notification
{
  NSString *mountedVolume = [[notificationuserInfo]
                                valueForKey:@"NSDevicePath"];

  if(mountedVolume == aVolumeThatYouCareAbout) {
    [self updateFinderIcon:mountedVolume];
}

I welcome feedback, suggestions, and improvements.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"MacFUSE" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/macfuse?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to