Skip to content

Instantly share code, notes, and snippets.

@bacongravy
Last active January 23, 2018 04:28
Show Gist options
  • Save bacongravy/7f2dbbd9a20b0cfa547d3a618b5b0732 to your computer and use it in GitHub Desktop.
Save bacongravy/7f2dbbd9a20b0cfa547d3a618b5b0732 to your computer and use it in GitHub Desktop.
js-eval - Execute JavaScript, parse JSON, on vanilla macOS (no dev tools or packages installed)
#!/bin/sh
alias js-eval="osascript -l JavaScript -e"
# examples:
js-eval "1+1"
# 2
js-eval "Math.PI"
# 3.141592653589793
JSON_STRING="[\"zero\", \"one\", \"two\"]"
js-eval "JSON.parse('$JSON_STRING')[1]"
# one
# advanced example:
# read from stdin and then parse as JSON
js-eval "stdin = $.NSString.alloc.initWithDataEncoding($.NSFileHandle.fileHandleWithStandardInput.readDataToEndOfFile, $.NSUTF8StringEncoding).js;
JSON.parse(stdin)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment