Return to Snippet

Revision: 6672
at June 5, 2008 15:23 by banzaiman


Updated Code
#!/usr/bin/env ruby -wKU
# Copyright 2008 Hirotsugu Asari

require "xmlrpc/client"
require 'cgi'

CMD = [ ENV['TM_SUPPORT_PATH'] +
'/bin/CocoaDialog.app/Contents/MacOS/CocoaDialog' ]

srv = XMLRPC::Client.new2("http://snipplr.com/xml-rpc.php")


if ENV['SNIPPLR_KEY'].nil?
  args = [
    'ok-msgbox', '--no-cancel', '--title "Snipplr"',
    '--text "Environment variable SNIPPLR_KEY missing."', '2>/dev/null'
  ]
  %x{ #{ (CMD + args).join(" ") } }
  exit
end


# get tags from user
args = [
  'standard-inputbox', '--title "Get Snippet from Snipplr"',
  '--informative-text "Enter tags to search for"', '--string-output',
  '2>/dev/null'
]

user_response_for_tags = %x{#{(CMD + args).join(" ")}}.split("\n")
if user_response_for_tags[0] == 'Cancel'
  exit
end

# grab snippets from server
matches = []
begin
  matches = srv.call('snippet.list',ENV['SNIPPLR_KEY'],user_response_for_tags[1],'date')
rescue
  args = [
    'ok-msgbox', '--no-cancel', '--title "Snipplr"',
    "--text 'No matching snippet found.'", '2>/dev/null'
  ]
  %x{ #{ (CMD + args).join(" ") } }
  exit
end

## XML-RPC call may return multiple instances of identical item, so trim
## the hash to our needs
snippet_title = {}
matches.each do |h|
  if !snippet_title.has_key?(h['id'])
    snippet_title[h['id']] = h['title']
  end
end

item_string = '' # for CocoaDialog option
snippet_title.each_value do |s|
  item_string += %Q{ '#{s}' }
end

args = [
  'standard-dropdown', '--title "Snipplr"', '--text "Select Snippet"',
  '--exit-onchange', '--string-output', '--items ' + item_string,
  '2>/dev/null'
]

user_choice_for_snippet = %x{#{(CMD + args).join(" ")}}.split("\n")
if user_choice_for_snippet[0] == 'Cancel'
  exit
end

begin
  puts CGI.unescapeHTML(srv.call('snippet.get',snippet_title.index(user_choice_for_snippet[1]))["source"])
rescue
  args = [
    'ok-msgbox', '--no-cancel', '--title "Snipplr"',
    "--text 'No matching snippet found.'", '2>/dev/null'
  ]
  %x{ #{ (CMD + args).join(" ") } }
end

Revision: 6671
at June 5, 2008 15:03 by banzaiman


Initial Code
#!/usr/bin/env ruby -wKU
# Copyright 2008 Hirotsugu Asari

require "xmlrpc/client"

CMD = [ ENV['TM_SUPPORT_PATH'] +
'/bin/CocoaDialog.app/Contents/MacOS/CocoaDialog' ]

srv = XMLRPC::Client.new2("http://snipplr.com/xml-rpc.php")


if ENV['SNIPPLR_KEY'].nil?
  args = [
    'ok-msgbox', '--no-cancel', '--title "Snipplr"',
    '--text "Environment variable SNIPPLR_KEY missing."', '2>/dev/null'
  ]
  %x{ #{ (CMD + args).join(" ") } }
  exit
end


# get tags from user
args = [
  'standard-inputbox', '--title "Get Snippet from Snipplr"',
  '--informative-text "Enter tags to search for"', '--string-output',
  '2>/dev/null'
]

user_response_for_tags = %x{#{(CMD + args).join(" ")}}.split("\n")
if user_response_for_tags[0] == 'Cancel'
  exit
end

# grab snippets from server
matches = []
begin
  matches = srv.call('snippet.list',ENV['SNIPPLR_KEY'],user_response_for_tags[1],'date')
rescue
  args = [
    'ok-msgbox', '--no-cancel', '--title "Snipplr"',
    "--text 'No matching snippet found.'", '2>/dev/null'
  ]
  %x{ #{ (CMD + args).join(" ") } }
  exit
end

## XML-RPC call may return multiple instances of identical item, so trim
## the hash to our needs
snippet_title = {}
matches.each do |h|
  if !snippet_title.has_key?(h['id'])
    snippet_title[h['id']] = h['title']
  end
end

item_string = '' # for CocoaDialog option
snippet_title.each_value do |s|
  item_string += %Q{ '#{s}' }
end

args = [
  'standard-dropdown', '--title "Snipplr"', '--text "Select Snippet"',
  '--exit-onchange', '--string-output', '--items ' + item_string,
  '2>/dev/null'
]

user_choice_for_snippet = %x{#{(CMD + args).join(" ")}}.split("\n")
if user_choice_for_snippet[0] == 'Cancel'
  exit
end

begin
  print srv.call('snippet.get',snippet_title.index(user_choice_for_snippet[1]))["source"]
rescue
  args = [
    'ok-msgbox', '--no-cancel', '--title "Snipplr"',
    "--text 'No matching snippet found.'", '2>/dev/null'
  ]
  %x{ #{ (CMD + args).join(" ") } }
end

Initial URL


Initial Description


Initial Title
TextMate bundle searching and pasting code

Initial Tags
textmate, ruby

Initial Language
Ruby