Re: Swiftautomation... [how to set it up]
Re: Swiftautomation... [how to set it up]
- Subject: Re: Swiftautomation... [how to set it up]
- From: has <email@hidden>
- Date: Wed, 23 Nov 2016 18:11:50 +0000
Olof Hellman wrote:
> Trying out SwiftAutomation for the first time:
>
> 1) Minor hurdle: In order to get anything to build, it seems one
has to change the “Development Team” build setting to one’s own team for
each of the products.
Ugh. Xcode. Someone else want to research and tell me the answer? I'm
stacked.
> 2) slightly larger hurdle: In order not to get an error on the lines
>
> import SwiftAutomation
> import MacOSGlues
>
> It seems like one needs to do something magic to have the swift
complier find the frameworks. Merely copying them to
/Library/Frameworks doesn’t seem to be sufficient. I can make an Xcode
project and set the frameworks search path (which works fine) but for
just a #! script launched from TextWrangler or BBEdit theres another
missing step.
OK. I will add a separate "How To Set Up" file/chapter that everyone can
follow.
I added instructions to the HTML docs on the last checking but they're
not obvious enough.)
[For now, you can build a Swift 3 framework, but it can only be imported
by Swift code that is built with *exactly* the same compiler version.
That's why the Swift docs currently tell you to create one big Xcode
Workspace with your app's Project and the projects for all of the
frameworks it uses, so that when you build your app, all the frameworks
get built at the same time and embedded in the .app bundle. Fine for
standalone apps (I suspect that's what Scriptarian does, which is why it
takes time to startup each time you click Run), useless for 'scripting'
- which is what we're interested in.
Here's a first draft (I'll create screenshots once the text is okay):
Swift does not yet allow you to build and distribute standalone
frameworks (as ObjC can). Hopefully Swift 4 will add this ability this
next year. Therefore, before automating your macOS apps with Swift, you
need to do a bit of manual set up before using SwiftAutomation for
'scripting' in Swift:
1. Run the following command in Terminal to get the SwiftAE project:
git clone https://bitbucket.org/hhas/swiftae.git
2. Open the in SwiftAE project in Xcode. To the right of the "Play" and
"Stop" buttons in the toolbar, there is a "scheme" menu that lets you
select how you want the project to be built. Select the "Release" menu
option, then select "Product > Build" in the menubar.
3. All going well, Xcode will build 4 products:
"SwiftAutomation.framework", "aeglue" (which gets embedded in the
framework), "MacOSGlues.framework", "AppleScriptToSwift.app". Scroll
down to the "Products" group at the left-hand side of the Xcode project
window, and you should see these 4 products' names in black and one
other ("test") in red. (Ignore the "test" product; that's just me doing
ad-hoc testing.)
4. Just to be extra annoying, Xcode puts these built products into a
"Release" folder buried at a random location deep down in ~/Library. (It
expects you to use "Archive" when you want to distribute files, but
there's no point doing that for Swift-based frameworks as Swift 3
doesn't support it.) The easiest way to locate this "Release" folder is
to right-click on "SwiftAutomation.framework" in the "Products" group
and select "Show in Finder", so do that.
5. Once you've opened the "Release" folder in Finder, it is best not to
copy the built Frameworks directly. It works fine at first, but because
of the aforementioned Swift 3 issues you will soon start getting tons of
errors about things being built at different times. This _will_ drive
you nutty. Therefore, the best approach I've found to-date is to leave
the built product where they are in the Release folder, and instead make
symlinks to them in /Library/Frameworks.
(I would include a shell script that did it automatically, but then
Xcode needs an admin password to create those symlinks in /Library. Have
I said how much I like the idea of a self-contained "Swift scripting"
micro-editor?)
So open Terminal and type the following:
cd /Library/Frameworks
6. Still in Terminal, type the following (include a space at the end):
sudo ln -s
and then drag the SwiftAutomation.framework from the Release folder onto
the Terminal window. Terminal will add the full path to the framework to
the end of the command for you (helpful)! Hit return, enter an admin
login when prompted, and a new link to the framework will be created in
/Library/Frameworks.
7. Repeat the process for the MacOSGlues.framework.
8. Create a regular Finder alias to the AppleScriptToSwift.app on your
desktop. (Unlike the frameworks, the app is totally self-contained so
you can do what you like with it, but this is simplest while the
project's still being developed.)
...
OK, you should now be good to go. In a plain text editor/code editor of
your choice (I suggest BBEdit/TextWrangler as that's what I use and know
it works), type the following Swift 'script':
#!/usr/bin/swift -target x86_64-apple-macosx10.12 -F /Library/Frameworks
import SwiftAutomation
import MacOSGlues
print(try Finder().home.name.get()) // output your home folder's name
The '#!...' line at the top's standard in all shell-type scripting; I'm
sure you've seen it before. You'll notice the extra options:
* The -target option is needed Swift will compile for
10.10-compatibility by default, and SwiftAutomation requires 10.11 or
later. (If for some reason you're still running Mac OS X 10.11, just
change the "10.12" to "10.11".)
* The -F option tells Swift to look for frameworks in
/Library/Frameworks as well as the default /System/Library/...
locations. Since we've symlinked our built Xcode frameworks to that
location, Swift should automatically find them from there.
If you're using BBEdit/TextWrangler, select '#! -> Run' in the menubar
to run the 'script' directly and see the result. (I like this way.)
Otherwise, to run the script in Terminal, save it as a plain text file
in your home folder; let's call it "automate.swift". To make this script
executable, open a new Terminal window and run:
chmod +x automate.swift
To run the script, type:
./automate.swift
All going well, the script should print out the name of your home folder
in a fraction of a second. Yay!
All going not well, Swift should print out an error message. Send us
screenshot/message/whatever so I can troubleshoot, and I'll figure out
what went wrong and update accordingly.
...
OK, please let me know if that works for you. Any problems, just shout.
If it's good to go I'll pretty it up and include in the next checkin.
Can I also ask please: When you downloaded the project, did you read the
README or any of the other doc files in the project, and/or read any of
the website online, before you clicked Xcode's "Run" button? Just so I
know how prominent these instructions need to be. (I hate that damn
"Run" button - it's very misleading. It really means "Build and Run" or
"Build Only", depending on whether the project is an app or a framework.)
HTH and many thanks,
has
_______________________________________________
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