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
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!
}
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.
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';
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!
sorry it's late to answer. Applescript in a cocoa application can be used easily with some basic principles, first set all descriptors to 'NULL', use 'NSAppleEventDescriptor' for proper script execution, and use a return value of executionn which will give your script:
NSString * scriptString = NULL, NSString * retvalue = NULL; NSAppleEventDescriptor * descriptor = NULL; NSDictionary * errInfo = nil; NSAppleScript * mailScript = NULL; scriptString = [NSString stringWithFormat: @ "the applescript"]; mailScript = [[NSAppleScript alloc] initWithSource: scriptString]; descriptor = [mailScript executeAndReturnError: & errInfo]; retvalue = [descriptor stringValue]; [mailScript release];
For "the applescript" you did not write what you want to achieve I guess it's for privacy.