Skip to content

Instantly share code, notes, and snippets.

@ELLIOTTCABLE
Created May 10, 2017 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ELLIOTTCABLE/12e33473dfb325c9374d0cc873bb5356 to your computer and use it in GitHub Desktop.
Save ELLIOTTCABLE/12e33473dfb325c9374d0cc873bb5356 to your computer and use it in GitHub Desktop.
JXA (JavaScript for Automation) ‘Script Library’ exports-test
// Export the mapping to the caller via the *documented* / known-working method
function results(){
return [ ""
, "1. Function declaration of the global environment"
, "2. Implicit propery of the objective global environment"
, "3. Identifier binding on the objective global environment"
, "4. Identifier binding on the declarative global environment"
, "5. Explicit property of the objective global environment (via `this` self-ref)"
, "6. Explicit property of the objective global environment (via anon func)"
]
}
// The documented approach
function one(){ return true }
// Identifier bindings
two = function(){ return true }
var three = function(){ return true }
let four = function(){ return true }
// Explicit property creation
this.five = function(){ return true }
;(function(){ return this })()
.six = function(){ return true }
#!/usr/bin/env osascript -l JavaScript
const test = Library('Test Library')
, results = test.results()
try { test.one() && console.log('✓ '+results[1]) } catch(e) { console.log('✗ '+results[1]) }
try { test.two() && console.log('✓ '+results[2]) } catch(e) { console.log('✗ '+results[2]) }
try { test.three() && console.log('✓ '+results[3]) } catch(e) { console.log('✗ '+results[3]) }
try { test.four() && console.log('✓ '+results[4]) } catch(e) { console.log('✗ '+results[4]) }
try { test.five() && console.log('✓ '+results[5]) } catch(e) { console.log('✗ '+results[5]) }
try { test.six() && console.log('✓ '+results[6]) } catch(e) { console.log('✗ '+results[6]) }
osacompile -l JavaScript -o "Test Library.scpt" "Test Library.js"
mkdir -p -- "$HOME/Library/Script Libraries/"
mv "Test Library.scpt" "$HOME/Library/Script Libraries/"
osascript -l JavaScript "Test Script.js" || true
rm "$HOME/Library/Script Libraries/Test Library.scpt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment