If you’re ever working on a really small Scala project — something that contains only a few source code files — and don’t want to use SBT to create a JAR file, you can do it yourself manually. Let’s look at a quick example. Note that the commands below work on Mac and Linux systems, and should work on Windows with minor changes.
In this article it helps if you already know a little bit about AppleScript, though that’s not completely necessary. Near the end of the tutorial I show how to invoke the AppleScript code using Scala, so feel free to skip down to there if you just want to see that — you can always read the stuff at the top for reference later.
I’ve currently written this document as a “note to self” about how the Android
AsyncTask works. It’s currently incomplete, but if you want to know how an AsyncTask works, most of the answers are generally here. I provide documentation for most aspects of the
AsyncTask, though my coverage of (a) updating progress/status and (b) canceling an
AsyncTask is a little weak atm.
To run external shell commands in SBT, first start SBT from your operating system command line:
Then run the
After some output you’ll see this prompt:
Now you can execute shell commands by including them in double quotes, and following them by an exclamation mark, like this:
scala> "ls -al" !
For more information, see the SBT consoleProject documentation page.
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.20, “An index of methods available to run external system commands.”
The following tables list the methods of the scala.sys.process package that you can use when running external (system) commands.
Methods to execute external commands
Table 12-1 lists the methods that you can use to execute system commands.
Table 12-1. Methods to execute system commands
If you ever need to execute AppleScript from a Java or Scala application, this code shows how to solve the programming part of this problem.
Given an AppleScript command as a Scala multiline string, like this:
// press the `fn` key twice val asCommand = """ tell application "System Events" key code 63 key code 63 end tell """
you can execute that command by calling this
If you ever want to create a Unix shell script that you can give to someone else so they can double-click it and run it through the Mac OS X Finder, all you have to do is (a) name the file with the ".command" extension and (b) make it executable. So, just name your Mac/Unix script like this:
Then make it executable, like this:
chmod +x ShowProcesses.command
You can also leave out the usual
#!/bin/sh part on the first line.
At the time of this writing, you can't easily find the Scala Process and ProcessBuilder classes (the Scala API documentation), so in an effort to help you (and the search engines) find those classes more easily, here are direct links to them:
Scala exec FAQ: How do I execute external system commands in Scala?
When it comes to executing external system commands, Scala is a dramatic improvement over Java. The operators Scala makes available are much more like Perl or Ruby, and the operators themselves are consistent with traditional shell commands, and are therefore easy to remember. Let's take a look at a few examples.
In earlier articles I've described how to execute system commands from Java applications. A long time ago I wrote my first article on this topic (How to execute system commands from Java), and more recently I wrote an updated version of that article titled "Executing system commands from Java using the ProcessBuilder and Process classes".