integrating applescript in cocoa

Go To StackoverFlow.com

0

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

2009-06-16 18:36
by NoName
FYI: Adium had problems with NSAppleScript leaking memory. You might try OSAScript from the OSAKit framework. The framework comes with Mac OS X, and the API is almost exactly the same - Peter Hosey 2009-06-17 01:01


5

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!
}
2009-06-16 18:56
by NoName


1

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.

2009-06-16 18:47
by Naaff
-[NSAppleScript executeAndReturnError:] is documented to return nil if an error occurs - NoName 2009-06-16 18:57
Good point... that's what I get for not thinking. ;) Your answer is more like what I should have said, so +1 - Naaff 2009-06-16 19:06


1

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';
2009-06-16 19:01
by Brian Ramsay


0

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!

2009-06-16 19:08
by Jorge Israel Peña


0

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.

2018-04-16 11:44
by user9652975
Ads