Re: running an external app
Re: running an external app
- Subject: Re: running an external app
- From: Michael Vannorsdel <email@hidden>
- Date: Mon, 26 May 2008 23:22:16 -0600
I'll try and give one last stab at helping.
So you're reading the NSWorkspace reference page and you see a method:
- (BOOL)launchApplication:(NSString *)appName
The beginning '-' tells us this method is an instance method; the
receiver is an instance (object) of the class. Which class? Well the
one's who's doc we're reading, NSWorkspace. If the method started
with a '+' it tells us the method is a class method; the receiver is
the class itself. Which class? Well the one's who's doc we're
reading, NSWorkspace.
So the next question is, where do we get an instance? Do we have one
right now? No. How do we get one? Check out the NSWorkspace
reference some more:
"There is one shared NSWorkspace object per application. You use the
class method sharedWorkspace to access it.
------------------------------
sharedWorkspace
Returns the shared NSWorkspace instance.
+ (NSWorkspace *)sharedWorkspace
Return Value
The NSWorkspace object associated with the process.
------------------------------"
sharedWorkspace starts with a '+', the receiver is a class. Which
class? Well the one's who's doc we're reading, NSWorkspace.
NSWorkspace * theInstance = [NSWorkspace sharedWorkspace];
Now we have an instance of NSWorkspace to use with those '-' instance
methods. So lets use it for what we want:
- (BOOL)launchApplication:(NSString *)appName
The (BOOL) tells us this method will return a BOOL value. What does
this BOOL tell us? Docs says:
"Return Value
YES if the application was successfully launched or was already
running; otherwise, NO."
So it tells us if we were successful. The argument for the method is
an NSString*. What NSString object do we need? Docs says:
"Parameters
appName
The name of the application to open."
How do we make an NSString with the application name? Fundamental
docs says we can make static NSStrings using the form @"string".
Knowing all this we can finally do:
NSWorkspace * theInstance = [NSWorkspace sharedWorkspace];
BOOL didWeSucceed = [theInstance launchApplication:@"TextEdit"];
if(didWeSucceed == NO)
NSLog(@"Failed to open TextEdit");
How did I know all this magic? I read and learned Objective-C first
so that I know what methods look like and how they work. That's a
crucial step before jumping right into using the frameworks with any
chance of success.
On May 26, 2008, at 10:33 PM, Johnny Lundy wrote:
Objective-C is different from the simple function you give as an
example.
There is a receiver. Yet the receiver is never listed in any of the
method descriptions.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden