Wednesday, September 07, 2016

Pastebin and Tasker - An Automatic Clipboard Archiving Solution

The other day I realized I had no seamless way of accessing my phone's clipboard while on my desktop computer. Sure, I had work-arounds like pasting the clipboard into a Google Doc and accessing said Google Doc on a desktop machine, but that's clunky to the say the least. Here's my first pass at a more automated solution.

After a bit of poking around, I decided that I'd shoot for the following: have my Android phone push the system clipboard to pastebin.com, and then use a browser on my desktop to access pastebin. Solutions like AirDroid are really slick, but are relatively cumbersome to setup. By having the clipboard in pastebin, I know I can easily access it from anywhere. Additionally, pastebin's archival nature means that accessing older clipboard entries is breeze. As a bonus, pastebin's API is drop dead simple and I was hoping it would play nice with Tasker.

Fortunately, my hunch was correct: Tasker's HTTP Post action is a near perfect fit for the pastebin API. Here's the Tasker Action I worked up that creates a new pastebin entry:

Pastebin, Create (115)
  A1: Variable Set [ Name:%api_key To:API_KEY ... ] 
  A2: Variable Set [ Name:%paste To:%par1 ...] 
  A3: Variable Set [ Name:%expires To:%par2 ... ] 
  A4: Variable Set [ Name:%user_key To:USER_KEY ... ] 
  A5: HTTP Post [ 
   Server:Port:pastebin.com
   Path:/api/api_post.php 
   Data / File:
    api_option=paste
    api_dev_key=%api_key
    api_paste_private=2
    api_paste_name=From My Phone
    api_paste_expire_date=%expires
    api_user_key=%user_key
    api_paste_code=%paste 
   ... Content Type:application/x-www-form-urlencoded
   Output File:Tasker/pastebin.response.txt
  ] 
  A6: Read File [
   File:Tasker/pastebin.response.txt To Var:%response 
  ] 
  A7: Return [ Value:%response Stop:On ] 

A few notes on the code above. You'll need to fill in your own version of API_KEY and USER_KEY to make this work. The above action is intended to be called by Perform Action with two parameters: the content to paste and an expiration date. The result is a generic Pastebin, Create action that can be used in a number of contexts.

With the above code written and debugged, I just needed to setup a simple action that would ship off the the magic %CLIP variable that contains the clipboard contents to pastebin.

Pastebin, Push Clipboard (134)
 A1: Perform Task [
   Name:Pastebin, Create
   Priority:%priority+1
   Parameter 1 (%par1):%CLIP
   Parameter 2 (%par2):1D
   Return Value Variable:%response
   Stop:Off ] 
 A2: Notify [
    Title:%response Text: Icon:ipack:crystalhd:package_editorspackage_editors
    Number:0 Permanent:Off Priority:3
 ] 

Note that I'm setting the 'paste' to expire in 1 day. I expect I'll tweak this over time, but for now, I'm thinking 1 day makes sense.

Finally, I setup a profile that detects when the %CLIP variable changes:

With all of this in place, anytime I copy text to my phone's clipboard, it shows up just a few moments later on pastebin. An unexpected bonus: my phone has a Copy to Clipboard sharing action which means that I can effectively push data from apps to pastebin with a single click. Here's an example of this functionality as seen from the YouTube app:

If I clicked the Copy Link option, a few moments later, the URL of the video would be on pastebin.

I doubt this will be the last word on copy and pasting the clipboard between my phone and desktop devices. But it sure is a fun start. I've been a fan of Pastebin for many years, so I'm psyched to integrate with the service. And the fact that it's completely automated, well let's just say I wrote the code and even I think it's magic. Time will tell how practical it is, though.

No comments:

Post a Comment