Get Path to selected file in Finder

前端 未结 2 1100
别跟我提以往
别跟我提以往 2021-02-02 04:26

How would I retrieve an array of paths to the selected files in Finder?

I have searched around but have only found links regarding AppleScript. I have also looked at

2条回答
  •  醉话见心
    2021-02-02 05:21

    Expanding on @Bavarious's (correct) answer, here's how I've gotten the selection from Finder using the Scripting Bridge:

    #import "Finder.h" //my copy is here: https://github.com/davedelong/BetterInfo/blob/master/Finder.h
    
    FinderApplication * finder = [SBApplication applicationWithBundleIdentifier:@"com.apple.finder"];
    SBElementArray * selection = [[finder selection] get];
    
    NSArray * items = [selection arrayByApplyingSelector:@selector(URL)];
    for (NSString * item in items) {
        NSURL * url = [NSURL URLWithString:item];
        NSLog(@"selected item url: %@", url);
    }
    

提交回复
热议问题