views:

570

answers:

4

I have made a short applescript that sends an email with attachment. Now I want to integrate this script in my cocoa application. I have tried the following code that i found on the internet:

NSAppleScript *mailScript;
NSString *scriptString= [NSString stringWithFormat:@"the applescript"];
mailScript = [[NSAppleScript alloc] initWithSource:scriptString];
[mailScript executeAndReturnError:nil];
[mailScript release];

This code doesn't work however. I am a complete newbie at cocoa and could use some help.

UPDATE: The email is created. The applescript seems to stop when the attachment is added though.The applescript works perfectly when runned in scripteditor. Any clue?

Thanks

+1  A: 

Your code looks okay. It's likely that there is an error in your AppleScript.

Try the following:

NSAppleScript *mailScript;
NSAppleEventDescriptor *resultDescriptor;
NSString *scriptString= [NSString stringWithFormat:@"the applescript"];
mailScript = [[NSAppleScript alloc] initWithSource:scriptString];
resultDescriptor = [mailScript executeAndReturnError:nil];
NSLog([resultDescriptor stringValue]);
[mailScript release];

NSLog will output a string describing any errors to the console. This should help you find any problems.

Naaff
-[NSAppleScript executeAndReturnError:] is documented to return nil if an error occurs.
Graham Lee
Good point... that's what I get for not thinking. ;) Your answer is more like what I *should* have said, so +1.
Naaff
+2  A: 

So when you don't ignore the error from -[NSAppleScript executeAndReturnError:], what is the error? Do its contents tell you anything about what went wrong?

NSDictionary *dict = nil;
if ([mailScript executeAndReturnError: &dict] == nil)
{
  //ooh, it went wrong, look at dict
}
else
{
  // well, that worked!
}
Graham Lee
+1  A: 

If it takes time to get to the right place in your application and you just want to test the Applescript, you can run it from the terminal via the osascript command, and see the results:

osascript -e 'applescript here';
Brian Ramsay
A: 

It seems like SBApplication should work, but I haven't used it before.

According to @cocoadevcentral:

SBApplication: use to make cross-application scripting calls with Objective-C instead of AppleScript. Ex: get current iTunes track.

Here is is the excerpt from the documentation:

The SBApplication class provides a mechanism enabling an Objective-C program to send Apple events to a scriptable application and receive Apple events in response. It thereby makes it possible for that program to control the application and exchange data with it. Scripting Bridge works by bridging data types between Apple event descriptors and Cocoa objects.

Although SBApplication includes methods that manually send and process Apple events, you should never have to call these methods directly. Instead, subclasses of SBApplication implement application-specific methods that handle the sending of Apple events automatically.

For example, if you wanted to get the current iTunes track, you can simply use the currentTrack method of the dynamically defined subclass for the iTunes application—which handles the details of sending the Apple event for you—rather than figuring out the more complicated, low-level alternative:

[iTunes propertyWithCode:'pTrk'];

If you do need to send Apple events manually, consider using the NSAppleEventDescriptor class.

Hope that helps!

Jorge Israel Peña