Re: Swiftautomation...
Re: Swiftautomation...
- Subject: Re: Swiftautomation...
- From: has <email@hidden>
- Date: Tue, 22 Nov 2016 22:55:48 +0000
Jean-Christophe Helary wrote:
So, I've cloned the thing, I've opened it in Xcode, I've hit Build and I'm not sure I'm getting anything besides for a "Build successful".
I'm*not* a Xcode user (or a programmer by any stretch of the imagination) so I'm not sure what to do next.
Ah yes, forgot folk might want to try before they buy.:) Being a doofus,
I had new and improved instructions written a week ago, but forgot to
check them in. Online copy here:
https://hhas.bitbucket.io/welcome.html
Select `Source Control -> Pull...` in Xcode to pull changes from
bitbucket to your local copy. If you've got an Alassian (bitbucket)
account, you can also add yourself as a Watcher to get email
notifications when updates are made. Obviously it's a working project so
don't be surprised when I push a change and break something; just shout
if you see anything wrong.
For playing around with simple 'scripts', I've added a 'Release' build
scheme that builds all the bits to get you started (select it from the
toolbar menu to right of Run and Stop buttons). Creating symlinks in
/Library/Frameworks that point to the built products seems to be the
least fragile way to pretend it's a properly installed framework (this
is just a workaround until Swift 4 makes it possible for pre-built
frameworks to be distributed and installed for real).
If you're not sure where Xcode puts the built products (it's a pain that
way), scroll down the left-side list to 'Products', right-click the
built Swift.Automation product (which should now be shown black instead
of red), and select Show in Finder. You can then just drag the items
into the Terminal window while typing the `ln` (symlinks) commands and
Terminal will insert the full file path automatically.
...
Personally I use TextWrangler to write and 'Run' scripts (it has a #!
menu so you don't need to use Terminal to test while writing). You don't
get the live syntax checking you do in Xcode, but once you know Swift
basics you can get by without it for simple 'scripts'. Just like writing
an old school shell script. :) And anything's nicer than Xcode for
'scripting'. [1]
I've provided prebuilt glues (the classes that let you write object
specifiers and commands using dictionary-defined names) for most of the
scriptable Apple apps in macOS. Any Apple apps you think I've missed,
let us know. (You can generate your own glues too, of course, but you'll
need to futz with the `#!/usr/bin/swift` line at the top of your Swift
'script' to make it import them. Easiest to play with the glues I've
provided for now.)
To view app dictionaries, scroll down the left-hand list in Xcode to the
'MacOSGlues' group. Select the .sdef file you want, right click, and
choose 'Open in External Editor' (which for .sdef files is Script Editor
by default). As a rough rule of thumb, multi-word keywords get the first
character after a space capitalized and then the space is removed.
...
Three main differences between AppleScript and Swift:
1. Swift is really pedantic about code that throws errors. As in AS, all
app commands throw errors on failure, so you must type `try` before them
or they won't compile. e.g.:
try TextEdit().activate()
2. No implicit `get`. e.g.:
TextEdit().documents.first.name
only gives you the reference (name of first document of app "TextEdit")
when run.
To actually get the value you need to add a `.get()` call to the end:
TextEdit().documents.first.name.get()
3. Swift is a statically typed language (super pedantic). Sometimes it
can infer types from context, e.g.:
if try textedit.documents.first.exists() {
// do stuff with frontmost TextEdit document
}
other times you must tell it before it will let you do _anything_ useful
with returned values. When in doubt, always add `as TYPE` after a
command, e.g.:
try finder.home.folders.name.get() as Array<String>
lets Swift (and SwiftAutomation) know that you want a list of strings
back. Or:
try itunes.currentTrack.get() as ITUItem
tells it you want a reference to the current track.
...
Like I say, code beta-ish but docs need good bit of work. If you can
avoid the disaster that is tutorial chapter, most of the rest if fairly
complete. Try the Examples chapter to get a flavor of AS vs Swift syntax:
https://hhas.bitbucket.io/examples.html
The AppleScriptToSwift app is also your absolute best friend when
figuring out how to write a reference (specifier) or command using Swift
syntax. Appscript users LOVED this.
Will try to do some more work on docs later this week. Super tired now.
Later.
HTH
has
[1] Hoping to nudge Mark Alldritt in direction of a 'Swift Debugger' if
he's looking for something to do. I daresay he has a lot to consider,
but expressing interest by throwing money at him couldn't hurt.;)
_______________________________________________
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