Javascript has a richer library (and more flexible records) than Applescript, but the JXA JavaScript for Applications version is only available from OS X Yosemite onwards.
Code that doesn’t automate applications can, nevertheless run on earlier versions of OS X, and may be useful for things like encodeURI()
and decodeURI()
etc;
The following example should, I think, work both on Yosemite and on previous versions:
#!/bin/bash
function runJS () {
local OSX_VER=$(sw_vers -productVersion | awk -F '.' '{print $2}')
if [ "$OSX_VER" -lt 10 ]; then
echo "$(
/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc -e "print($1)"
)"
else
echo "$(osascript -l JavaScript -e "$1")"
fi
}
ENCODED_URI=$(runJS "'file://' + encodeURI('/Users/houthakker/Desktop/Some file that we want to print.txt')")
printf "Encoded:\n\n"
echo "$ENCODED_URI"
DECODED_URI=$(runJS "decodeURI('$ENCODED_URI').slice(7)")
printf "\n\nand then unencoded:\n\n"
printf "\t$DECODED_URI"