beforeRunningCommand nop bundleUUID 5A9D4FC6-6CBE-11D9-A21B-000D93589AF6 command #!/usr/bin/env ruby -rjcode -KU # Modified version of the bundled "Paste Line/Selection Online…" command. # # Modifications by Henrik Nyh <http://henrik.nyh.se> 2007-07-10. # # Just presents a dialog to select public or private and always copies to # clipboard + Growls when done, or whatever you configure. BUNDLE_SUPPORT = ENV['TM_BUNDLE_SUPPORT'] SUPPORT_PATH = ENV['TM_SUPPORT_PATH'] PASTE_URL = 'http://pastie.textmate.org/pastes' # Options: "Send to Clipboard", "Open in Browser" and whatever else the original command supports – and "Growl". # "Growl" action assumes you have Growl with growlnotify: http://growl.info/documentation/growlnotify.php ACTIONS = ["Send to Clipboard", "Growl"] MESSAGE = "The URL will be copied to clipboard." require "#{BUNDLE_SUPPORT}/lib/doctohtml.rb" %w<textmate progress escape ui>.each { |lib| require "#{SUPPORT_PATH}/lib/#{lib}" } %w<cgi fileutils zlib>.each { |lib| require lib } # We actually don't use the URL, but feel free to. def growl(url) what = ENV.has_key?('TM_SELECTED_TEXT') ? "Selection" : "Document" `growlnotify -t "Pasted #{what} Online" -m "The pastie URL is in your clipboard." --appIcon "TextMate"` # --appIcon "Grab" and --icon "textClipping" are nice alternative icons. end def temp_script(script) dest = %x{ /usr/bin/mktemp -t tm_paste }.chomp at_exit { File.delete dest } FileUtils.cp(script, dest) dest end def get_privacy what = ENV['TM_SELECTED_TEXT'] ? 'selected text' : 'document contents' button = TextMate::UI.alert( :informational, "You're about to pastie the #{what}.", "#{MESSAGE} Please specify the paste privacy level.", "Private", "Cancel", "Public" ) %w<Public Private>.index(button) || exit end def find_language_ext bundle_dirs = [ File.expand_path('~/Library/Application Support/TextMate/Bundles'), '/Library/Application Support/TextMate/Bundles', TextMate.app_path + '/Contents/SharedSupport/Bundles' ] if scope = ENV['TM_SCOPE'] then scope = scope.split(' ').first bundle_dirs.each do |dir| Dir.glob(dir + '/*.tmbundle/Syntaxes/*.{plist,tmLanguage}') do |filename| File.open(filename) do |io| begin plist = OSX::PropertyList.load(io) if scope == plist['scopeName'].to_s then return Array(plist['fileTypes']).first || 'txt' end rescue Exception => e abort "Error while parsing “#{filename}”\n\n#{e}" end end end end end ext = File.extname(ENV['TM_FILENAME'].to_s).sub(/\A\./, '') ext.empty? ? 'txt' : ext end def paste_stdin(priv) wrap = (ENV['TM_SCOPE'] =~ /^text\./) ? 1 : 0 server = $1 if PASTE_URL =~ %r{.*?://(.*?)(/.*)?$} TextMate.call_with_progress(:title => 'Paste to Pastie', :message => "Contacting server “#{server}”…") do text_file, html_file = `/usr/bin/mktemp -t tm_paste && /usr/bin/mktemp -t tm_paste`.split("\n") at_exit { File.delete(text_file); File.delete(html_file) } xml = STDIN.read open(text_file, 'w') do |io| io << CGI::unescapeHTML(xml.gsub(/<[^>]*>/, '')) end Zlib::GzipWriter.open(html_file) do |gz| gz.write document_to_html(xml) end author = ENV['TM_AUTHOR'] || "#{`dscl . read "/Users/$USER" realname|cut -f2 -d:`.strip} (#{ENV['USER']})" ext = find_language_ext %x{ curl #{PASTE_URL} \ -s -L -o /dev/null -w "%{url_effective}" \ -H "Expect:" \ -F "paste[parser]=plaintext" \ -F "paste[restricted]=#{priv}" \ -F "paste[wrap]=#{wrap}" \ -F "paste[display_name]=#{author}" \ -F "paste[file_extension]=#{ext}" \ -F "paste[body]=<#{text_file}" \ -F "paste[textmate_html_gz]=<#{html_file}" } end end pid = fork do priv = get_privacy url = paste_stdin(priv) if url == PASTE_URL # pastie error--something went wrong puts "Server error: Your paste could not be made at this time. " + "Please wait a moment and try again." else growl = ACTIONS.delete("Growl") %x{ osascript #{e_sh temp_script(BUNDLE_SUPPORT + "/paste_to_destinations.scpt")} #{e_sh url} #{e_sh ACTIONS.join("\n")} } if growl growl(url) else puts "Pasted #{url}." end end end Process.detach(pid) fallbackInput line input selection inputFormat xml keyEquivalent ^~V name Paste Document / Selection Online… output showAsTooltip uuid 6E779E48-F146-49BF-B60C-EFDFD1380772