One of the big advantages of Swift is that it gives you access to all Cocoa APIs and lets you use them in some very flexible ways.

One of those is the possibility to use Swift as a general OS “scripting” language – instead of bash, PyObjC or C or any other option that you might have opted for in the past. Moreover, you can do that entirely from outside of XCode – so write your Swift program in any editor and then simply use Terminal to execute it, as if it was pure script.

The obvious advantage of such approach is that you now have the same single language to handle iOS programming, OS X app programming and generic system/automation tasks that you might want to perform from the command line.

Let’s have a look.

Swift REPL

Swift ships with the REPL (Read-Evaluate-Print Loop) which lets you execute blocks of code in an interactive way, within the context of a single session. This concept, already available in a number of other languages, is a terrific experimental playground, as well as a great addition to the debugging experience. You can launch a new REPL session from the Terminal, or break into the REPL from a stopped application.

There are already some terrific resources on the web that show you how to set up your Terminal to be able to run Swift REPL. In short, to do that, you need to set you active developer directory to point to XCode 6 installation.

With this in place, you can now start Swift REPL directly from the Terminal using the following:

Shell

1

xccrun swift

This should give you the following screen.

Alternatively, Rob suggests to alias your XCode version as swift to avoid swicthing between profiles. You may choose to do that if that feels more comfortable to you.

Executing Code With Swift REPL

Swift’s command line compiler not only allows you to use the REPL, but also allows you to execute arbitrary snippets of Swift code, without any previous packaging into Mach-O executable or any upfront compilation (well the compilation does happen, but it’s happening together with execution in that case).
This is of great significance, because it could effectively eliminate any need (or at least some of the need) for bash scripting or for anything that you’d want to achieve with the XCode Command Line Tool project template – as the exact same thing could be done with much less ceremony, through a bit Swift.

Swift, when run from command line, has a number of options shown below:

-Xlinker<value>Specifies an option which should be passed tothe linker

-Xllvm<arg>Pass<arg>toLLVM.

What’s interesting for us in this case, is the -i switch – immediate mode. This lets us pass a path to a Swift file and it will be compiled and executed straight away, as if it was a bash script.

To give you an example, consider the following Swift code, which is setting a wallpaper on your Mac. The code is very simple – an NSURL pointing to a JPG file on my disk, which is then used with the setDesktopImageURL and the current screen to give my Mac a new desktop background image. If the operation succeeds, a message is printed.

To run this from the Terminal you also have to specify the relevant SDK, in our case, a Mac OS X one, and pass the path to the file through the aforementioned -i flag.

Shell

1

xcrun swift-sdk$(xcrun--show-sdk-path--sdk macosx)-imySwiftFile.swift

Here is the output – and, obviously the wallpaper is changed correctly.

This is by no means revolutionary – as the same goal could be achieved with a Command Line XCode project, which could be written in Objective-C, C or C++. In fact, Command Line Tool project template also allows you to specify Swift as a language. But it is very neat regardless, in a sense that you are free to “script” from any editor, in a modern language and easily execute without building Mach-O executable. Moreover, Swift is really well suited – with it’s low ceremony and simplicity – for such script-like usage.

Summary

You can use Swift as general purpose scripting language for all kinds of OS tasks or any automation scripts; the benefit of that will be the possibility to easily access Cocoa APIs and use the same Swift language that you’d use for your day-to-day OS X and iOS development. Moreover, you are free to do that without XCode, and without having to compile any code upfront.

Really, REALLY hoping that Apple will port – or at least not prevent others from porting – Swift to Linux. I want to use it on my servers.

VZi8lJze49gtGR3h

Maybe you should try http://nimrod-lang.org then, it’s essentially swift but cross platform without Apple specific objc stuff. The languages are so similar you can hardly see any differences looking at source code other than swift’s extra braces.

That is exactly what I intend to use Swift for mostly. I’ve built a very useful module manager called SWM. Check it out (not only) if you want to skip XCode for development: https://github.com/jankuca/swm

Sidney de Koning

Have you found a way to pass arguments to this file? So you can call NSTask for instance

Josh

Running the wallpaper code with XCode 7 beta 3 results in errors and does not change the background.