Re: Save a NSAppleScript to a file
Re: Save a NSAppleScript to a file
- Subject: Re: Save a NSAppleScript to a file
- From: p3consulting <email@hidden>
- Date: Mon, 24 Nov 2003 09:55:53 +0100
On 23 nov. 2003, at 22:46, Lorenzo wrote:
>
Pascal,
>
you are so kind, but the problem is that I am letting the users write
>
their
>
own short AppleScript in my applications, just to be executed at
>
launch and
>
before quit. So they could write whatever in the AS string code.
>
It's not something like a fixed string code. It could vary all the
>
time.
>
It could be "tell application" or "mount volume" or a very complex
>
script.
>
>
So I think the best is to compile the AS when the user, after he wrote
>
the
>
AS text, presses the button Apply, then if there are no errors I save
>
the
>
compiled script somehow, then when the user later launchs the app, my
>
app
>
executes the compiled AppleScript. The problem is that I cannot save
>
the
>
compiled AppleScript. I would need something like
>
>
*as = [[NSAppleScript alloc] initWithSource:asText];
>
ok = [*as compileAndReturnError:nil];
>
if(!ok) return;
>
NSData *asData = [NSData dataWithNSAppleScript:as];
>
[asData writeToFile:filePath atomically:NO];
>
>
But as you know the API dataWithNSAppleScript doesn't exist.
>
I think there should be a way to convert a NSAppleScript to a NSData
>
or to a
>
simple pointer, but I don't know how.
>
Read
http://developer.apple.com/documentation/Carbon/Reference/
Open_Scripti_Architecture/Open_Script_Arch/function_group_1.html#//
apple_ref/c/func/OSAStore
and
http://developer.apple.com/samplecode/Sample_Code/
Interapplication_Comm/EmbededAppleScripts.htm
http://developer.apple.com/samplecode/Sample_Code/
Interapplication_Comm/MoreAppleEvents.htm
There is also a CAppleScript class for PowerPlant,
http://cvs.sourceforge.net/viewcvs.py/maccvspro/maccvs25/Scripting/
CAppleScript.cp?rev=1.7
You should be able to do what you want using these Carbon calls:
AECreateDesc, OSACompile, OSAStore, OSALoad, OSAExecute, GetHandleSize
wrting:
from text to AEDesc: AECreateDesc (typeChar)
from AEDesc to scriptID: OSACompile
from scriptID to AEDesc: OSAStore
from AEDesc to NSData: GetHandleSize, NSData dataWithBytes
reading:
from NSData to AEDesc: AECreateDesc (typeOSAGenericStorage)
from AEDesc to scriptID: OSALoad
to execute from scriptID
OSAExecute
Or
NSAppleScript has some private methods:
@interface NSAppleScript(NSPrivate)
+ (struct ComponentInstanceRecord *)_defaultScriptingComponent;
- (unsigned long)_compiledScriptID;
@end
you can use at your own risks the private method of NSAppleScript to
get the scriptID after compiling it
then you use OSAStore to get a "binary" AEDesc you can write to disk
by making a NSData out of it ( GetHandleSize on the dataHandle field of
AEDesc)
To get the source back: use OSADisplay (or OSAGetSource read the doc
for the diffs)
Pascal Pochet
P3 Consulting
email@hidden
http://www.p3-consulting.net
_______________________________________________
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.