Download image via URL in text file?

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Download image via URL in text file? Thu Jul 07, 2016 3:03 pm • by gcoghill
I'm trying to figure out how to use Hazel to monitor a text file for changes, which will be the addition of image file URLs. I want Hazel (via I suspect a script) to then download the file to a local folder.

This looks promising for the script part: http://osxdaily.com/2007/05/11/download ... mand-line/ (and AppleScript or Automator is fine if either are more ideal solutions):

Code: Select all
cd ~/Desktop; curl -O http://remote-server-IP/remote-image-file.jpg


But I am having trouble as a scripting novice to figure out how to have Hazel grab the URL from the text file and use it in a script.

My initial thought was to set up a Contents rule in Hazel and a Contains Match condition and custom tokens to grab the URL at the end of the text document, and pass that along into the script.

I can see how Hazel grabs the file path (using $1) for use in the shell script, but not how I can dynamically insert a URL.

And for clarification, here's what I want to do: keep a text file on Dropbox that I append image URLs to (via Drafts on the iPhone), and then Have Hazel and a script grab those URLs and save the images locally.

Images are the only limitation to my plain-text workflow, and this is my initial attempt to get past that.
gcoghill
 
Posts: 290
Joined: Tue Sep 18, 2007 8:09 am
Location: Kent, OH

Re: Download image via URL in text file? Fri Jul 08, 2016 11:01 am • by Mr_Noodle
Shellscript is probably not ideal here. Hazel passes in the path of the file being matched. You can't specify any other arguments at the moment. If you use Apple/Javascript, you can specify input attributes that Hazel sends in. The script can then use that info. Check the in-app help for AppleScript as it has more details on this.

Note also that you can call a shellscript from AppleScript.
Mr_Noodle
Site Admin
 
Posts: 11872
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Download image via URL in text file? Fri Jul 08, 2016 11:08 am • by gcoghill
OK cool, thanks for the heads up on AppleScript for passing info from Hazel. That will help my search.
gcoghill
 
Posts: 290
Joined: Tue Sep 18, 2007 8:09 am
Location: Kent, OH

Re: Download image via URL in text file? Sun Jul 10, 2016 4:32 pm • by gcoghill
Here's the solution, in case anyone else wants to do something like this:

Set up a rule to match to the URL you want to download. Set this up a s a custom token, so you can later import the URL as inputAttributes in the AppleScript.

My custom token matched to
Code: Select all
http://[anything]
so that it would just match anything starting with http://, but the custom token would be the full URL.

Then choose "run AppleScript" and set the embedded AppleScript similar to the following:

Code: Select all
set theURL to item 1 of inputAttributes

do shell script "cd ~/Desktop/Hazel\\ test\\ folder/images; curl -O '" & theURL & "'"


(Those double-backslahes are needed if you have spaces in the file path.)

This will save the image as the original file name.
gcoghill
 
Posts: 290
Joined: Tue Sep 18, 2007 8:09 am
Location: Kent, OH

Re: Download image via URL in text file? Mon Aug 08, 2016 7:33 pm • by Anthomoony
gcoghill wrote:Here's the solution, in case anyone else wants to do something like this:

Set up a rule to match to the URL you want to download. Set this up a s a custom token, so you can later import the URL as inputAttributes in the AppleScript.

My custom token matched to
Code: Select all
http://[anything]
so that it would just match anything starting with http://, but the custom token would be the full URL.

Then choose "run AppleScript" and set the embedded AppleScript similar to the following:

Code: Select all
set theURL to item 1 of inputAttributes

do shell script "cd ~/Desktop/Hazel\\ test\\ folder/images; curl -O '" & theURL & "'"


(Those double-backslahes are needed if you have spaces in the file path.)

This will save the image as the original file name.


Thank you so much gcoghill for your Shellscript. It's very useful !

In my case, I would like to move the downloaded file into a subfolder which name would be an other custom token.
I've been learning the Applescript for two days, and I know absolutly nothing about Shell script.

Can you help me add this action to the Shellscript just after that the downloading has run, please ?

Best regards
Anthomoony
Anthomoony
 
Posts: 1
Joined: Mon Aug 08, 2016 2:32 pm


Return to Support