Xcode Swift playgrounds 1: freezing and failing

Having spent most of a day with Scriptarian, it was time to take a more serious look at what Xcode’s Swift playgrounds can offer. I’m running Xcode 8.1 on macOS Sierra 10.12.1, and using Swift 3.0.1, so everything is up to date.

I have previously been starting to explore the Swift language using playgrounds, but now am I more interested in features which are important in scripting, such as basic user interaction. So my attention was focussed on standard dialogs to open files, present alerts, and so on – simple tasks in AppleScript, and supported well in Scriptarian’s library too.

To help me explore playgrounds better, I have bought a copy of Erica Sadun’s Playground Secrets and Power Tips from Apple’s iBooks store (and added it to the list of resources here). It’s the only book which really gets to grips with the features of playgrounds, and shares many secrets that will save you a lot of time and effort.

However, it and everywhere else that I have looked suffer from a major problem: Apple has altered Xcode and Swift so much and so quickly that it has left even the most conscientious of bloggers behind. Erica has updated her book to try to keep pace, but almost everything else that you will come across will be out of date. And I have a sneaky feeling that this article will be out of date in a few weeks too. Progress is great, but in the absence of documentation it can become counterproductive.

A good example of this is the way in which playground source is automatically run as you code along (much as in Scriptarian). In Xcode 8.1 you can now turn this off, which is often better. My first recommendation to anyone exploring Swift scripting – at least, if they are as clumsy as I have been – is to turn this feature off when working with Cocoa and other macOS calls. It will save a lot of distraction.

Much of the rest of my day in the playground has been rather frustrating. The most that I seem to have achieved is a completely blank window, and consistently getting Xcode to hang with a spinning beachball cursor.

As a result of the frenetic pace of change, Xcode’s documentation of playgrounds is abysmal, its Using Swift with Cocoa and Objective-C is of little use when trying to code, and much of the code posted to the internet is out of date. Given the frequency of their occurrence in macOS apps, you would have thought that example Swift 3 code to display the Open File dialog, and to display a simple alert, would be commonplace. But it isn’t, not unless Google had it in for me with my searches.

In the end, I tried variations along the lines of:

import Cocoa

let openPanel: NSOpenPanel = NSOpenPanel()
openPanel.title = "Choose a file"
openPanel.allowsMultipleSelection = false
openPanel.canChooseDirectories = false
openPanel.canChooseFiles = true
let i = openPanel.runModal()

for the Open File dialog, which draws a blank dialog window with only a maximise button, and stops.

xcodepg02

When I tried the following code to display a modal alert from my playground, Xcode consistently froze with a spinning beachball:

let alert = NSAlert()
alert.messageText = "Warning"
alert.addButton(withTitle: "OK")
alert.addButton(withTitle: "Cancel")
alert.informativeText = "This is a test"
let i = alert.runModal()

In a bid to be more ingenious, I went for another piece of stolen code:

func showErrorInKeyWindow(message: String) {
if let window = NSApp.keyWindow {
let alert = NSAlert()
alert.messageText = "Error"
alert.informativeText = message
alert.addButton(withTitle: "Dismiss")
alert.beginSheetModal(for: window, completionHandler: nil)
}
}
showErrorInKeyWindow(message: "This is a test")

which completes, but does nothing.

xcodepg01

Xcode’s playgrounds are really lovely, though. Their code completion feature is very impressive, and there are lots of bells and whistles too. But when they don’t work, you’re left without any documentation, and error messages which are unhelpful unless you really know what you’re doing – in which case, you presumably already know what to do.

So my conclusion after the first day with Xcode’s Swift playgrounds is that, unless you’re already a competent Swift developer, they’re not yet ready for use in scripting macOS. Scriptarian may have a major weakness in its AE Bridge which severely limits its ability to control other apps, but at least it is usable for the tasks that it can do.