Return to Snippet

Revision: 6670
at June 5, 2008 14:59 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' ]

def ask_for(item='')
  if item.to_s.empty?
    return []
  end
  args = [
    'standard-inputbox','--title "Post Snippet to Snipplr"',
    "--informative-text 'Enter the new snippets #{item}'",
    '--string-output', '2>/dev/null'
  ]
  return %x{#{(CMD + args).join(" ")}}.split("\n")
end

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

# Title
user_response_for_title = ask_for('title')
if user_response_for_title[0] == 'Cancel'
  exit
end

# Tags
user_response_for_tags = ask_for('tags')
if user_response_for_tags[0] == 'Cancel'
  exit
end
user_response_for_tags[1] += ' textmatebundle' # shameless self-promotion

# Languages
user_response_for_langs = nil
while user_response_for_langs.nil?
  args = [
    'inputbox', '--title "Snipplr"', '--informative-text "Choose Language for this snippet"',
    '--button1 "OK"', '--button2 "Cancel"', '--button3 "Show Legal Languages"',
    '--string-output', '2>/dev/null'
  ]
  user_response_for_langs = %x{ #{ (CMD + args).join(" ") } }.split("\n")
  
  
  if user_response_for_langs[0] == 'Cancel'
    exit
  elsif user_response_for_langs[0] == 'Show Legal Languages'
    informative_text = ''
    begin
      known_langs = srv.call('languages.list',ENV['SNIPPLR_KEY']).invert
      known_langs.sort {|a,b| a[1] <=> b[1]}.each do |k,v|
        informative_text += "#{k} (#{v})\n"
      end
    rescue
      informative_text << 'Failed to obtain known language list'
    end
    args = [
      'msgbox', '--title "Snipplr"', '--text "Currently known languages"',
      '--informative-text "' + informative_text + '"', '--debug',
      '--button1 "OK"','2>/dev/null'
    ]
    %x{ #{ (CMD + args).join(" ") } }
    user_response_for_langs = nil
    redo
  end
end


# finally ready to post
begin
  srv_response = srv.call('snippet.post',ENV['SNIPPLR_KEY'],
  user_response_for_title[1],
  ARGF.to_a.to_s,
  user_response_for_tags[1], user_response_for_langs[1])
  print "Successfully posted '#{user_response_for_title[1]}'"
rescue
  args = [
    'ok-msgbox', '--no-cancel', '--title "Snipplr"',
    "--text 'Posting new snippet failed: #{$!}'"  , '2>/dev/null'
  ]
  %x{#{(CMD + args).join(" ")}}
end

Initial URL


Initial Description


Initial Title
TextMate bundle posting code

Initial Tags
textmate, ruby

Initial Language
Ruby