Re: NSAppleScript question
Re: NSAppleScript question
- Subject: Re: NSAppleScript question
- From: Nir Soffer <email@hidden>
- Date: Tue, 5 Feb 2008 01:23:17 +0200
On Feb 2, 2008, at 09:12, Matthew Delves wrote:
NSDictionary *loadError = [[NSDictionary alloc] init];
You leak a dictionary here. Correct:
NSDictionary *loadError = nil;
NSDictionary *error = [[NSDictionary alloc] init];
Same issue, same correction.
NSAppleEventDescriptor *returnDescriptor = nil;
NSString *scriptLocation = [[NSString alloc] initWithFormat:@"%@/%
@", [[NSBundle mainBundle] resourcePath],
@"RSITunesLaunchApplescript.scpt"];
There is a method to find path of resources:
NSString *scriptPath = [[NSBundle mainBundle]
pathForResource:@"RSITunesLaunchApplescript" orType:@"scpt"];
Note that I renamed the variable to scriptPath. There is no such
thing as location in Cocoa, but there are many methods that use "path".
Now check that path:
if (sciptPath == nil)
// There is no point to continue. Fail as soon as you can.
NSURL *fileURL = [[NSURL alloc] initWithString:scriptLocation];
Use fileURLWithPath:
NSFileManager *manager = [NSFileManager defaultManager];
You don't need the file manager.
NSAppleScript *scriptObject = nil;
Aren't all those objects objects? What's wrong with "script"?
// Create the script here
You don't need that check, pathForResources already detected the file.
if([manager fileExistsAtPath:scriptLocation]) {
scriptObject = [[NSAppleScript alloc]
initWithContentsOfURL:fileURL error:&loadError];
// Both scriptObject and loadError are null for some unknown reason
if(scriptObject == nil) {
NSLog(@"The error is: %@", [loadError
valueForKey:NSAppleScriptErrorMessage]);
Use objectForKey: to access a dictionary values.
This is a good place to bail out from this method. There is no point
in continuing and checking again if the script is nil.
}
}
NSLog(@"%@", scriptObject);
// Run the script
NSLog(@"executing script");
if(scriptObject != nil)
returnDescriptor = [scriptObject executeAndReturnError: &error];
if ([returnDescriptor descriptorType]) {
The correct test for success is:
if (returnDescriptor != nil)
NSLog(@"script executed sucessfully.");
} else {
NSLog(@"Script execution has gone a bit pear shaped: %@",
[error objectForKey: @"NSAppleScriptErrorMessage"]);
}
What's wrong with pear shape?
Best Regards,
Nir Soffer
_______________________________________________
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