Set up Programmable Tooltip to open Sublime with a specific file

Today I just set up a more complex and powerful use case: I now can select a file path and open the Sublime text to see that file.

Occasionally, I'd see a file path in my terminal because it is printed by the program that is running, and I would copy the file path and open to see and/or edit in Sublime Text.

Programmable Tooltip can support this use case using the Execute tip. Execute behaves a bit differently in a way that, if it is selected, Programmable Tooltip will execute your script again with the defined args of the Execute tip.

Here's how your script should look like:

#!/usr/bin/env ruby

require 'json'
require 'cgi'

def main(input)
  input = input.strip.force_encoding('UTF-8')
  # The force encoding prevents the error: ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError)
  
  items = [
    {
      type: 'execute',
      label: 'Open in Sublime',
      args: ['--execute-programmable-tooltip', '/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl', input]
    }
  ]

  puts items.compact.to_json
end

if __FILE__ == $0
  if ARGV[0] == '--execute-programmable-tooltip'
    system(*ARGV[1..-1])
    puts [].to_json
  else
    main(ARGV[0])
  end
end

Basically, in your script, you can set --execute-programmable-tooltip for the Execute tip. When selected, your script will be executed with the arguments: ['--execute-programmable-tooltip', '/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl', input].

Then, you can detect --execute-programmable-tooltip and execute the command that you want. In this case, I open the selected file path, input, with Sublime.

The Execute tip is a bit dangerous, so please be mindful when using the Execute tip.

Subscribe to tanin

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe