$30 off During Our Annual Pro Sale. View Details »

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

    View Slide

  2. An Introduction to Mac
    Automation

    View Slide

  3. AppleScript
    (or JavaScript)

    View Slide

  4. 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

    View Slide

  5. » Drive Mac applications
    » Work with the Finder
    » Get user input
    » Dialog boxes

    View Slide

  6. launchd
    » Think cron, but with more XML




    Label
    com.spotify.webhelper
    KeepAlive

    NetworkState


    RunAtLoad

    Program
    /Users/chrisvannoy/Library/Application Support/Spotify/SpotifyWebHelper
    SpotifyPath
    /Applications/Spotify.app

    View Slide

  7. » ~/Library/LaunchAgents
    » /Library/LaunchAgents
    » Can run at load and/or at intervals
    » man launchctl
    » load
    » unload
    » Highly recommended: LaunchControl

    View Slide

  8. Automator

    View Slide

  9. » GUI
    » Can package a script in an app, workflow or
    service
    » Can even code sign if you want to distribute

    View Slide

  10. Where Ruby fits in
    » Installed on every Mac
    » 2.0.0 since Mavericks (2013)
    » Support baked into Automator

    View Slide

  11. Wait. System Ruby?
    » We're not "developing" here, we're scripting
    » Dependencies are the devil (especially if
    distributing)

    View Slide

  12. Some examples

    View Slide

  13. 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

    View Slide

  14. #!/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

    View Slide

  15. SearchLink
    » Brett Terpstra
    » Replace placeholder markdown with search results
    in selected text
    » ~2000 lines of Ruby.

    View Slide

  16. Helpful tidbits
    » sqlite3 on every Mac.
    » oascript
    » curl on every Mac.
    » Ruby's system can be your bestest friend.

    View Slide

  17. Some ideas

    View Slide

  18. » Text processing
    » Backup
    » File processing
    » Whatever. Let your imagination run wild.

    View Slide

  19. THANKS

    View Slide