Re: AppleScripts from Cocoa apps
Re: AppleScripts from Cocoa apps
- Subject: Re: AppleScripts from Cocoa apps
- From: Adam Atlas <email@hidden>
- Date: Mon, 18 Mar 2002 08:49:45 -0500
I found these methods somewhere:
You'd declare void runScript(NSString *txt) (These are functions, not
ObjC methods) in a header file, and include/import it in whatever source
files you need AppleScript. I'm not sure if it lets you get the result,
though.
Example:
runScript(@"tell application \"Finder\"\n quit\n activate\n end tell");
This is
tell application "Finder"
quit
activate
end tell
with proper escape characters.
I hope this helps. (Note, you don't need to deal with the function
aedesc_to_id(AEDesc *desc). It is mainly for use by the runScript
function.)
----
static id aedesc_to_id(AEDesc *desc)
{
OSErr ok;
if (desc->descriptorType == typeChar)
{
NSMutableData *outBytes;
NSString *txt;
outBytes = [[NSMutableData alloc]
initWithLength:AEGetDescDataSize(desc)];
ok = AEGetDescData(desc, [outBytes mutableBytes], [outBytes
length]);
CHECK;
txt = [[NSString alloc] initWith
Data:outBytes encoding:[NSString
defaultCStringEncoding]];
[outBytes release];
[txt autorelease];
return txt;
}
if (desc->descriptorType == typeSInt16)
{
SInt16 buf;
AEGetDescData(desc, &buf, sizeof(buf));
return [NSNumber numberWithShort:buf];
}
return [NSString stringWithFormat:@"[unconverted AEDesc,
type=\"%c%c%c%c\"]", ((char *)&(desc->descriptorType))[0], ((char
*)&(desc->descriptorType))[1], ((char *)&(desc->descriptorType))[2],
((char *)&(desc->descriptorType))[3]];
}
void runScript(NSString *txt)
{
NSData *scriptChars = [txt dataUsingEncoding:[NSString
defaultCStringEncoding]];
AEDesc source, resultText;
ComponentInstance myComponent =
OpenDefaultComponent(kOSAComponentType,
kOSAGenericScriptingComponentSubtype);
OSAID scriptId, resultId;
//OSAID execContext;
OSErr ok;
//AEDesc contextName;
//[self setResultText:@""];
// Convert the source string into an AEDesc of string type.
ok = AECreateDesc(typeChar, [scriptChars bytes], [scriptChars
length], &source);
CHECK;
// Compile the source into a script.
scriptId = kOSANullScript;
ok = OSACompile(myComponent, &source, kOSAModeNull, &scriptId);
AEDisposeDesc(&source);
CHECK;
// ok = AECreateDesc(typeNull, "", 0, &contextName);
// CHECK;
// ok = OSAMakeContext(myComponent, &contextName, kOSANullScript,
&execContext);
// CHECK;
// Execute the script, using defaults for everything.
resultId = 0;
ok = OSAExecute(myComponent, scriptId, kOSANullScript, kOSAModeNull,
&resultId);
CHECK;
//NSLog(@" --> id = x", resultId);
if (ok == errOSAScriptError) {
AEDesc ernum, erstr;
id ernumobj, erstrobj;
// Extract the error number and error message from our scripting
component.
ok = OSAScriptError(myComponent, kOSAErrorNumber,
typeShortInteger, &ernum);
CHECK;
ok = OSAScriptError(myComponent, kOSAErrorMessage, typeChar,
&erstr);
CHECK;
// Convert them to ObjC types.
ernumobj = aedesc_to_id(&ernum);
AEDisposeDesc(&ernum);
erstrobj = aedesc_to_id(&erstr);
AEDisposeDesc(&erstr);
txt = [NSString stringWithFormat:@"Error, number=%@,
message=%@", ernumobj, erstrobj];
} else {
// If no error, extract the result, and convert it to a string
for display
if (resultId != 0) { // apple doesn't mention that this can be 0?
ok = OSADisplay(myComponent, resultId, typeChar,
kOSAModeNull, &resultText);
CHECK;
//NSLog(@"result thingy type = \"%c%c%c%c\"", ((char
*)&(resultText.descriptorType))[0], ((char
*)&(resultText.descriptorType))[1], ((char
*)&(resultText.descriptorType))[2], ((char
*)&(resultText.descriptorType))[3]);
txt = aedesc_to_id(&resultText);
AEDisposeDesc(&resultText);
} else {
txt = @"[no value returned]";
}
OSADispose(myComponent, resultId);
}
// ok = OSADispose(myComponent, execContext);
ok = OSADispose(myComponent, scriptId);
CHECK;
}
----
On Monday, March 18, 2002, at 08:12 AM, cocoa-dev-
email@hidden wrote:
I have two questions:
1) How can I call an AppleScript from within a Cocoa app and get the
result?
2) Is there any way I can compile an AppleScript from a string, to make
droplets, for example?
I have searched the archives, but have been unable to find any specific
answers to these questions.
Thanks,
Jonathon Mah
email@hidden
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.