execute

How to fire Mac OS X notifications with AppleScript (and Scala) alvin November 3, 2016 - 9:06pm
Table of Contents1 - A “Hello, world” MacOS AppleScript notification2 - AppleScript: Mac notification with a sound3 - How to add a title and subtitle to your notification4 - Running from Scala or Java5 - See also6 - Summary

Summary: This tutorial demonstrates how to fire MacOS system notifications with AppleScript (and Scala or Java).

In this article I assume that you already know at least a little bit about how to use AppleScript, and just want to know how to trigger a MacOS notification. At the end of the tutorial I show how to invoke the AppleScript code using Scala and Java.

How to run external shell commands in SBT (Simple Build Tool)

To run external shell commands in SBT, first start SBT from your operating system command line:

$ sbt

Then run the consoleProject task/command:

> consoleProject

After some output you’ll see this prompt:

scala>

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.

Scala - An index of methods to run/execute external system commands

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

How to execute AppleScript from a Java or Scala application

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 executeAppleScriptCommand method:

How to run a Unix shell script from the Mac Finder

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:

ShowProcesses.command

Then make it executable, like this:

chmod +x ShowProcesses.command

You can also leave out the usual #!/bin/sh part on the first line.

The Scala Process and ProcessBuilder classes (API documentation)

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:

How to execute (exec) external system commands in Scala alvin May 19, 2012 - 10:36am

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.