如何在cocoa中访问app package contents文件夹?

时间:2015-11-03 11:08:17

标签: objective-c cocoa

我已尝试过此代码,但它适用于下载文件夹(示例路径:/Users/%@/Downloads/),而不适用于应用程序文件夹。所以我想访问Application/iTunes/contents/,但我无法访问此路径。

我的代码:

- (IBAction)showTrashPressed:(id)sender
{
    NSString *trashPath = [NSString stringWithFormat:@"/Users/%@/Applications/",NSUserName()];
    ////NSLog(@"%@",trashPath);

    //[[NSWorkspace sharedWorkspace] selectFile:trashPath inFileViewerRootedAtPath:@"Finder"];

    [[NSWorkspace sharedWorkspace]openFile:trashPath withApplication:@"Finder"];
}

1 个答案:

答案 0 :(得分:2)

通常iTunes.app位于本地域的文件夹Applications而不是用户域。

因此,您可以将路径指定为文字字符串

NSString *iTunesContentsPath = @"/Applications/iTunes.app/Contents";
[[NSWorkspace sharedWorkspace] openFile:iTunesContentsPath withApplication:@"Finder"];

但问NSWorkspace路径

更可靠
NSWorkspace *sharedWorkspace = [NSWorkspace sharedWorkspace];
NSString *iTunesPath = [sharedWorkspace fullPathForApplication:@"iTunes"];
[sharedWorkspace openFile:[iTunesPath stringByAppendingPathComponent:@"Contents"] withApplication:@"Finder"];