Created
November 28, 2017 14:24
-
-
Save doekman/4f1b08d5b111c92c6294f0fdd3d85365 to your computer and use it in GitHub Desktop.
Determine the used language of an `.scpt` file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
function usage { | |
echo "Usage: $(basename $0) $1 [-n|-i|-a] | |
Arguments: | |
-n or --name: show name of language (default) | |
-i or --id: show id of language | |
-a or --all: show 'id:name' of language | |
-e or --error: (for TESTING) to generate an AppleScript error" | |
} | |
scpt_file=".*\.scpt$" | |
if [[ $# -lt 1 || ! $1 =~ .*\.scpt$ ]]; then | |
usage "applescript-file.scpt" | |
exit 1 | |
fi | |
if [[ $1 =~ ^/.* ]]; then | |
INPUT_FILE=$1 | |
else | |
INPUT_FILE=$(pwd)/$1 | |
fi | |
if [[ ! -f $INPUT_FILE ]]; then | |
echo "File '$INPUT_FILE' doesn't exist" | |
exit 2 | |
fi | |
if [[ $# -gt 1 ]]; then | |
ALL=0 PROPERTY=id | |
case $2 in | |
-n | --name) PROPERTY=name;; | |
-i | --id) ;; | |
-a | --all) ALL=1;; | |
-e | --error) PROPERTY="generate_error";; | |
-h | -? | --help) usage $1; exit 0;; | |
*) echo "Unknown argument '$2'"; exit 3;; | |
esac | |
else | |
PROPERTY=name | |
fi | |
osascript <<AS_EOF | |
set scpt_file to POSIX file "${INPUT_FILE}" | |
try | |
tell application "Script Editor" | |
set scpt_doc to open scpt_file | |
set lang to language of scpt_doc | |
close scpt_doc without saving | |
end tell | |
if "${ALL}" = "1" then | |
set result to (id of lang) & ":" & (name of lang) | |
else | |
set result to ${PROPERTY} of lang | |
end if | |
do shell script "echo " & quoted form of result | |
on error err_msg number err_nr | |
set msg to "AppleScript error #" & err_nr & ": " & err_msg | |
do shell script "echo " & quoted form of msg | |
end | |
AS_EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment