Objective c 使用Finder+;按路径名/URI处理文件;脚本桥

Objective c 使用Finder+;按路径名/URI处理文件;脚本桥,objective-c,cocoa,applescript,scripting-bridge,Objective C,Cocoa,Applescript,Scripting Bridge,我使用的是从sdef实用程序生成的Finder.h头文件,似乎很多Finder SB方法都需要FinderItem类型的对象来执行任何有意义的操作 如何根据文件的路径或URI创建其中一个对象 我得到的最远的方法是[SBObject initWithProperties]方法,该方法在SB指南中简要提到,但不知道从那里可以走到哪里。换句话说,我想翻译成Objective-C的基本应用脚本是: set myFile to POSIX file "/untitled folder/funneh/

我使用的是从sdef实用程序生成的Finder.h头文件,似乎很多Finder SB方法都需要FinderItem类型的对象来执行任何有意义的操作

如何根据文件的路径或URI创建其中一个对象

我得到的最远的方法是[SBObject initWithProperties]方法,该方法在SB指南中简要提到,但不知道从那里可以走到哪里。换句话说,我想翻译成Objective-C的基本应用脚本是:

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

我不确定
SBObject
,但是如果你想要一个
FinderItem*
,你可以这样得到一个

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

如果您只需要
FinderItem
对象,那么如果您更改以下行,则slf的代码将起作用:

NSURL* theFileURL = [pathString fileURLWithPath:pathString];

但是如果您想要一个HFS样式的路径,那么我找到了这个片段

NSString* path = [(NSString*)CFURLCopyFileSystemPath((CFURLRef)theFileURL, kCFURLHFSPathStyle) autorelease];
 NSLog(@"path= %@",path);
返回字符串“Hard Disk:untitled folder:funneh:keyboardcat.mov”

可以找到这个片段。

在阅读了与AppleScript相关的答案后,我决定坚持下去,这让事情变得更简单:

FNApplication *finder = [[FNApplication alloc] initWithName:@"Finder.app"];
FNReference *ref = [[finder files] byName: @"/users/movies/kitteh.mov"];

我手头没有Xcode,但它看起来确实是个好主意。非常感谢你!方便的谢谢你的片段!
FNApplication *finder = [[FNApplication alloc] initWithName:@"Finder.app"];
FNReference *ref = [[finder files] byName: @"/users/movies/kitteh.mov"];