是否有人知道如何从活动的查找程序窗口中检索所选文件的列表? 我对AppleScript没有任何经验,我们将非常感谢您的帮助!
我尝试在SO上使用另一个答案中的以下代码,但我可以让它返回任何内容。没有错误,没有结果,没有任何结果。我明白,即使它确实有效,那么它只会让我得到第一个文件,我需要完整的文件选择..
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]);
}
修改
我在执行脚本之前打印出错误字典,这就是为什么没有错误。 这是我在finder窗口中选择两个文件时得到的结果:
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
谢谢! Shumais
答案 0 :(得分:0)
更新2 。 所以我才意识到我和另一个答案是让你获得第一个文件的容器。因为就我而言,我会更新你提供的代码并更正它,而不是注意你说你需要路径列表
所以这里有一些代码可以获取列表(作为查找器中所有选定项目的posix路径。
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];
这将返回一个数组:
pathArray =( “/Users/Shumais/Desktop/newFolder/testdrop/image1.jpg” “/Users/Shumais/Desktop/newFolder/testdrop/image2.jpg” “/Users/Shumais/Desktop/newFolder/testdrop/image3.jpg” )
我已经离开了其他代码,因为它可能有用
更新后的代码。经过测试和工作。
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]);
}
退货 - &gt; “Macintosh HD:用户:Shumais:桌面:”
或者获取POSIX路径:
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]);
}
返回 - &gt; “/ Users / Shumais / Desktop / untitled folder”
答案 1 :(得分:0)
看起来容器窗口不起作用。请尝试使用Parent:
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