Re: Swift + AppleScript
Re: Swift + AppleScript
- Subject: Re: Swift + AppleScript
- From: Shane Stanley <email@hidden>
- Date: Tue, 04 Oct 2016 14:30:11 +1100
On 4 Oct. 2016, at 1:59 pm, Neil Faiman <email@hidden> wrote:
>
> Is there a straightforward way to put together an app containing both Swift (or ObjC) and AppleScript code, where the main program is Swift, but it calls into AppleScript to do particular functions?
>
> I’m more comfortable programming the main UI and model logic of an app in Swift, but I need to have AppleScript available because some key functionality is provided by BBedit, Word, Mail, and Address Book. I suppose I could exec a shell osascript command, but that’s just silly.
If it's only a small amount of code you can use NSAppleScript, although that still leaves you the job of unpacking the descriptor results, which can be tedious depending on what you are doing.
The other way is to use AppleScriptObjC. You put your scripts in an AppleScript .scpt file in your app's /Contents/Resources, import the AppleScriptObjC framework, and call [[NSBundle mainBundle] loadAppleScriptObjectiveCScripts] early on. Your scripts should be of the form of a script object with a Cocoa class as parent and the handlers in a form compatible with Objective-C conventions:
script SomeClassName
property parent: class "NSObject"
on doStuff()
--
end doStuff
on doStuffWith:x
set x to x as integer
return x * 2
end doStuffWith:
end script
You then include the handlers in an @nterface file for the "class", and instantiate your instance:
self.helperASObject = [[NSClassFromString(@"SomeClassName") alloc] init];
and essentially treat it like a Cocoa class:
NSUInteger val = [self.helperASObject doStuffWith:@4];
The scripting bridge will then convert the common classes (NSString, NSNumbers, NSArray, NSDictionary, etc).
--
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden