Max/MSP Quick Tip #7: Some more about Applescript in Max
The first Quick Tip about using in Applescript seems to be quite popular so here are a few more useful things to know and some further elaboration on points from the first post. The aim here is to point out some actual uses for Applescripts when working in Max so don’t expect any amazing examples of code you can just throw into a project.
1. osascript -e
You may notice that any example of actual scripts that can be written in max have this ‘osascript -e’ string at the beginning. This is important as the shell object actually communicates with the UNIX shell which, or the Terminal, in a mac so it is necessary to alert the shell that the command should be interpreted as an Applescript and not just a standard shell command. For anyone who understands or possibly uses the terminal in their day to day use of a mac will therefore be happy to know that the shell object can also understand your standard shell commands; ‘mkdir ~/Desktop/mynewfolder’ anyone???
1. Applescript mostly uses simple English
As you may or may not be aware because Applescript is simply a lightweight scripting language to control other processes on your computer it can be written in fairly simple english. To open an app simple tell it to ‘open’ or ‘activate’. If you want it to close tell it to ‘close’. So if you don’t know how to do something, try writing the command more or less as a standard sentence and see if that works. If it doesn’t then maybe started searching the documentation.
2. Escaping reserved characters
Certain characters, mostly grammar marks, need to be ‘escaped’ when writing scripts in message boxes as they often mean different things when used in different contexts. In Max one of the main culprits of broken scripts are speech marks (they disappear when you put them in a message box). So they need to be escaped with a backslash (\).
So to tell Max to open Spotify for example the script would need to look something like this (what is in the speech marks needs to match EXACTLY the name of the application as it is on the system):
osascript -e ‘tell application “Spotify” to open’
To make this work in Max you would need to write:
osascript -e ‘tell application \”Spotify\” to open’
3. You can simulate typing with Applescript
In some cases you might want to automate a keyboard shortcut for example by attaching it to a simple button. To do this you need to send commands to the application called ‘System Events’ using the ‘keystroke’ tool.
As these kinds of scripts will be slightly longer in length it is advisable to write these in the Applescript editor and just trigger the running of the file from max as demonstrated in the first Applescript post on here.
The way you lay this kind of command out is as follows:
tell application “Max”
tell application “System Events”
keystroke “s” using command down
As you can see this script first makes Max the primary app and then proceeds to press the “s” key whilst holding down command. (Note all modifier keys are written in this manner). In Max this script would be written as follows:
osascript -e ‘tell application \”System Events\” to keystroke \”s\” using command down’
Commands in Applescript can also be delayed, a useful trick when scheduling operations and is often necessary in longer and more complicated scripts to space out the commands.
tell application “Max”
Although it’s not particularly imaginative this script gives you an idea of how to lay things out.
Hopefully this was useful to some in pointing out some of the relevant uses of Applescripts when used in conjunction with Max/MSP.