Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Scripting the Mac with Ruby

Scripting the Mac with Ruby

Given at the June 2017 meeting of the Indy.rb user group, this talk covers core Mac automation technologies and how Ruby fits into the equation.

Chris Vannoy

June 14, 2017
Tweet

More Decks by Chris Vannoy

Other Decks in Technology

Transcript

  1. Scripting the Mac with Ruby Indy.rb - June 2017 Chris

    Vannoy (@dummied), Senior Engineer @ Sigtsr
  2. using terms from application "Messages" on message received this_message from

    this_buddy for this_chat if the name of this_buddy is in {"My Former Student"} then set canned_responses to {"Can't talk.", "BRB", "You're not my supervisor"} set this_response to some item of the canned_responses send this_response to this_chat end if end message received end using terms from
  3. launchd » Think cron, but with more XML <?xml version="1.0"

    encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.spotify.webhelper</string> <key>KeepAlive</key> <dict> <key>NetworkState</key> <true/> </dict> <key>RunAtLoad</key> <true/> <key>Program</key> <string>/Users/chrisvannoy/Library/Application Support/Spotify/SpotifyWebHelper</string> <key>SpotifyPath</key> <string>/Applications/Spotify.app</string></dict> </plist>
  4. » ~/Library/LaunchAgents » /Library/LaunchAgents » Can run at load and/or

    at intervals » man launchctl » load <plist> » unload <plist> » Highly recommended: LaunchControl
  5. » GUI » Can package a script in an app,

    workflow or service » Can even code sign if you want to distribute
  6. Where Ruby fits in » Installed on every Mac »

    2.0.0 since Mavericks (2013) » Support baked into Automator
  7. Wait. System Ruby? » We're not "developing" here, we're scripting

    » Dependencies are the devil (especially if distributing)
  8. PinboardDigest » Pulls your last 10 links with a certain

    tag(s) from Pinboard. » Takes user input » Passes said input into Ruby » Script calls the Pinboard API, processes results » Copies markdown to clipboard » AppleScript pastes it into the open document
  9. #!/usr/bin/ruby require 'net/https' require 'cgi' require 'json' AUTH_TOKEN = "YOUR_AUTH_CODE_GOES_HERE"

    NUMBER_OF_RESULTS = 10 input = STDIN.read starter = "https://api.pinboard.in/v1/posts/all?auth_token=#{AUTH_TOKEN}&results=#{NUMBER_OF_RESULTS}&format=json" url = URI.parse(input != "" ? starter + "&tag=#{CGI.escape(input)}" : starter) http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE # ¯\_(ϑ)_/¯ request = Net::HTTP::Get.new(url.request_uri) res = http.request(request) result = JSON.parse(res.body) text = "#{input}" text << "# Links for #{Time.now.strftime("%B %e ")}\n\n" result.each do |link| text << "* **[#{link["description"]}](#{link["href"]}):** #{link["extended"]}\n\n" end print text
  10. SearchLink » Brett Terpstra » Replace placeholder markdown with search

    results in selected text » ~2000 lines of Ruby.
  11. Helpful tidbits » sqlite3 on every Mac. » oascript »

    curl on every Mac. » Ruby's system can be your bestest friend.