1

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" 
dmkc
  • 1,161
  • 1
  • 15
  • 22

3 Answers3

2

If you just want the FinderItem object then slf's code will work if you change the line:

NSURL* theFileURL = [pathString fileURLWithPath:pathString];

to

NSURL* theFileURL = [NSURL fileURLWithPath:pathString];

But if you want a HFS style path then I found this snippet.

NSString* path = [(NSString*)CFURLCopyFileSystemPath((CFURLRef)theFileURL, kCFURLHFSPathStyle) autorelease];
 NSLog(@"path= %@",path);

Which returns a string of "Hard Disk:untitled folder:funneh:keyboardcat.mov"

The snippet can be found here.

markhunte
  • 6,805
  • 2
  • 25
  • 44
1

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];
slf
  • 22,595
  • 11
  • 77
  • 101
  • I don't have Xcode handy here but it does look like the right idea. Thank you very much! – dmkc Jul 12 '10 at 15:38
0

After reading a very detailed AppleScript related answer, I decided to stick with appscript, which made things much easier:

FNApplication *finder = [[FNApplication alloc] initWithName:@"Finder.app"];
FNReference *ref = [[finder files] byName: @"/users/movies/kitteh.mov"];
Community
  • 1
  • 1
dmkc
  • 1,161
  • 1
  • 15
  • 22