Page 1 of 1

Shorten Dropbox URL + custom Growl notification

PostPosted: Sun Feb 19, 2012 3:36 am
by Pe8er
(big update 5/6 — updated the scripts)

Hey all,

I wrote a couple simple Applescripts to help me automate uploading to Dropbox. Both of them trigger when any file is added to the Public folder or any other folder under it.

Here's how the rule looks like:

Image

The first script generates a Dropbox URL, shortens it using bit.ly and copies to clipboard. I use this service because it's a) reliable and b) I really like the "j.mp" domain name :) If you don't like bit.ly, I'm sure It is very easy to customize it for your shortening service of choice.

Here's the first script. I save it as .scpt in Library > Scripts and call in Hazel. This method makes it a lot easier to maintain updates to the script across different computers.

Code: Select all
on hazelProcessFile(theFile)
   
   set login to "xxx"
   set apikey to "xxx" --both login and apikey are from bit.ly
   set db_ to "xxx" --your Dropbox ID
   set path_ to "/" & my strip(theFile as text)
   
   --set long URL
   set url_ to "http://dl.dropbox.com/u/" & db_ & path_
   
   --encode URL
   set cmd to "/usr/bin/php -r 'echo trim(urlencode(" & "\"" & url_ & "" & "\"));'"
   set url_ to (do shell script cmd)
   
   --shorten URL
   set cmd to "curl --stderr /dev/null 'https://api-ssl.bitly.com/v3/shorten?&login=" & login & "&apiKey=" & apikey & "&format=txt&uri=" & url_ & "'"
   set url_ to (do shell script cmd)
   
   set the clipboard to url_
end hazelProcessFile

on strip(theText)
   set text item delimiters to (path to home folder as string) & "Dropbox:Public:" as text
   set a to text items of theText
   set b to item 2 of a
   set text item delimiters to ":"
   set a to text items of b
   set text item delimiters to "/"
   set path_ to a as text
   set text item delimiters to ""
   return path_
end strip


The second script displays a Growl notification. In order to make things as universal as possible, I have the main Growl script sitting in Scripts folder. It contains all subroutines related to displaying Growl notifications. And then I have a tiny script for Hazel, which calls the big one and lets me customize title and description of the notification.

Here's the general Growl script:

Code: Select all
(* EXAMPLE OF USAGE
   my notify("title", "description", "some path")
on notify(title_, description_, filepath_)
   set growlscript to load script POSIX path of ((path to home folder as text) & "Dropbox:Library:Scripts:Snippets:" & "Growl Notifications" & ".scpt")
   set growlscript's title_ to title_
   set growlscript's description_ to description_
   set growlscript's filepath_ to filepath_ as alias
   growlscript's notify()
end notify
*)

property growlapp_ : "Misc Notifications"
property notifs_ : "Info"
property icon : "Growl.app"
property name_ : notifs_

property title_ : missing value
property description_ : missing value
property filepath_ : missing value
property pr : "2"
property list_ : {"public.jpeg", "public.tiff", "public.png"}

on notify()
   tell application "Finder"
      set path_ to filepath_
      if path_ is "" then set path_ to (path to applications folder as text) & "Growl.app" as alias
      set appname_ to name of file path_
      set info_ to info for path_
      set type_ to the type identifier of info_
   end tell
   
   if type_ is in the list_ then
      my notifyimg(filepath_)
   else if type_ is "com.apple.application-bundle" then
      my notifyapp(appname_)
   else
      my notifyicon(filepath_)
   end if
end notify

on register()
   tell application id "com.Growl.GrowlHelperApp" to register as application growlapp_ all notifications notifs_ default notifications notifs_ icon of application icon
end register

on notifyimg(filepath_)
   tell application id "com.Growl.GrowlHelperApp" to notify with name name_ title title_ description description_ application name growlapp_ priority pr image from location (filepath_ as alias)
end notifyimg

on notifyapp(appname_)
   tell application id "com.Growl.GrowlHelperApp" to notify with name name_ title title_ description description_ application name growlapp_ priority pr icon of application appname_
end notifyapp

on notifyicon(filepath_)
   tell application id "com.Growl.GrowlHelperApp" to notify with name name_ title title_ description description_ application name growlapp_ priority pr icon of file filepath_
end notifyicon


Save it to Library > Scripts > Snippets as "Growl Notifications.scpt".

Now, the script for Hazel rule:

Code: Select all
set growlscript to load script POSIX path of ((path to home folder as text) & "Dropbox:Library:Scripts:Snippets:" & "Growl Notifications" & ".scpt")

tell application "Finder" to set growlscript's title_ to text 1 thru -5 of (get name of file theFile)

set growlscript's description_ to "Uploaded successfully!"
set growlscript's filepath_ to theFile as alias
growlscript's notify()


I hope someone will find this useful!

Re: Shorten Dropbox URL + custom Growl notification

PostPosted: Tue Feb 28, 2012 1:57 pm
by Zefish
Thanks for the tips! Those are the kind I love to do with Hazel :)

Re: Shorten Dropbox URL + custom Growl notification

PostPosted: Tue Feb 28, 2012 2:55 pm
by Pe8er
Cheers!

Here is an updated version, with 4x more awesome. It's universal now. It shows appropriate icon, depending on file type — image, app, document etc.

Code: Select all
set _list to {"public.jpeg", "public.tiff", "public.png", "com.compuserve.gif", "com.adobe.photoshop-image"}

tell application "Finder"
   set _name to name of file theFile
   set _info to info for theFile
   set _type to the type identifier of _info
end tell

set _title to "Done!"
set _description to _name & " was uploaded to Dropbox."

tell application id "com.Growl.GrowlHelperApp" to register as application "Hazel Notifications" all notifications {"Info", "Error"} default notifications {"Info", "Error"} icon of application "HazelHelper.app"

if _type is in the _list then
   tell application id "com.Growl.GrowlHelperApp" to notify with name "Info" title _title description _description application name "Hazel Notifications" priority 0 image from location theFile as alias
else if _type is "com.apple.application-bundle" then
   tell application id "com.Growl.GrowlHelperApp" to notify with name "Info" title _title description _description application name "Hazel Notifications" priority 0 icon of application _name
else
   tell application id "com.Growl.GrowlHelperApp" to notify with name "Info" title _title description _description application name "Hazel Notifications" priority 0 icon of file POSIX path of theFile
end if

Re: Shorten Dropbox URL + custom Growl notification

PostPosted: Sat Apr 07, 2012 1:21 pm
by orky
This is wonderful! Since I am somewhat of a rookie, could you elaborate on the hazel rules you have created? The scripts work like a charm, however, I get the growl notification as soon as I place it in the folder and not once the file is uploaded. Also: what condition do you use?
Any help is greatly appreciated.
Seb

Re: Shorten Dropbox URL + custom Growl notification

PostPosted: Tue Jun 05, 2012 5:43 am
by Pe8er
Hey orky, sorry for the late reply. To answer your questions:

1. You're getting the Growl notification as soon as the file is placed in Dropbox folder because Hazel can't know when the upload is completed. Dropbox doesn't tell that in any capture-able way unfortunately.

2. Here's how my rule looks like: http://j.mp/MwuEgy

Let me update the original post with new scripts, I made some improvements to them.

Re: Shorten Dropbox URL + custom Growl notification

PostPosted: Sun May 18, 2014 11:00 am
by Hiryu
Hi -- new user of hazel automation/growl notifications -- and stumbled upon your post. Don't have any coding experience. Was wondering if you have an updated version of this -- basically, I'm trying to have a workflow where as a new file gets added to a specific folder in Dropbox (not public), Hazel would send a growl notification and within that notification is a url link to the file. Then I send that notification through pushover to my phone. When the pushover notification shows up on the phone, I can hit the URL link within the pushover notification so that it opens up the dropbox app on the phone to that specific folder. Any ideas -- would love to hear your opinion, someone who clearly knows a lot more about the scripting world than I do. Thanks!

Re: Shorten Dropbox URL + custom Growl notification

PostPosted: Sun May 18, 2014 11:06 am
by Hiryu
Also, playing around with the first script -- as far as the Dropbox ID -- would that just be the email login? I don't think the script is accepting emails (with the @ and .com). I assume the inputs are without the quotes in the "xxx"? Thanks.