Cocoa - 在Finder中打开文件包

时间:2010-10-20 18:48:47

标签: cocoa

我试图在某个包中打开Finder。

我有这个文件,Example.backup,它只是一个带扩展名的文件夹。在查找程序中,您可以右键单击它(显示包内容),我将看到文件。 (没有Contents文件夹)。

我尝试用NSWorkspace的方法打开它,但没有打开该目录的finder,他们要么选择文件,要么用关联的程序打开它。

有没有办法在AppleScript中执行此操作?还是可可?

由于

1 个答案:

答案 0 :(得分:2)

由于还没有人回答,可能没有单线解决方案。

我能想到的只是一种解决方法:使用-R选项调用/ usr / bin / open,这将显示Finder中的给定文件。由于您要显示包的内容,您必须显示内部的任何文件(而不是包本身)。

下行:对空包不起作用(但是你可以只显示包),Finder窗口也会显示最后一项的选择。

NSString *pathToBundle = @"/tmp/test.app";
NSFileManager *fm = [[[NSFileManager alloc] init] autorelease];

// get the last file/directory in the package
// TODO: error handling / empty package
NSString *lastItemInBundle = [[fm contentsOfDirectoryAtPath:pathToBundle error:NULL] lastObject];

// reveal this file in Finder, by using /usr/bin/open
// see -R option in the manpage
NSTask *open = [[[NSTask alloc] init] autorelease];
[open setLaunchPath:@"/usr/bin/open"];
[open setCurrentDirectoryPath:pathToBundle];
[open setArguments:[NSArray arrayWithObjects:@"-R", lastItemInBundle, nil]];
[open launch];