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.
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a very short recipe, Recipe 12.18, “How to run an external command (process) in a different directory.”
You want to use another directory as the base directory when running an external command.
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.16, “How to use 'and' and 'or' when running external commands in Scala.”
You want to use the equivalent of the Unix
|| commands to perform an if/then/else operation when executing external commands.
Use the Scala operators
#||, which mirror the Unix
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.
Linux find/copy FAQ: How can I use the
find command to find many files and copy them all to a directory?
Scala shell script FAQ: How do I create a Unix/Linux shell script to run a small Scala script?
If you want to run a Scala script as a Unix or Linux shell script -- such as hello.sh -- write your script like this:
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".
Our ThreadedStreamHandler class
Based on an old-but-good article at JavaWorld, I'm developing the following Java ThreadedStreamHandler class. (It's still a work-in-progress, but as mentioned, commands that don't have to be run using
sudo seem to work just fine right now.)