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 开发者_JAVA百科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?
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.
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 keep the plain text .applescript files in Git, and I have a simple Bash script that I run every time I want to make a build of the app, which takes care of compiling the AppleScript. Here's my script:
#!/usr/bin/env bash
APPNAME="My Awesome App"
# make sure we're in the right place
if [ ! -d ".git" ]; then
echo "ERROR: This script must be run from the root of the repository."
exit
fi
# clear out old build
rm -r dist/*
mkdir -p "dist/$APPNAME.app/"
# copy files
cp -r app/* "dist/$APPNAME.app/"
# compile all applescript
cd "dist/$APPNAME.app/Contents/Resources/Scripts/"
for f in *.applescript
do
osacompile -o "`basename -s .applescript "$f"`.scpt" "$f"
rm "$f"
done
This script assumes that you have your entire app (i.e. the Contents/
folder and everything in it) inside the folder app/
in the root of your Git repository. It copies everything over to a new copy of the app in dist/
, then compiles all AppleScript files in the Contents/Resources/Scripts/
folder of the new copy.
To use this yourself, I recommend copying my script to bin/build.sh
in the root of your repository, running chmod +x bin/build.sh
to make it executable, and then just run ./bin/build.sh
any time you want a new build of your app.
精彩评论