Have you looked at OSAKit? It has methods for executing subroutines of scripts without having to do that "initWithSource" hack.

You can find the header files here: "/System/Library/Frameworks/ OSAKit.framework/Versions/A/Headers/"

A couple of caveats about using it though. Subroutine names have to be all lowercase when you call them and parameters you feed to the script MUST be enclosed in an NSArray, even if your script only takes one parameter. So, for example if the subroutine name in the script is "DoSomethineNifty" you have to call it as "dosomethingnifty"

Here's a sampling of what you'll find in the OSAKit->script header file (Note the "executeHandlerWithName" method)

// Construction
- (id)initWithSource:(NSString *)source;
- (id)initWithSource:(NSString *)source language:(OSALanguage *)language; - (id)initWithContentsOfURL:(NSURL *)url error:(NSDictionary **)errorInfo; - (id)initWithContentsOfURL:(NSURL *)url language:(OSALanguage *)language error:(NSDictionary **)errorInfo; - (id)initWithCompiledData:(NSData *)data error:(NSDictionary **)errorInfo;

// Accessors
- (NSString *)source;
- (NSURL *)url;
- (OSALanguage *)language;
- (void)setLanguage:(OSALanguage *)language;
- (BOOL)isCompiled;

// Operations
- (BOOL)compileAndReturnError:(NSDictionary **)errorInfo;
- (NSAppleEventDescriptor *)executeAndReturnError:(NSDictionary **)errorInfo; - (NSAppleEventDescriptor *)executeAppleEvent:(NSAppleEventDescriptor *)event error:(NSDictionary **)errorInfo; - (NSAppleEventDescriptor *)executeAndReturnDisplayValue: (NSAttributedString **)displayValue error:(NSDictionary **)errorInfo; - (NSAppleEventDescriptor *)executeHandlerWithName:(NSString *)name arguments:(NSArray *)arguments error:(NSDictionary **)errorInfo;
- (NSAttributedString *)richTextSource;
- (NSAttributedString *)richTextFromDescriptor:(NSAppleEventDescriptor *)descriptor; - (BOOL)writeToURL:(NSURL *)url ofType:(NSString *)type error: (NSDictionary **)errorInfo; - (BOOL)writeToURL:(NSURL *)url ofType:(NSString *)type usingStorageOptions:(OSAStorageOptions)storageOptions error: (NSDictionary **)errorInfo; - (NSData *)compiledDataForType:(NSString *)type usingStorageOptions: (OSAStorageOptions)storageOptions error:(NSDictionary **)errorInfo;

Also, Have you looked into AppleScript Studio? You can call built-in and custom Cocoa classes directly from your scripts which might be useful if you just need to use Cocoa classes in certain spots to speed things up.

For example:
To load an NSDictionary:
set dict to call method "dictionaryWithContentsOfFile:" of class "NSDictionary" with parameter "/path/to/a/plist/file"
set x to (|some key| of dict) as string -- as list -- as record
log x
log dict

To split a string at a delimiter
set testString to "Homer says Doh!"
set parts to call method "componentsSeparatedByString:" of testString with parameter " "
log parts


On Mar 13, 2009, at 10:23 AM, Karen van Eck wrote:

We have a lot of code in libraries in Applescript. Now starting to look at using Cocoa to move forward, as we are really using Applescript to pretty much its limit.

But it is not a short term solution to rewrite all our Applescript libraries in Objective-C. We need to be able to use Objective-C programs to load and execute our Applescripts.

I've managed to get a program to execute an Applescript and to execute a handler in an Applescript.

But most of our libraries are saved as Script Objects. Please could someone point me in the right direction for executing a subroutine inside a script object in an Applescript.

I'm guessing I could do something like:

NSApplescript *scriptObject = [[NSAppleScript alloc] initWithSource: @"set scriptObject to (scriptObjectName of (load script file scriptPath))
                                                                                     
                      tell scriptObject to doHandler()"];

and execute it, but that seems to be quite klunky. There must be a neater way to do it.

Any advice, hints and links will be gratefully received.

Thank you

Karen






_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/kentozier%40comcast.net

This email sent to kentoz...@comcast.net

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to