Skip to content

Instantly share code, notes, and snippets.

@yosangwon
Created July 29, 2020 09:46
Show Gist options
  • Save yosangwon/329cf8658e01840777c7786dcc24e1df to your computer and use it in GitHub Desktop.
Save yosangwon/329cf8658e01840777c7786dcc24e1df to your computer and use it in GitHub Desktop.
invoke javascript codes inside ruby on macos to automate macOS features
require 'open3'
require 'shellwords'
require 'json'
def runjs(script)
command = ['osascript', '-ss', '-l', 'JavaScript']
script.split(/\n/).each { |l| command.concat ['-e', l] }
output, status = Open3.capture2(command.shelljoin)
raise "osascript didn't run successfully!" unless status.success?
JSON.parse(output)
end
result = runjs <<-END
var app = Application.currentApplication()
app.includeStandardAdditions = true
var dialogText = "The current date and time is " + (app.currentDate())
app.displayDialog(dialogText)
END
puts result["buttonReturned"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment