2

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

Shumais Ul Haq
  • 1,427
  • 1
  • 15
  • 26
  • Hi, I have UPDATEDmy code. Both tested and work. – markhunte Jul 26 '13 at 20:52
  • Hi, So I just realised that my and the other answer is getting you the container of the first file. Because for my part I red the code you supplied and corrected it rather than pay attention to you saying you need a **path list** So I have update the answer with some code that gets the list – markhunte Jul 27 '13 at 18:01

2 Answers2

0

UPDATE 2. So I just realised that my and the other answer is getting you the container of the first file. Because for my part I red the code you supplied and corrected it rather than pay attention to you saying you need a path list

So here is some code that get the list ( as posix paths of all the selected items in the finder.

 NSMutableArray * pathArray =[[NSMutableArray alloc ] initWithCapacity:10];
    NSDictionary* errorMessage = [NSDictionary dictionary];
    NSString *code = @"set biglist to {}\n tell application \"Finder\" to set theSeletion to (get selection)\n if (count of theSeletion) > 0 then\n repeat with i from 1 to number of items in theSeletion\n set this_item to POSIX path of (item i of theSeletion as alias)\n copy this_item to end of biglist\n end repeat\n return biglist\n  end if\n  ";
    NSLog(@"code =  %@ ",code);
    NSAppleScript *script = [[NSAppleScript alloc] initWithSource:code];
    NSAppleEventDescriptor *result = [script executeAndReturnError:&errorMessage];

    count = [result numberOfItems];
    for (int i = 1; i <= count; ++i) {
    NSAppleEventDescriptor *desc =  [result descriptorAtIndex:i]  ;
    id thisPath = [desc stringValue];


        [pathArray addObject:thisPath];


    }

    if (script == nil) {
        NSLog(@"failed to create script!");
    }

    for (id key in errorMessage) {
        NSLog(@"key: %@, value: %@", key, [errorMessage objectForKey:key]);
    }


      NSLog(@"pathArray =  %@ ",pathArray);

    [pathArray release];

This will return an Array:

pathArray = ( "/Users/Shumais/Desktop/newFolder/testdrop/image1.jpg", "/Users/Shumais/Desktop/newFolder/testdrop/image2.jpg", "/Users/Shumais/Desktop/newFolder/testdrop/image3.jpg" )

I have left the other code as it may be useful

UPDATED CODE. Both tested and work.

 NSDictionary* errorMessage = [NSDictionary dictionary];
    NSString *code = @"tell application \"Finder\"\n set theSeletion to (get selection)\n if (count of theSeletion) > 0 then\n return   (container of item 1 of theSeletion as alias)  as string \n end if\n end tell";
     NSLog(@"code =  %@ ",code);
    NSAppleScript *script = [[NSAppleScript alloc] initWithSource:code];
    NSAppleEventDescriptor *result = [script executeAndReturnError:&errorMessage];

    if (script == nil) {
        NSLog(@"failed to create script!");
    }
    if (result) {
        // POSIX path returns trailing /'s, so standardize the path
        NSString *path = [[result stringValue] stringByStandardizingPath];

          NSLog(@"path =  %@ ",path);
    }

    for (id key in errorMessage) {
        NSLog(@"key: %@, value: %@", key, [errorMessage objectForKey:key]);
    }

Returns --> "Macintosh HD:Users:Shumais:Desktop:"

Or to get the POSIX path:

 NSDictionary* errorMessage = [NSDictionary dictionary];
    NSString *code = @"tell application \"Finder\"\n set theSeletion to (get selection)\n if (count of theSeletion) > 0 then\n return POSIX path of (container of item 1 of theSeletion as alias)\n end if\n end tell";
     NSLog(@"code =  %@ ",code);
    NSAppleScript *script = [[NSAppleScript alloc] initWithSource:code];
    NSAppleEventDescriptor *result = [script executeAndReturnError:&errorMessage];

    if (script == nil) {
        NSLog(@"failed to create script!");
    }
    if (result) {
        // POSIX path returns trailing /'s, so standardize the path
        NSString *path = [[result stringValue] stringByStandardizingPath];

          NSLog(@"path =  %@ ",path);
    }

    for (id key in errorMessage) {
        NSLog(@"key: %@, value: %@", key, [errorMessage objectForKey:key]);
    }

returns -> "/Users/Shumais/Desktop/untitled folder"

markhunte
  • 6,805
  • 2
  • 25
  • 44
  • Thank you for the help! Even before this last edit, your answer, and the other one too, was helpful and enabled me to put together a script which did what I needed. It is almost the same as yours but there are some differences with the same end result though. AppleScript is a real pain, now I am trying to figure out how to run it with NSTask since the scripts freezes the whole app till it completes, but I guess that is another question :) I wish there was more help regarding these topics, that which is available is usually very basic introductory stuff. Thanks again. – Shumais Ul Haq Jul 27 '13 at 18:19
0

It looks like Container Window doesn't work. Try Parent instead:

set parentFolder to ""
tell application "Finder"
    set selectedItems to selection
    if ((count of selectedItems) > 0) then
        set selectedItem to (item 1 of selectedItems) as alias
        set parentFolder to the parent of selectedItem
    end if
end tell
return parentFolder
Mark
  • 2,380
  • 11
  • 29
  • 49
  • I tried your script while selecting a couple of files in a desktop folder and this is what I get:: key: NSAppleScriptErrorMessage, value: Finder got an error: Can’t get container window of document file "app-preferences.png" of folder "untitled folder" of folder "Desktop" of folder "Shumais" of folder "Users" of startup disk... I must tell you that I have no experience with Apple scripts at all. – Shumais Ul Haq Jul 26 '13 at 15:15
  • I replaced the two single quotes with two double quotes, in the first line. Does that make a difference? What happens if you copy the script, paste it in Script Editor and run it just like that? – Mark Jul 26 '13 at 18:34
  • While there is nothing wrong with your script, it does only get the container of the selection :) None the less, it did help me out in putting together a script of my own. Thanks! – Shumais Ul Haq Jul 27 '13 at 18:22
  • It gets the parent folder of the selection. If that's not what you want, then you need to explain better. – Mark Jul 28 '13 at 09:09