使用Finder + ScriptingBridge通过路径名/ URI处理文件

时间:2010-07-12 13:11:46

标签: objective-c cocoa applescript scripting-bridge

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

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

我得到的最远的是SB指南中简要提到的[SBObject initWithProperties]方法,但不知道从哪里开始。那么,我想将其转换为Objective-C的基本AppleScript,换句话说:

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

3 个答案:

答案 0 :(得分:2)

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

NSURL* theFileURL = [pathString fileURLWithPath:pathString];

NSURL* theFileURL = [NSURL fileURLWithPath:pathString];

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

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

返回一个字符串“Hard Disk:untitled folder:funneh:keyboardcat.mov”

可以找到代码段here

答案 1 :(得分:1)

我不确定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];

答案 2 :(得分:0)

在阅读了very detailed与AppleScript相关的答案后,我决定坚持使用appscript,这让事情变得更加轻松:

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