代码之家  ›  专栏  ›  技术社区  ›  dmkc

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

  •  1
  • dmkc  · 技术社区  · 14 年前

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

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

    set myFile to POSIX file 
      "/untitled folder/funneh/keyboardcat.mov" 
    
    3 回复  |  直到 14 年前
        1
  •  1
  •   slf    14 年前

    我不确定 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
  •  2
  •   markhunte    14 年前

    如果你只是想 FinderItem 如果您更改行,则slf的代码将起作用:

    NSURL* theFileURL = [pathString fileURLWithPath:pathString];
    

    NSURL* theFileURL = [NSURL fileURLWithPath:pathString];
    

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

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

    可以找到代码段 here .

        3
  •  0
  •   Community CDub    8 年前

    在读了一篇文章之后 very detailed AppleScript相关答案,我决定坚持 appscript ,这让事情变得更简单:

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