Re: Swift + AppleScript
Re: Swift + AppleScript
- Subject: Re: Swift + AppleScript
- From: has <email@hidden>
- Date: Tue, 04 Oct 2016 21:16:35 +0100
On Oct 3, 2016, at 11:30 PM, Shane Stanley <email@hidden>
wrote:
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?
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.
As Shane says, the best way to call into AppleScript for anything
non-trivial is via AppleScriptObjC, though it's a bit more complicated
doing it in Swift than ObjC. Here's a slightly outdated HowTo that may
provide a starting point:
http://macscripter.net/viewtopic.php?id=43127
and you'll want to read up on Swift 3, which supports NSClassFromName():
https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/WritingSwiftClassesWithObjective-CBehavior.html
I imagine you can fuse together a working solution out of that.
Your other option[1] would be the SwiftAutomation framework I'm
currently working on <https://bitbucket.org/hhas/swiftae>, which
provides production-quality application scripting support that's >99.9%
as good as AppleScript's own, though with a couple of caveats:
1. It's not _quite_ finished - I'm still sorting out error reporting,
and the documentation's a dogawful mess - and it hasn't had a full
shakedown yet so there may still be some bugs and compatibility issues
to knock out. Plus I've just had work land today so I'm not sure how
much I'll be doing on it this month (though this is maybe as well since
Swift's being a giant PITA that's driving me crazy with its bugs and
horrible REPL support).
2. My future support of it is _absolutely_ dependent on how much backing
it gets from Apple/Swift devs, because I've been down this road before
<http://appscript.sourceforge.net/status.html>. I'm not going to piss
away yet another thousand hours of my own time on useless windmill
tilting; never mind piss off another thousand users who've invested
thousands more hours of their own time into it when I eventually pull
the plug.
So anyone who does choose to use it is 1. going to be helping iron out
teething issues over the next few months, and 2. going to need to jump
up and down on the aforementioned Apple/Swift devs to get them to
support it too. Because the best - and only guaranteed - way for
SwiftAutomation to provide Swift users with a true, trustworthy
alternative to AppleScript is for Apple to adopt it themselves and
include it the OS.[2]
Still worth grabbing a copy and building the AppleScriptToSwift.app
target for a play about, if nothing else: seeing your AppleScript
commands automagically transform to Swift syntax is pretty damn awesome
if I do say so myself.:)
HTH
has
[1] Don't even bother with Apple's own ScriptingBridge framework: it's a
broken piece of crap in ObjC, and sucks even worse in anything else.
[2] As they nearly did for appscript, had I not been asleep at the
wheel. (Protip: nepotism, not technical merit, is what really counts.)
_______________________________________________
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