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"