Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you put an AppleScript script under version control?

I was wondering if this was the best solution:

  • Put the .applescript files under version control
  • Create an installation script to compile the files with osacompile

But there is also the .scptd directory. Or I could put both .applescript and .scpt files under version control?

What is the best solution?

like image 271
charlax Avatar asked Oct 03 '11 23:10

charlax


People also ask

Does Apple still use AppleScript?

AppleScript is a scripting language created by Apple Inc. that facilitates automated control over scriptable Mac applications. First introduced in System 7, it is currently included in all versions of macOS as part of a package of system automation tools.

How do I run AppleScript on Mac?

In the Script Editor app on your Mac, click the Run button in the toolbar, or press Command-R, to execute the commands in your script.

What does Apple script editor do?

Script Editor lets you create powerful scripts, tools, and even apps. You can create scripts to perform repetitive tasks, automate complex workflows, and control apps or even the system.


2 Answers

I love @DanielTrebbien's solution, but it is a little too complex for me to expect people to implement in order to use for my github project. A simpler option that just empowers you to see text changes in the diff is to tell the diff process to textconv with osadecompile.

Add to .gitattributes

*.scpt diff=scpt 

Add to .git/config

[diff "scpt"]   textconv = osadecompile   binary=true 

Here is sample diff from my AppleScript-droplet github project

$ git diff --- a/AppleScript-droplet.app/Contents/Resources/Scripts/main.scpt +++ b/AppleScript-droplet.app/Contents/Resources/Scripts/main.scpt @@ -1,3 +1,3 @@ -on open filelist -       ## Set useTerminal to true to run the script in a terminal -       set useTerminal to true +on open file_list +       ## Set use_terminal to true to run the script in a terminal +       set use_terminal to true 

Big thanks to @DanielTrebbien for his answer that lead me to osadecompile.

like image 113
Bruno Bronosky Avatar answered Sep 18 '22 04:09

Bruno Bronosky


Edit: The now is a "official" gitfilter to do this, called osagitfilter. It builds on this idea and fixes some quirks of osacompile...


If using git, you can use a filter driver to transparently (1) decompile SCPT files so that only the AppleScript source code is committed (called "cleaning" the binary SCPT) and (2) recompile back to SCPT when checking out (called "smudging" the AppleScript source).

First add the following shell script named git-ascr-filter to /usr/local/bin:

#!/bin/sh if [ $# -ne 2 ]; then     echo "Usage: $0 --clean/--smudge FILE">&2     exit 1 else     if [ "$1" = "--clean" ]; then         osadecompile "$2" | sed 's/[[:space:]]*$//'     elif [ "$1" = "--smudge" ]; then         TMPFILE=`mktemp -t tempXXXXXX`         if [ $? -ne 0 ]; then             echo "Error: \`mktemp' failed to create a temporary file.">&2             exit 3         fi         if ! mv "$TMPFILE" "$TMPFILE.scpt" ; then             echo "Error: Failed to create a temporary SCPT file.">&2             rm "$TMPFILE"             exit 4         fi         TMPFILE="$TMPFILE.scpt"         # Compile the AppleScript source on stdin.         if ! osacompile -l AppleScript -o "$TMPFILE" ; then             rm "$TMPFILE"             exit 5         fi         cat "$TMPFILE" && rm "$TMPFILE"     else         echo "Error: Unknown mode '$1'">&2         exit 2     fi fi 

Make sure to chmod a+x the script.

Configure the 'ascr' filter by running:

 git config filter.ascr.clean "git-ascr-filter --clean %f" git config filter.ascr.smudge "git-ascr-filter --smudge %f" 

Then add to .gitattributes:

 *.scpt filter=ascr 

Now whenever you make a change to a SCPT file and git add it, the decompiled AppleScript source will be staged instead of the binary SCPT. Also, whenever you check out a SCPT file (which is really stored as an AppleScript blob in the repository), the SCPT file is re-created on disk.

like image 26
Daniel Trebbien Avatar answered Sep 20 '22 04:09

Daniel Trebbien