I'm using the generated Finder.h headers from the sdef utility, and it appears that a lot of Finder SB methods require objects o开发者_运维技巧f 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"
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.
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];
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"];
精彩评论