I was wondering if this was the best solution:
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?
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.
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.
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.
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.
*.scpt diff=scpt
[diff "scpt"] textconv = osadecompile binary=true
$ 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With