How would you put an AppleScript script under version control?

I always put the .applescript (plain text files) in version control (SVN). This way I can easily compare between different versions, and it is also quite easy if for multiusers. You can highlight the changes that has been made by other users. This is not possible with binary files, like compiled script files.


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.


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.