Search code examples
objective-ccocoaapplescriptscripting-bridge

Working with a file by pathname/URI using Finder + ScriptingBridge


I'm using the generated Finder.h headers from the sdef utility, and it appears that a lot of Finder SB methods require objects of type FinderItem to do anything meaningful with.

How would I go about creating one of them objects based on a path to or URI of the file?

The farthest I've got is the [SBObject initWithProperties] method briefly mentioned in the SB guide but have no clue where to go from there. The basic AppleScript I'd like to translate into Objective-C is then, to put it yet another way:

set myFile to POSIX file 
  "/untitled folder/funneh/keyboardcat.mov" 

Solution

  • I'm not sure about SBObject, but if you want a FinderItem*, this is how you could get one.

    NSString* pathString = [@"/untitled folder/funneh/keyboardcat.mov" stringByExpandingTildeInPath];
    NSURL* theFileURL = [pathString fileURLWithPath:pathString];
    FinderApplication* finder = [SBApplication applicationWithBundleIdentifier:@"com.apple.finder"];
    FinderItem * theItem = [[finder items] objectAtLocation: theFileURL];