Last active
December 6, 2015 04:07
-
-
Save jjpmann/c7352e896bb31c7ab732 to your computer and use it in GitHub Desktop.
OSX Folder Action (script) to copy dropbox public link to clipboard for new files added to shared folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on adding folder items to this_folder after receiving added_items | |
try | |
set the item_count to the number of items in the added_items | |
if the item_count is equal to 1 then | |
set theFile to item 1 of added_items | |
set theRawFilename to ("" & theFile) | |
set tid to AppleScript's text item delimiters | |
set AppleScript's text item delimiters to ":" | |
set theFileName to (text item 7 of theRawFilename) as text | |
set AppleScript's text item delimiters to tid | |
set theWebSafeFileName to switchText from theFileName to "%20" instead of " " | |
// XXX = dropbox id | |
set theURL to "http://dl.getdropbox.com/u/XXX/screenshots/" & theWebSafeFileName | |
set shortURL to bitlyShorten(theURL) | |
set the clipboard to shortURL as text | |
-- display dialog shortURL & " copied to clipboard." | |
display notification shortURL & " copied to clipboard." | |
delay 1 --> allow time for the notification to trigger | |
end if | |
end try | |
end adding folder items to | |
to switchText from t to r instead of s | |
set d to text item delimiters | |
set text item delimiters to s | |
set t to t's text items | |
set text item delimiters to r | |
tell t to set t to item 1 & ({""} & rest) | |
set text item delimiters to d | |
t | |
end switchText | |
on bitlyShorten(_text) | |
try | |
set login to "XXXX" -- Put your login name here | |
set api_key to "XXXX" -- Put your API key here | |
set the _encodedUrl to _urlEncode(_text) of me | |
if login is "" or api_key is "" then | |
set question to display dialog "Sorry, you need to open the script and provide your bitly username and api key. You can get them from: " & return & "https://bitly.com/a/your_api_key/" & return & "Open this location now?" giving up after 15 | |
if the button returned of the result is "OK" then | |
tell application "Finder" to open location "https://bitly.com/a/your_api_key/" | |
quit | |
end if | |
end if | |
set curlCMD to ¬ | |
¬ | |
"curl --stderr /dev/null \"http://api.bit.ly/shorten?longUrl=" & _encodedUrl & "&history=1&version=2.0.1&login=" & login ¬ | |
& "&apiKey=" & api_key ¬ | |
& "\"| grep shortUrl | grep -o http.*[/a-zA-Z0-9]" | |
tell me to set _bitlyUrl to (do shell script curlCMD) | |
set AppleScript's text item delimiters to "http://" | |
set _bitlyUrl to text item 3 of _bitlyUrl | |
set AppleScript's text item delimiters to "\"" | |
set _bitlyUrl to "http://" & text item 1 of _bitlyUrl | |
set AppleScript's text item delimiters to "" | |
-- Uncomment below if you'd rather use the clipboard | |
-- than Quicksilver | |
-- set the clipboard to _bitlyUrl | |
return _bitlyUrl | |
-- tell application "Quicksilver" to set selection to _bitlyUrl | |
on error a number b | |
display dialog a | |
return | |
end try | |
end bitlyShorten | |
on _urlEncode(theText) | |
set theTextEnc to "" | |
repeat with eachChar in characters of theText | |
set useChar to eachChar | |
set eachCharNum to ASCII number of eachChar | |
if eachCharNum = 32 then | |
set useChar to "+" | |
else if (eachCharNum ≠ 42) and (eachCharNum ≠ 95) ¬ | |
and (eachCharNum < 45 or eachCharNum > 46) ¬ | |
and (eachCharNum < 48 or eachCharNum > 57) ¬ | |
and (eachCharNum < 65 or eachCharNum > 90) ¬ | |
and (eachCharNum < 97 or eachCharNum > 122) then | |
set firstDig to round (eachCharNum / 16) rounding down | |
set secondDig to eachCharNum mod 16 | |
if firstDig > 9 then | |
set aNum to firstDig + 55 | |
set firstDig to ASCII character aNum | |
end if | |
if secondDig > 9 then | |
set aNum to secondDig + 55 | |
set secondDig to ASCII character aNum | |
end if | |
set numHex to ("%" & (firstDig as string) ¬ | |
& (secondDig as string)) as string | |
set useChar to numHex | |
end if | |
set theTextEnc to theTextEnc & useChar as string | |
end repeat | |
return theTextEnc | |
end _urlEncode |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment