iVipS
iVipS

Reputation: 1485

'Show Package Contents' through objective C code

We can right click an .app file and see its package contents. To open a file programmatically, we can use NSWorkspace but how to open the app's package contents?

I have searched a lot but seems there is no solution. Please help if I am missing something.

enter image description here

Upvotes: 0

Views: 805

Answers (3)

JWWalker
JWWalker

Reputation: 22717

This seems to work:

NSString* contentsPath = [appPath stringByAppendingPathComponent:@"Contents"];
[[NSWorkspace sharedWorkspace] selectFile: contentsPath
                                inFileViewerRootedAtPath: appPath];

Upvotes: 1

zaph
zaph

Reputation: 112865

Per the comment it seems the OP want to open the contents directory of an app in the finder from code. The following line will accomplish that:

Swift:

let appName = "Safari";
let command:NSString = String(format:"open '/Applications/%@.app/Contents'", appName);
system(command.cStringUsingEncoding(NSUTF8StringEncoding))

Objective-C:

NSString *appName = @"Safari";
NSString *command = [NSString stringWithFormat:@"open '/Applications/%@.app/Contents'", appName];
system([command cStringUsingEncoding:NSUTF8StringEncoding]);

Alternate:

NSString *appPath = @"/Applications/Safari";
NSString *command = [NSString stringWithFormat:@"open '/%@.app/Contents'", appPath];
system([command cStringUsingEncoding:NSUTF8StringEncoding]);

Upvotes: 1

TheAmateurProgrammer
TheAmateurProgrammer

Reputation: 9392

An application is just a directory, show package contents just allows you to access the application as a directory rather than executing it. So you can access a file within an application such as

/Applications/Safari.app/Contents/Info.plist

Upvotes: 0

Related Questions