Re: Calling AppleScript from a Cocoa app
Re: Calling AppleScript from a Cocoa app
- Subject: Re: Calling AppleScript from a Cocoa app
- From: Phil <email@hidden>
- Date: Tue, 6 Sep 2005 12:14:47 -0400
Matt,
I just posted more info (including the AS) and here's a step-by-step
to reproduce the code and error:
1) Download the sample project at http://www.scifihifi.com/cocoa/
ASHandlerTest.dmg (should Buzz be reading - thanks for the code and
it was working fine until I started messing with it ;-)
2) You'll need OmniGraffle (Pro) (you probably know this but for
completeness: its commercial s/w with a trial lic. avail. Download
at http://www.omnigroup.com/download/latest/OmniGrafflePro.dmg)
3) Per my previous message, make the following changes to the project
(summary of changes: add 2nd function to .scpt (a) and make 2 changes
to AppController.m (b and c))
(a) Modify the script.scpt to include the OGNewObj function:
on show_message(theMessage)
tell application "iTunes"
display dialog theMessage
end tell
return 0
end show_message
on OGNewObj(objName)
tell application "OmniGraffle Professional 3"
activate
set myDocument to front document
tell first page of myDocument
set theShape to make new shape at front of graphics with
properties {origin:{20, 20}, size:{100, 60}, name:objName, text:objName}
end tell
end tell
return 0
end OGNewObj
(b) Modify AppController.m to add a call to the second function by
inserting a definition for the new function at line 23:
#define kHandlerName2 (@"OGNewObj")
(c) Finally, insert the code to call it in AppController.m at line 49
(which is simply a copied block from the example with new variable
names):
// Begin added code
NSDictionary *errorInfo2 = nil;
errorInfo2 = nil;
/* Call the handler using the method in our special category */
NSAppleEventDescriptor *result2 = [script callHandler:
kHandlerName2 withArguments: arguments errorInfo: &errorInfo2];
int scriptResult2 = [result2 int32Value];
/* Check for errors in running the handler */
if (errorInfo2) {
[self handleScriptError: errorInfo2];
}
/* Check the handler's return value */
else if (scriptResult2 != noScriptErr) {
NSRunAlertPanel(NSLocalizedString(@"Script Failure", @"Title
on script failure window."), [NSString stringWithFormat: @"%@ %d",
NSLocalizedString(@"The script failed:", @"Message on script failure
window."), scriptResult2], NSLocalizedString(@"OK", @""), nil, nil);
}
// End added code
At this point, you should be able reproduce the -1708 error.
Thanks,
Phil
On Sep 6, 2005, at 11:27 AM, Matt Neuburg wrote:
On Tue, 6 Sep 2005 03:29:07 -0400, Phil <email@hidden> said:
Does anyone have suggestions / pointers re: calling AppleScript from
Cocoa apps? This is an area that Apple docs don't seem to cover in a
way that I am finding helpful.
Specifically, I need to call AppleScript code (both with and without
parameters) from a Cocoa app preferably by calling functions stored
in a central script. In playing around with some code found on the
web, it appears to be the core of what is needed: no problem getting
a sample function to display a dialog with a passed parameter.
However, when the same code is called to pass a parameter to a
different function in the same script, it fails with a -1708 error
which seems to mean 'the AppleEvent was not handled by any
handler'... which I find clear as mud since the function executes
correctly via Script Editor.
Could you be specific? Show the AppleScript code you'd like to call
from
Obj-C. m.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide
<http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden