Paste to pastie.org

Sharing and discussing custom actions for LaunchBar
esteele
Posts: 1
Joined: Wed Jul 22, 2009 2:30 am

Paste to pastie.org

Postby esteele » Wed Jul 22, 2009 2:41 am

I spent the afternoon working on a script to paste to pastie.org. The script will accept files, text pasted into LaunchBar, or the clipboard contents if neither of those has been passed in. The text will be posted to pastie.org and the URL will be returned to LaunchBar (and the clipboard).

My typical workflow would be:
Select text in terminal, command-shift-F1 to send the text to LaunchBar, tab, type "pastie", hit enter, and then paste the url to wherever I'd need it (or hit enter to open it in a browser).

Or you can start by searching for a file, tab, type "pastie", etc...

Hope it helps someone. :)


Code: Select all

on run
   sendTextToPastie(the clipboard)
end run

on handle_string(textToPaste)
   sendTextToPastie(textToPaste)
end handle_string

on open of filesToPaste
   set nl to ASCII character 10
   set bannerblock to "*******************************************************************************"
   set textToPaste to ""
   repeat with oneFile in filesToPaste
      set textToPaste to textToPaste & nl & nl & bannerblock & nl & (POSIX path of oneFile) & nl & bannerblock & nl & nl & (read oneFile)
   end repeat
   sendTextToPastie(textToPaste)
end open

on sendTextToPastie(textToPaste)
   set pastieURL to "http://pastie.org/pastes"
   # Escape the text, particularly single quotes which seem to cause problems
   set textToPaste to quoted form of textToPaste
   # Use line breaks that pastie will understand
   set textToPaste to replaceString(textToPaste, ASCII character 13, ASCII character 10)
   try
      set responseURL to (do shell script "curl http://pastie.caboo.se/pastes/create -H 'Expect:' -F 'paste[parser]=plaintext' -F 'paste[body]=" & "'" & textToPaste & "'" & "' -F 'paste[authorization]=burger' -s -L -o /dev/null -w '%{url_effective}'")
      set the clipboard to responseURL
      growlAnnounce("Text pasted to " & responseURL)
      tell application "LaunchBar"
         set selection to responseURL
      end tell
   on error errmesg number errnumber
      growlAnnounce("There was an error")
      display dialog errmesg
   end try
end sendTextToPastie

on replaceString(theText, oldString, newString)
   set AppleScript's text item delimiters to oldString
   set tempList to every text item of theText
   set AppleScript's text item delimiters to newString
   set theText to the tempList as string
   set AppleScript's text item delimiters to ""
   return theText
end replaceString

on growlAnnounce(txt)
   tell application "GrowlHelperApp"
      notify with name ¬
         "LaunchBar" title ¬
         "Pastie" description txt ¬
         application name "LaunchBar"
   end tell
end growlAnnounce

Todd
Posts: 6
Joined: Sat Jan 31, 2009 7:22 pm

Re: Paste to pastie.org

Postby Todd » Fri Aug 28, 2009 10:10 pm

Thanks esteele, works great and very useful.

Todd

aristidesfl
Rank 3
Rank 3
Posts: 120
Joined: Sat Feb 27, 2010 12:54 am

Re: Paste to pastie.org

Postby aristidesfl » Wed Mar 03, 2010 12:02 am

Very good but growl notifications not working

no-username
Rank 1
Rank 1
Posts: 36
Joined: Thu Mar 19, 2009 4:31 am

Re: Paste to pastie.org

Postby no-username » Tue Mar 09, 2010 2:11 am

I worked one of these up the other day, and ran into issues with certain characters, such as a lone "<" or any line starting with "<" will kick an error when you hit curl. There are a few other characters that will throw curl for an error as well.

Your script handle UTF-8 nicely, which I was not able to easily do as I took the route of saving the copied contents to a file, and then sending it into curl with </tmp/username/paste.txt

I do like how you send the event to LaunchBar for opening the url, I may have to incorporate that if you do not mind :)

Here is my final working script:
http://bornbroken.wordpress.com/2010/03 ... astie-org/

The basics are:
1) Registers itself with Growl as “LaunchBar2Pastie”, only if Growl is installed.
2) Takes your current clipboard and folds it up into a smaller chunk so you can see a preview of what you are about to send. This should alleviate any fears that passwords or other private data is on your clipboard.
3) Saves your entire clipboard into /tmp in a directory of your username. This is so there are no collisions with other users on your computer who may happen to also use this script.
4) If you click OK to the preview dialog, your data is sent to pastie.org.
5) Growl shows you a notification that all went well, along with the url where you code will reside.
6) The final url is placed on your clipboard.
7) A log of the date and pastie.org url is saved in ~/Library/Logs as tab separated text.

aristidesfl
Rank 3
Rank 3
Posts: 120
Joined: Sat Feb 27, 2010 12:54 am

Re: Paste to pastie.org

Postby aristidesfl » Tue Mar 09, 2010 2:59 pm

The previous script with some modifications:
- Changed the contents od the growl notifications
- Added the logo of pastie to the growl notification (for this you need to download the file, put it on the actions folder and do not change the name)
- Removed the confirmation script
- Added the url to lauchbar after the contents have been pasted (for quick access if needed)

http://hotfile.com/dl/31959199/79a1e55/Pastie.scpt.html

no-username
Rank 1
Rank 1
Posts: 36
Joined: Thu Mar 19, 2009 4:31 am

Re: Paste to pastie.org

Postby no-username » Sun Mar 14, 2010 5:04 am

aristidesfl wrote:The previous script with some modifications:
- Changed the contents od the growl notifications
- Added the logo of pastie to the growl notification (for this you need to download the file, put it on the actions folder and do not change the name)
- Removed the confirmation script
- Added the url to lauchbar after the contents have been pasted (for quick access if needed)

http://hotfile.com/dl/31959199/79a1e55/Pastie.scpt.html


Thank you for the modifications.
Can you elaborate on your second change about the logo? I think this can be automated easily, with curl just grabbing the logo if it does not find it in the same directory as the script. I just need to know how to make it work. I have had intermittent luck downloading your file at the link, and did not save it the other day.

I think I will make a first run preference, where it was ask: "Do you want previews of your posts... [YES] [NO] which will save a preference file. I personally just do not trust what is on my pasteboard, and wanted to see a few lines of it. I also saw it was a request of a few others in some comments on a very minimal version of a script much like this.

I like your last change. Do you mind if I incorporate your changes, or would you like to maintain two separate versions? If you want to keep the versions distinct, would you like me to include your version with mine, and make note of it in the read me?

The last thing I want to do is add instant send to it. This way, you can select a file, and instant send it to the script, never needing to open the file, copy anything at all, etc. I should have that in place wither this weekend or next.

Thank you for the suggestions/changes.
Is there an official repository of scripts for Launchbar?

aristidesfl
Rank 3
Rank 3
Posts: 120
Joined: Sat Feb 27, 2010 12:54 am

Re: Paste to pastie.org

Postby aristidesfl » Sun Mar 14, 2010 1:03 pm

Can you elaborate on your second change about the logo? I think this can be automated easily, with curl just grabbing the logo if it does not find it in the same directory as the script. I just need to know how to make it work. I have had intermittent luck downloading your file at the link, and did not save it the other day.

The script is getting the logo from the script file icon itself. So, if it as the right icon, the right name and it is in the right location it should work. The problem in getting the icon with curl is that it would take some time every time you run the script.

I think I will make a first run preference, where it was ask: "Do you want previews of your posts... [YES] [NO] which will save a preference file. I personally just do not trust what is on my pasteboard, and wanted to see a few lines of it. I also saw it was a request of a few others in some comments on a very minimal version of a script much like this.

Make it if you can. But you would need to have a file to store the preferences right?

I like your last change. Do you mind if I incorporate your changes, or would you like to maintain two separate versions? If you want to keep the versions distinct, would you like me to include your version with mine, and make note of it in the read me?

Not at all. No need to have 2 separate versions. I just posted mine the way I like it.

The last thing I want to do is add instant send to it. This way, you can select a file, and instant send it to the script, never needing to open the file, copy anything at all, etc. I should have that in place wither this weekend or next.

Instant send would be very nice. Plus you could make instant send with text too. Like selecting a piece of text and instant send it.

If you have troubles downloading with that link, it is another one: http://www.zshare.net/download/73691852043466e4/

Also if you want just to see the code:

Code: Select all

-- LaunchBar2Pastie
-- Suggestions and comments: http://twitter.com/cometbus

-- Script that will take the current clipboard, send it to pastie.org, and return the url
-- Notifications via growl, and an option to preview the paste before it is sent

-- `curl` command discovered at http://codesofa.com/blog/archive/2009/01/08/use-pastieorg-with-launchbar.html
-- but it was broken, and would not accept much input, so I started over.  In the intest of full-disclosure, I wanted to
-- give credit as it was a source of start.  In the end, I did not use any of the code from that page.

-- [01/22/2010 08:51:29 PM]   
--   Added support for growl
--   Changed hard coded name of "pastie" to growl_app_name, look for growl_app_name in growlRegister() function
--   Defined growl_app_name as a global, which will allow it to scope into the two sub functions for growl.
--      This is an old function I wrote for a LunachBar to a tr.im url script I made a while back
--   Now returning false if there is trouble registering with growl, posting to pastie.org etc
--   Show a small growl window with the url to pastie.org in it, not sure what happens if growl is not installed, someone tell me please?
--   Added in try/catch/on error blocks, with return [true|false], I do not want accidental pastie.org's that the user does not know about

-- [01/23/2010 06:01:38 AM]   
--   Added in display dialog that shows the first 10 lines of what you are posting, messsy shell code to limit the amount of text in display dialog
--   set a carriage return variable cause the code was looking gross ( eff you applescript )
--   There is a big mess of passing the clipboard to `tr` to convert to unix line endings, which seem to matter
--      and is also needed to fold it's width and then get the 10 lines.  This is because
--      display dialog is not that flexible, and can push the buttons off screen
--   Logging pastie.org urls to a .log file, format is url<tab>date at ~/Library/Logs/LaunchBar2Pastie.log
--    Cleaned up all display dialogs, added titles and buttons and default buttons
--    Fixed a bug where the clipboard was getting sent to pastie as just the preview clipboard, see original_clipboard

-- [02/24/2010 01:39:13 AM]
-- When saving the data to tmp, save it in a directory of their username, so this does not collide with multi-user systems
-- Appear to work, I think I am going to call it a night.
-- Fixed a bug where if line endings were mac, pastie.org would show the data wrong.
--    [1] this is now done in two locations in the code, either make a fucntion, or do it once and carry it in a varable.

-- TODO [01/22/2010 09:54:14 PM]   
--   LaunchBar can instand send a file anywhere, which means it could read that file, and pastie.org it or also just hit
--      space and paste in some code as well.  Not sure the limit of the small LaunchBar input field
--      http://www.obdev.at/resources/launchbar/help/RunningAppleScripts.html
-- Clean up /tmp when done.  This is debatable if needed, since /tmp is cleaned on reboot anyway
-- Fix the line endings code to be cleaner, see [1]

-- Define the name that will be listed in growl
set growl_app_name to "Pasted"
global growl_app_name -- This is shared in functions

-- This is what you call redic and ritarded
set carriage_rtn to "
"

-- All the logic for regisering with growl is in the function, run it, and it will know to avoid repeatedly re-registering
try
   -- I am not sure there is anything in growlRegister() to kick the `try` into an error, but it can't hurt to fall back onto AppleScript error internals
   growlRegister() -- Register with growl, or exit past if the .growlTicket is discovered
on error
   display dialog "There was an error talking to growl, grrrowl. (No data posted to pastie.org)" with title "Growl Error" with icon 0 buttons {"rawr!!!"} default button 1
   return false -- halts from sending to pastie.org
end try

-- Show the user the first n lines of the pasteboard, allow them to back out if they have something
-- secret on the clipboard they do not want to go to pastie.org
try
   set original_clipboard to the clipboard
   set x to the clipboard
   set x to do shell script "pbpaste | tr '\\r' '\\n' | fold -s -w 50 | head -n 10 | pbcopy" -- yes, this is needed, fixed line endings, and stuffs it all back onto the clipboard
   set x to do shell script "pbpaste | head -n 10"
   set intro_dialog_copy to "Here are the first 10 lines of the text your are abou to send to pastie.org."
   set full_message to intro_dialog_copy & carriage_rtn & carriage_rtn & x
   set the clipboard to original_clipboard
   
   --display dialog of full_message with title "Text about to be sent to pasite.org" buttons {"Cancel", "Send to pastie.org"} default button 2
   --on error
   --   display dialog "You cancelled the pastie.org operation. (No data posted to pastie.org)" with title "pastie.org cancelled" with icon 2 buttons {"OK"} default button 1
   --return false -- halts from sending to pastie.org
end try

-- ***** Start the work of sending the current clipboard to pastie.org ***** --
-- Make a directory in /tmp of the user's short username so this script can be used by multi-user systems
try
   set short_username to (short user name of (system info)) -- Get the current users short username
   do shell script ("/bin/mkdir -p /tmp/" & short_username)
   
   -- Put the entire pasteboard into /tmp/short_username/pastie.txt
   -- Fix the line endings, or they can be mac, which pastie.org chokes on
   do shell script ("/usr/bin/pbpaste | tr '\\r' '\\n' > /tmp/" & short_username & "/pastie.txt")
on error errText number errNum
   display dialog "Error saving to tmp (" & errText & ")" with title "pastie.org Error" with icon 0 buttons {"Shucks"} default button 1
   return false -- halts from sending to pastie.org
end try

-- Set to variable the big mess of curl code that will post to pastie.org, reading in from a file, as
-- ust shoving in a clipboard can have characters like double and single quote, or greater then and less than
-- that will toss it's lunch.
-- `curl` command returns the pastie.org variable "url_effective", which is the url of our pasted data
try
   set pastie_shell_script to "curl http://pastie.caboo.se/pastes/create -H 'Expect:' -F 'paste[parser]=plaintext' -F 'paste[body]=</tmp/" & short_username & "/pastie.txt' -F 'paste[authorization]=burger' -s -L -o /dev/null -w '%{url_effective}'"
   set responseURL to (do shell script pastie_shell_script)
   set the clipboard to responseURL -- Put the pastie.org url into the clipboard, nukes existing clipboard
   growlNotify(responseURL) -- Show the user that the url is in the clipboard
   tell application "LaunchBar"
      set selection to responseURL
   end tell
   -- Log the url to a file
   set shell_date to do shell script "date"
   set log_cmd to "echo " & responseURL & "   " & shell_date & " >> ~/Library/Logs/" & growl_app_name & ".log"
   do shell script log_cmd
on error
   display dialog "Posting to pastie.org failed." with title "pastie.org Error" with icon 0 buttons {"Shucks"} default button 1
   return false -- halts from sending to pastie.org
end try

-- functions and support for growl communication

-- Register this script with growl so notifications can be configured
-- Also defines a helper function to easily allow definging the actual growl text copy
-- Sets the icon in growl to that of Launchbar
using terms from application "GrowlHelperApp"
   on growlRegister()
      -- test to see if growl has already been registered
      set growl_registered to "no"
      set app_support_path to path to application support from user domain
      set growl_ticket_path to POSIX path of app_support_path & "Growl/Tickets/" & growl_app_name & ".growlTicket"
      
      tell application "Finder"
         if exists growl_ticket_path as POSIX file then
            set growl_registered to "yes"
         end if
      end tell
      
      -- debugging, toggle on and off as needed
      --display dialog "growl registration status: " & growl_registered
      
      -- Only if this script is not registered, do we need to run the below registration, which is slow, but only on first run
      if growl_registered = "no" then
         tell application "GrowlHelperApp"
            register as application growl_app_name all notifications {"Alert"} default notifications {"Alert"} icon of application "Launchbar.app"
         end tell
      end if
   end growlRegister
   
   -- Quick notifier for Growl, takes a string, and shoots it to growls nifty little window
   on growlNotify(astring)
      set growl_description to astring
      
      tell application "GrowlHelperApp"
         notify with name "Alert" title growl_app_name description growl_description application name growl_app_name icon of file "~/Library/Application Support/LaunchBar/Actions/Pastie.scpt"
      end tell
   end growlNotify
end using terms from

-- All done, have fun, going to the store, making chili, which you really should ask for the recipe, it is *that* good



As far as I know this forum is the only "repository" with scripts

ptujec
Wizard
Wizard
Posts: 321
Joined: Fri Dec 19, 2008 11:36 am
Contact:

Re: Paste to pastie.org

Postby ptujec » Sun Aug 22, 2010 11:31 pm

I modified (and simplified the script a bit). It works for me, hope for you aswell …

I'm using "on handle_string" in order to avoid accidentally posting stuff from my clipboard. So what I'm doing is 1) select code 2) send it to LaunchBar via InstantSend 3) choose and execute the script

Code: Select all

-- LaunchBar2Pastie by @cometbus
-- http://forums.obdev.at/viewtopic.php?p=13647#p13647
-- modified 2010-08-22 by @Ptujec

on handle_string(s)
   
   -- copy string to clipboard
   set the clipboard to s
   
   -- Put the entire pasteboard into /tmp/pastie.txt
   -- Fix the line endings, or they can be mac, which pastie.org chokes on
   do shell script ("/usr/bin/pbpaste | tr '\\r' '\\n' > /tmp/" & "/pastie.txt")
   
   set pastie_shell_script to "curl http://pastie.caboo.se/pastes/create -H 'Expect:' -F 'paste[parser]=plaintext' -F 'paste[body]=</tmp/" & "/pastie.txt' -F 'paste[authorization]=burger' -s -L -o /dev/null -w '%{url_effective}'"
   set responseURL to (do shell script pastie_shell_script)
   set the clipboard to responseURL -- Put the pastie.org url into the clipboard, nukes existing clipboard
   
   my growlRegister()
   growlNotify("LaunchBar2Pastie", responseURL) -- Show the user that the url is in the clipboard
   
end handle_string

----------------------------------
on growlRegister()
   tell application "GrowlHelperApp"
      register as application "LaunchBar2Pastie" all notifications {"Alert"} default notifications {"Alert"} icon of application "Launchbar.app"
   end tell
end growlRegister

on growlNotify(grrTitle, grrDescription)
   tell application "GrowlHelperApp"
      notify with name "Alert" title grrTitle description grrDescription application name "LaunchBar2Pastie"
   end tell
end growlNotify


jazz albert
Posts: 2
Joined: Wed Nov 09, 2016 12:07 pm

Re: Paste to pastie.org

Postby jazz albert » Wed Nov 09, 2016 12:09 pm

ptujec wrote:I modified (and simplified the script a bit). It works for me, hope for you aswell …

I'm using "on handle_string" in order to avoid accidentally posting stuff from my clipboard. So what I'm doing is 1) select code 2) send it to LaunchBar via InstantSend 3) choose and execute the script

[code]-- LaunchBar2Pastie by @cometbus
-- http://forums.obdev.at/viewtopic.php?p=13647#p13647
-- modified 2010-08-22 by @Ptujec
TragusInfantigoRx


It is still working i have checked it. Nice work!


Return to “LaunchBar Actions”

Who is online

Users browsing this forum: No registered users and 3 guests