Does any one know how I could retrieve a list of selected files from the active finder window? I have no experience at all with AppleScript and your help will be appreciated!
I tried using the following code from another answer on SO, but I could get it to return anything at all. No error, no results, just nothing. I understand that even if it did work, then it would just get me the first file, I need the complete selection of files..
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:
@"tell application \"Finder\"\n"
"set selectedItems to selection\n"
"if ((count of selectedItems) > 0) then\n"
"set selectedItem to (item 1 of selectedItems) as alias\n"
"container window of selectedItem\n"
"end if\n"
"end tell\n"];
if (script == nil) {
NSLog(@"failed to create script!");
}
NSAppleEventDescriptor *result = [script executeAndReturnError:&errorMessage];
if (result) {
// POSIX path returns trailing /'s, so standardize the path
NSString *path = [[result stringValue] stringByStandardizingPath];
}
for (id key in errorMessage) {
NSLog(@"key: %@, value: %@", key, [errorMessage objectForKey:key]);
}
Edit
I was printing out the error dictionary before executing the script, that is why there was no error. This is what I get when I select two files in a finder window:
2013-07-23 13:36:14.817 myAPP[12959:303] key: NSAppleScriptErrorMessage, value: Finder got an error: Can’t get container window of document file "myAPP-logo-white-n-black.png" of folder "untitled folder" of folder "Desktop" of folder "Shumais" of folder "Users" of startup disk.
2013-07-23 13:36:14.817 myAPP[12959:303] key: NSAppleScriptErrorRange, value: NSRange: {151, 16}
2013-07-23 13:36:14.817 myAPP[12959:303] key: NSAppleScriptErrorBriefMessage, value: Can’t get container window of document file "myAPP-logo-white-n-black.png" of folder "untitled folder" of folder "Desktop" of folder "Shumais" of folder "Users" of startup disk.
2013-07-23 13:36:14.818 myAPP[12959:303] key: NSAppleScriptErrorNumber, value: -1728
2013-07-23 13:36:14.818 myAPP[12959:303] key: NSAppleScriptErrorAppName, value: Finder
Thank You! Shumais