I want to download gifs from an imgur link

For example I have this link : http://i.imgur.com/lzAppm3.gifv

I want to press a hotkey and the gif will be downloaded to ~/Downloads. Can I somehow do that?

Hey Nikita,

On pages like that something like this AppleScript should work.

-Chris

--------------------------------------------------------------------------------
# Download animated GIF from i.imgur.com.
--------------------------------------------------------------------------------

set jsStr to "document.getElementsByClassName('controls')[0].innerHTML"
set theURL to doJavaScriptInSafari(jsStr)

if theURL ≠ "" then
   set AppleScript's text item delimiters to {"<a href=\"", "\">download<"}
   set theURL to text item 2 of theURL
   
   if theURL starts with "//" then
      set theURL to "http:" & theURL
   end if
   
   tell application "Safari"
      set URL of front document to theURL
   end tell
   
end if

--------------------------------------------------------------------------------
--» HANDLERS
--------------------------------------------------------------------------------
on doJavaScriptInSafari(javascriptStr)
   try
      tell application "Safari" to do JavaScript javascriptStr in front document
   on error e
      error "Error in handler doJavaScriptInSafari() of library NLb!" & return & return & e
   end try
end doJavaScriptInSafari
--------------------------------------------------------------------------------
1 Like

Wow, I really wish I could make sense of this code. Thank you. Is it possible to specify where the file is being saved to?

Hey Nikita,

Not when using Safari as the downloader mechanism.

You'd have to use Curl or another downloader mechanism.

--------------------------------------------------------------------------------
# Download animated GIF from i.imgur.com.
# Using the cURL command-line tool.
--------------------------------------------------------------------------------

set destinationFolder to POSIX path of (path to downloads folder)

set jsStr to "document.getElementsByClassName('controls')[0].innerHTML"
set theURL to doJavaScriptInSafari(jsStr)

if theURL ≠ "" then
   tell application "Safari"
      tell front document
         set refererURL to URL
         set fileName to name
      end tell
   end tell
   
   set AppleScript's text item delimiters to {"<a href=\"", "\">download<"}
   set theURL to text item 2 of theURL
   
   if theURL starts with "//" then
      set theURL to "http:" & theURL
   end if
   
   set curlConfig to "url = " & theURL & linefeed & "referer = " & refererURL
   
   set shellCMD to "
cd " & quoted form of destinationFolder & ";
echo " & quoted form of curlConfig & " | " & "curl -L -s -A 'Opera/9.70 (Linux ppc64 ; U; en) Presto/2.2.1' -K -  -o " & quoted form of (fileName & ".gif")
   
   do shell script shellCMD
   
end if

--------------------------------------------------------------------------------
--» HANDLERS
--------------------------------------------------------------------------------
on doJavaScriptInSafari(javascriptStr)
   try
      tell application "Safari" to do JavaScript javascriptStr in front document
   on error e
      error "Error in handler doJavaScriptInSafari() of library NLb!" & return & return & e
   end try
end doJavaScriptInSafari
--------------------------------------------------------------------------------

-Chris