Created
          July 29, 2020 09:46 
        
      - 
      
- 
        Save yosangwon/329cf8658e01840777c7786dcc24e1df to your computer and use it in GitHub Desktop. 
    invoke javascript codes inside ruby on macos to automate macOS features
  
        
  
    
      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
    
  
  
    
  | 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