Get the path to the selected file in Finder

How do I get an array of paths to selected files in Finder?

I searched, but found links only to AppleScript. I also looked at NSWorkspace and NSFileManager , but found nothing.

+4
source share
2 answers

If you can get a list of the selected files in this Finder window using AppleScript, you can probably use the Scripting Bridge in Cocoa to interact with the Finder. Copying documentation for apples,

Scripting Bridge is a framework and technology that makes it easy for Cocoa developers to manage and interact with scripted applications. Instead of including AppleScript scripts in your application or dealing with the complexities of sending and processing Apple events, you can simply send Objective-C messages to an object representing the scripting application. Your Cocoa application can do anything that the AppleScript script can use, but it does so in Objective-C code, which is integrated with the rest of your project code.

There is no Cocoa class that represents the Finder, or rather, the Finder window. Finder is an application and a scripting application, so Scripting Bridge is the way to go.

+5
source

Turning to @Bavarious's (correct) answer, here's how I got a selection from Finder using a script 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); } 
+15
source

Source: https://habr.com/ru/post/1333767/


All Articles