Re: Save a NSAppleScript to a file
Re: Save a NSAppleScript to a file
- Subject: Re: Save a NSAppleScript to a file
- From: Mark Lilback <email@hidden>
- Date: Sun, 23 Nov 2003 19:22:48 -0800 (PST)
If your application requires Panther, you can use OSAStoreFile which
can save in any of the standard AppleScript file formats (including
as a bundle).
Otherwise, you've got to make a few OSA calls. Here is a category
that adds -writeToFile: to NSAppleScript, which saves the compiled script
(with the source code recoverable by OSA calls).
@implementation NSAppleScript(RTExtensions)
//writes out as a data-fork compiled script that should have the
// extension ".scpt"
-(BOOL)writeToFile:(NSString*)path
{
OSStatus err;
ComponentInstance defComponent, asComponent;
OSAID scriptId=kOSANullScript;
BOOL success = NO;
NSAppleEventDescriptor *compiledDesc =
[NSAppleEventDescriptor nullDescriptor];
NSAppleEventDescriptor *srcDesc =
[NSAppleEventDescriptor descriptorWithString: [self source]];
defComponent = OpenDefaultComponent(kOSAComponentType, 'scpt');
if (nil == defComponent)
return NO;
err = OSAGetScriptingComponent(defComponent, kAppleScriptSubtype,
&asComponent);
if (noErr != err) {
CloseComponent(defComponent);
return NO;
}
err = OSACompile(asComponent, (AEDesc*)[srcDesc aeDesc],
kOSAModeCompileIntoContext, &scriptId);
if (noErr == err) {
err = OSAStore(asComponent, scriptId, typeOSAGenericStorage,
kOSAModeNull, (AEDesc*)[compiledDesc aeDesc]);
if (noErr == err)
success = [[compiledDesc data] writeToFile: path
atomically: NO];
}
CloseComponent(defComponent);
return success;
}
@end
__________________________________________________________________________
"They that can give up essential liberty
Mark J. Lilback to obtain a little temporary safety
<email@hidden> deserve neither liberty or safety."
http://www.lilback.com/ -- Benjamin Franklin
_______________________________________________
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.