In a continuation of the Cocoa Snippets experiment, we're going to talk about the basics of NSWorkspace in several parts. First, we're going to look at how you can use this class to work with other applications.
You can use NSWorkspace to get a list of running applications. For example:
NSWorkspace * ws = [NSWorkspace sharedWorkspace];
NSArray * apps = [ws launchedApplications];
NSLog (@"%@", apps);
The output would look something like this:
{
NSApplicationBundleIdentifier = "com.apple.finder";
NSApplicationName = Finder;
NSApplicationPath = "/System/Library/CoreServices/Finder.app";
NSApplicationProcessIdentifier = 184;
NSApplicationProcessSerialNumberHigh = 0;
NSApplicationProcessSerialNumberLow = 1048577;
},
{
NSApplicationBundleIdentifier = "com.macromates.textmate";
NSApplicationName = TextMate;
NSApplicationPath = "/Applications/TextMate.app";
NSApplicationProcessIdentifier = 220;
NSApplicationProcessSerialNumberHigh = 0;
NSApplicationProcessSerialNumberLow = 2490369;
},
{
NSApplicationBundleIdentifier = "com.apple.iTunes";
NSApplicationName = iTunes;
NSApplicationPath = "/Applications/iTunes.app";
NSApplicationProcessIdentifier = 242;
NSApplicationProcessSerialNumberHigh = 0;
NSApplicationProcessSerialNumberLow = 3145729;
},
The bundle identifier is set in Info.plist, and is the unique string that Launch Services needs to manage the application in the system. The process indentifier is the unix pid number. This could be used, for example, with the 'kill' command in the shell.
We can simplify and clean up this output a bit using NSArray's implementation of KVC:
NSWorkspace * ws = [NSWorkspace sharedWorkspace];
BOOL result = [ws launchApplication:@"Safari"];
NSArray * apps;
apps = [ws valueForKeyPath:@"launchedApplications.NSApplicationName"];
NSLog (@"%@", apps);
Which results in this:
(
Finder,
TextMate,
iTunes
)
We can also launch an application. The most simple way to do this is with -launchApplication:
NSWorkspace * ws = [NSWorkspace sharedWorkspace];
BOOL wasLaunched = [ws launchApplication:@"Safari"];
if ( wasLaunched )
NSLog (@"Safari was launched");
else
NSLog (@"Safari was not launched");
NSArray * apps;
apps = [ws valueForKeyPath:@"launchedApplications.NSApplicationName"];
NSLog (@"%@", apps);
Keep in mind, though, that Safari may not be actually up and running by the time you reach the next line, even if the result is "true":
MyApp[620] Safari was launched
MyApp[620] (
Finder,
TextMate,
iTunes
)
You may want to launch an application but keep your app in the foreground. This can be done with by sending a more detailed launch message:
NSWorkspace * ws = [NSWorkspace sharedWorkspace];
[ws launchAppWithBundleIdentifier: @"com.apple.Safari"
options: NSWorkspaceLaunchWithoutActivation
additionalEventParamDescriptor: NULL
launchIdentifier: nil];
Finally, you might want to get an icon for an application. This is done in two steps:
NSWorkspace * ws = [NSWorkspace sharedWorkspace];
NSString * path = [ws fullPathForApplication:@"Safari"];
NSImage * icon = [ws iconForFile: path];
NSLog (@"%@", icon);
This will give you something like the following:
NSImage 0x39b940 Size={32, 32} Reps=(
NSBitmapImageRep 0x39d5e0 Size={128, 128} ColorSpace=NSDeviceRGBColorSpace BPS=8 BPP=32 Pixels=128x128 Alpha=YES Planar=NO Format=0,
NSBitmapImageRep 0x39dd20 Size={32, 32} ColorSpace=NSDeviceRGBColorSpace BPS=8 BPP=32 Pixels=32x32 Alpha=YES Planar=NO Format=0,
NSBitmapImageRep 0x39d740 Size={16, 16} ColorSpace=NSDeviceRGBColorSpace BPS=8 BPP=32 Pixels=16x16 Alpha=YES Planar=NO Format=0
)
... which is only moderately interesting. What you'd probably want to do to display the icon is to use NSImage's -compositeToPoint:operation: to draw into a view. Alternately, you could just use an NSImageView, and use -setImage:.
To open a particular URL in Safari:
NSWorkspace * ws = [NSWorkspace sharedWorkspace];
NSURL * url = [NSURL URLWithString:@"http://theocacao.com/"];
[ws openURL: url];