Re: Executing AppleScript from NSString?
Re: Executing AppleScript from NSString?
- Subject: Re: Executing AppleScript from NSString?
- From: Nathan Day <email@hidden>
- Date: Mon, 24 Dec 2001 16:38:21 +1030
This question should almost be a FAQ I,ve heard similar questions at
least twice on the other two mailing list I'm on. This is the extract
from one of my own projects, with some small modifications, which I
started before ASS, I haven't found a way to do this using ASS, it seems
they haven't made this stuff public yet. If somebody can correct me
please do.
+ (id)compileExecuteString:(NSString *) aString
{
OSAID theResultID;
AEDesc theResultDesc = { typeNull, NULL },
theScriptDesc = { typeNull, NULL };
id theResultObject = nil;
if( (AECreateDesc( typeChar, [aString cString], [aString
cStringLength], &theScriptDesc) == noErr) && (OSACompileExecute
( [AppleScriptObject OSAComponent], &theScriptDesc, kOSANullScript,
kOSAModeNull, &theResultID) == noErr ) )
{
if( OSACoerceToDesc( [AppleScriptObject OSAComponent],
theResultID, typeChar, kOSAModeNull, &theResultDesc ) == noErr )
{
if( theResultDesc.descriptorType == typeChar )
{
theResultObject = [NSString stringWithAEDesc:&theResultDesc];
AEDisposeDesc( &theResultDesc );
}
}
AEDisposeDesc( &theScriptDesc );
if( theResultID != kOSANullScript )
OSADispose( [AppleScriptObject OSAComponent], theResultID );
}
return theResultObject;
}
+ (ComponentInstance)OSAComponent
{
static ComponentInstance theComponent = NULL;
if( !theComponent )
{
theComponent = OpenDefaultComponent( kOSAComponentType,
kAppleScriptSubtype );
}
return theComponent;
}
@implementation NSString (AEDescCreation)
/*
* + stringWithAEDesc:
*/
+ (id)stringWithAEDesc:(const AEDesc *)aDesc
{
NSData * theTextData;
theTextData = [NSData dataWithAEDesc: aDesc];
return ( theTextData == nil ) ? nil : [[[NSString
alloc]initWith
Data:theTextData encoding:NSMacOSRomanStringEncoding]
autorelease];
}
@end
On Monday, December 24, 2001, at 07:19 AM, Matt Gemmell wrote:
Hi everyone,
Firstly, I feel I should apologise for coding on a Sunday evening two
days
before Christmas. Now that that's over with, here's a question:
I'm interested in being able to take an NSString and execute its
contents as
AppleScript; i.e. assume that the NSString contains valid AppleScript
code,
and running that AppleScript. Naturally, I'd like to do so without
having to
resort to Carbon stuff.
I'm not asking for code or a complete solution, just pointers as to
where I
should look in the Cocoa reference material to find out what I need to
know.
Thanks for any help or comments, and all the best for the holidays. :-)