[AppleScript] Upload image to imgur

Sharing and discussing custom actions for LaunchBar
iRounak
Rank 4
Rank 4
Posts: 191
Joined: Fri Oct 23, 2009 7:11 pm
Contact:

[AppleScript] Upload image to imgur

Postby iRounak » Fri Feb 15, 2013 4:37 am

Credit goes to http://scriptlife.tumblr.com/post/19005232591/imgur

I just made the necessary changes to make it work with LB (haven't even read the code throughly :D ).

How to use:
1. Bring up the image(s) you want to upload in LB
2. Tab
3. Bring up the script and hit return
4. The URL will be copied to your clipboard

Code: Select all

on open input
   set thelist to {}
   tell application "Finder"
      -- convert file paths to posix
      set imageList to {}
      repeat with i from 1 to (count input)
         set end of imageList to POSIX path of (item i of input as alias)
      end repeat
      -- no images selected
      if (count imageList) is 0 then
         display dialog "No image files selected" with title "Imgur uploader" buttons {"Quit"} default button "Quit"
         return
         --upload
      else
         --set dialogResponse to display dialog "You are uploading " & (count imageList) & " image(s)." & return & return & "The images will show up in separate tabs in your default browser once they are uploaded." with title "Imgur uploader" buttons {"Upload", "Cancel"} default button "Cancel"
         --if button returned of dialogResponse is "Upload" then
         repeat with i from 1 to (count imageList)
            set apiKey to "26ff5c40cbedf50e7f81124ab473c1cc"
            set curlCommand to "curl -F \"key=" & apiKey & "\" -F \"image=@" & item i of imageList & "\" http://api.imgur.com/2/upload"
            set answer to do shell script curlCommand
            set atid to AppleScript's text item delimiters
            set AppleScript's text item delimiters to "<imgur_page>"
            set link to text item 2 of answer
            set AppleScript's text item delimiters to "<"
            set link to text item 1 of link
            set AppleScript's text item delimiters to atid
            set browser to my default_Browser()
            tell application browser to open location link
            set end of thelist to (link & "
") as text
            
         end repeat
         set the clipboard to thelist as text
         --   end if
      end if
   end tell
end open

on default_Browser()
   tell (system attribute "sysv") to set MacOS_version to it mod 4096 div 16
   if MacOS_version is 5 then
      set {a1, a2} to {1, 2}
   else
      set {a1, a2} to {2, 1}
   end if
   set pListpath to (path to preferences as Unicode text) & "com.apple.LaunchServices.plist"
   tell application "System Events"
      repeat with i in property list items of property list item 1 of contents of property list file pListpath
         if value of property list item a2 of i is "http" then
            set longName to value of property list item a1 of i
            set AppleScript's text item delimiters to "."
            set shortName to last text item of longName
            set AppleScript's text item delimiters to ""
            return shortName
         end if
      end repeat
      return "Safari"
   end tell
end default_Browser

greenblue
Rank 1
Rank 1
Posts: 35
Joined: Sun Aug 07, 2011 9:27 am

Re: [AppleScript] Upload image to imgur

Postby greenblue » Sat Mar 30, 2013 12:28 am

Thanks! Works perfect in LaunchBar. I just wish there was a way to use my username and password within the script so it would upload to my imgur account instead of anonymously to imgur.

EDIT: Whoops... The "Upload images to imgur.com" script is now disrupted by some changes at imgur.

Now the script ends up giving a link not directly to the image (http://i.imgur.com/M0kkAft.jpg) but to a container area for the image
(http://imgur.com/M0kkAft) instead.

Is there an easy change to the script to get the former link directly to the image (http://i.imgur.com/M0kkAft.jpg) instead of the latter?

Or to put it more simply, we should have the extension in the clipboard when it's done.

greenblue
Rank 1
Rank 1
Posts: 35
Joined: Sun Aug 07, 2011 9:27 am

Re: [AppleScript] Upload image to imgur

Postby greenblue » Wed Jan 04, 2017 10:33 pm

This completely stopped working some time ago. I'm currently using Mac OS 10.12.2, but it also didn't work in previous versions. There's no error or anything, just nothing happens.

Any ideas on how to fix?


Return to “LaunchBar Actions”

Who is online

Users browsing this forum: No registered users and 4 guests