• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSAppleScript question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >NSAppleScript question (From: Matthew Delves <email@hidden>)

  • Prev by Date: Re: need to not shift row in outline view
  • Next by Date: Re: NSTextView/text system slowdown on Leopard
  • Previous by thread: Re: NSAppleScript question
  • Next by thread: setAutoresizingMask from code
  • Index(es):
    • Date
    • Thread