CURL help download to specific directory

The goal of my script is as follows:

  • curl URL copied to the clipboard
  • isolate a URL for the file download & isolate a NAME for the downloaded file
  • specify PATH for downloaded file to be placed on HD

Here it is, so far;

---GET HTML SOURCE CODE FROM URL ON THE CLIPBOARD, RETURN grep'D SECTION---
set curl_url to (do shell script "curl " & (the clipboard) & " | grep '170x170-75.jpg'") as text
---DEFINE PREFIX AND SUFIX OF DESIRED TEXT---
set name_delim1 to "alt=\""
set name_delim2 to "\" class="
---CLEAN BETWEEN DELIMS---
set text item delimiters to name_delim1
set interim_text1 to text item 2 of curl_url
set text item delimiters to name_delim2
set file_name to text item 1 of interim_text1 & ".jpg"
set text item delimiters to ""
---DEFINE PREFIX AND SUFIX OF DESIRED URL---
set image_delim1 to "\" class=\"artwork\" src=\""
set image_delim2 to ".170x170-75.jpg\" /></div></a>"
---CLEAN BETWEEN DELIMS---
set text item delimiters to image_delim1
set interim_text2 to text item 2 of curl_url
set text item delimiters to image_delim2
set image_url to text item 1 of interim_text2 & ".jpg"
set text item delimiters to ""
---DOWNLOAD---
set download_path to (POSIX path of (path to pictures folder)) & file_name
do shell script "curl " & image_url & " -o " & download_path

Problem:
If file_name is “TV Show: The Episode, Season 1” the file is downloaded as “TV”
Question:
How would I specify “$HOME:Pictures:Artwork:TV” as the directory instead of path to pictures folder

This is my first post, so please be gentle. My script has been sourced from info found on other posts, and in no way signifies my “understanding” (if any) of Applescript. I am very determined and patient, please bear with me.

Thanks to everyone!

Carpe Diem

This might help.

---GET HTML SOURCE CODE FROM URL ON THE CLIPBOARD, RETURN grep'D SECTION---
set curl_url to (do shell script "curl " & (the clipboard) & " | grep '170x170-75.jpg'") as text
---DEFINE PREFIX AND SUFIX OF DESIRED TEXT---
set name_delim1 to "alt=\""
set name_delim2 to "\" class="
---CLEAN BETWEEN DELIMS---
set text item delimiters to name_delim1
set interim_text1 to text item 2 of curl_url
set text item delimiters to name_delim2
set file_name to text item 1 of interim_text1 & ".jpg"
set text item delimiters to ""
---DEFINE PREFIX AND SUFIX OF DESIRED URL---
set image_delim1 to "\" class=\"artwork\" src=\""
set image_delim2 to ".170x170-75.jpg\" /></div></a>"
---CLEAN BETWEEN DELIMS---
set text item delimiters to image_delim1
set interim_text2 to text item 2 of curl_url
set text item delimiters to image_delim2
set image_url to text item 1 of interim_text2 & ".jpg"
set text item delimiters to ""
---DOWNLOAD---
set download_path to (POSIX path of (path to pictures folder)) & "Artwork/TV/" & file_name as string
do shell script "curl " & image_url & " -o " & download_path

Thanks for the suggestion!

I changed my path to start at Users folder and find the TV folder from there, this works for now but Ideally, how would I specify a folder on a different HDD.

Also, the major problem still persists. If the file_name as defined by my applescript is returned “Star Wars: The Clone Wars, Season 1” the file is saved in my specified directory but as “Star”. It is always the first word of whatever file_name is, even if I try different images / names etc.

To make troubleshooting a bit more realistic I have included the URL which will be on the clipboard;
http://itunes.apple.com/ca/tv-season/star-wars-the-clone-wars-season/id291145063

---GET HTML SOURCE CODE FROM URL ON THE CLIPBOARD, RETURN grep'D SECTION---
set curl_url to (do shell script "curl " & (the clipboard) & " | grep '170x170-75.jpg'") as text
---DEFINE PREFIX AND SUFIX OF DESIRED TEXT---
set name_delim1 to "alt=\""
set name_delim2 to "\" class="
---CLEAN BETWEEN DELIMS---
set text item delimiters to name_delim1
set interim_text1 to text item 2 of curl_url
set text item delimiters to name_delim2
set file_name to text item 1 of interim_text1 & ".jpg"
set text item delimiters to ""
---DEFINE PREFIX AND SUFIX OF DESIRED URL---
set image_delim1 to "\" class=\"artwork\" src=\""
set image_delim2 to ".170x170-75.jpg\" /></div></a>"
---CLEAN BETWEEN DELIMS---
set text item delimiters to image_delim1
set interim_text2 to text item 2 of curl_url
set text item delimiters to image_delim2
set image_url to text item 1 of interim_text2 & ".jpg"
set text item delimiters to ""
---DOWNLOAD---
set download_path to (POSIX path of (path to users folder)) & "Artwork/TV/" & file_name as string
do shell script "curl " & image_url & " -o " & download_path

Here are the currents results of the script;
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 0 --:–:-- --:–:-- --:–:-- 0Warning: Failed to create the file /Users/Artwork/TV/Star: No such file or
Warning: directory

0 356k 0 1179 0 0 2348 0 0:02:35 --:–:-- 0:02:35 19650
curl: (23) Failed writing body (0 != 1179)

curl: (6) Could not resolve host: Wars; nodename nor servname provided, or not known

curl: (6) Could not resolve host: The; nodename nor servname provided, or not known

curl: (6) Could not resolve host: Clone; nodename nor servname provided, or not known

curl: (6) Could not resolve host: Wars,; nodename nor servname provided, or not known

curl: (6) Could not resolve host: Season; nodename nor servname provided, or not known

curl: (6) Could not resolve host: 1.jpg; nodename nor servname provided, or not known

set file_name to text item 1 of interim_text1 & ".jpg"

do you think this has anything to do with it?

Honestly I’m unsure.

If you try running the script and return image_url it returns what I want. i.e. the high-res URL of the original URL we isolated with grep. As I said, my understanding of applescript is in the embryonic stages, so at the moment I am just messing around with that section of the script you specified without much results.

I understand your thought I believe, and It must be something simple that is missing, because the file is currently being saved where I want it.

Please keep the suggestions coming, I might stumble onto a solution, and fresh perspective is ALWAYS welcome.

here’s what I get for “download_path” when I run the script:
“/Users/Artwork/TV/Star Wars: The Clone Wars, Season 1.jpg”

Firstly, I if your finished path or file names are going to have spaces in them, you are going to have to use “quoted form of posix path…” because you can’t pass a file path with spaces to the shell, it has be to single quoted out, or else you have to manually delimit spaces with forward slashes (and that gets ugly).

And just as importantly… Not sure if you’ve been working on Macs long or not, but, you can not have a colon in any file/path name. Colons are the Mac’s native path delimiters… (Mac HD:folder:foo:bar.jpg) …as also are slashes when you do unix/posix stuff.
So, you are going to have to turn any colon in any file name or path into something else, like a dash or underscore.

Chris

Hey Chris, thanks for the info!

I am very new at this stuff, so I’m sure theres a better way to do this. Heres the changes I’ve made. file_name is now returned as “Star Wars - The Clone Wars, Season 1” which shouldn’t conflict I hope? the download_path shows correct as “/Users/Artwork/TV/Star Wars - The Clone Wars, Season 1.jpg”

---GET HTML SOURCE CODE FROM URL ON THE CLIPBOARD, RETURN grep'D SECTION---
set curl_url to (do shell script "curl " & (the clipboard) & " | grep '170x170-75.jpg'") as text
---DEFINE PREFIX AND SUFIX OF DESIRED TEXT---
set name_delim1 to "alt=\""
set name_delim2 to "\" class="
---CLEAN BETWEEN DELIMS---
set text item delimiters to name_delim1
set interim_text1 to text item 2 of curl_url
set text item delimiters to name_delim2
set file_name to text item 1 of interim_text1 & ".jpg"
set text item delimiters to ""
---REPLACE : IF FOUND---
set name_delim to text item delimiters of AppleScript
set text item delimiters of AppleScript to ":"
set file_name to text items of file_name
set text item delimiters of AppleScript to " -"
set file_name to "" & file_name
set text item delimiters of AppleScript to name_delim
---DEFINE PREFIX AND SUFIX OF DESIRED URL---
set image_delim1 to "\" class=\"artwork\" src=\""
set image_delim2 to ".170x170-75.jpg\" /></div></a>"
---CLEAN BETWEEN DELIMS---
set text item delimiters to image_delim1
set interim_text2 to text item 2 of curl_url
set text item delimiters to image_delim2
set image_url to text item 1 of interim_text2 as string
set text item delimiters to ""
---DOWNLOAD---
set download_path to (POSIX path of (path to users folder)) & "Artwork/TV/" & file_name as string
do shell script "curl " & image_url & " -o " & download_path

I have a new issue since the changes I just made (I don’t understand why lol). When I run script I receive this error message;
error “curl: option -: is unknown
curl: try ‘curl --help’ or ‘curl --manual’ for more information” number 2

Hi,

all strings which could contain space characters must be escaped in shell scripts, because spaces are parameter delimiters. The best way is using quoted form of.


set download_path to (POSIX path of (path to users folder as text)) & "Artwork/TV/" & file_name
do shell script "curl " & image_url & " -o " & quoted form of download_path

Stefan thanks so much!

The file is now saved in the correct directory with the correct name. However, when I try and open the image I receive a message saying;

The file “Star Wars - The Clone Wars, Season 1.jpg” could not be opened.

It may be damaged or use a file format that preview doesn’t recognize.

Any ideas? (Thanks so much for all the suggestions so far, probably saved me 2 days worth of eye burning web surfing)

EDIT: The odd thing here is that when the file was saved using the incorrect file name( i.e just “Star”), I was able to open with preview. It didn’t identify as an image, which bothered me. Now it is identified as a jpg image with no image to show…more head scratching and research is needed… Oh and here is the whole script as is;

---GET HTML SOURCE CODE FROM URL ON THE CLIPBOARD, RETURN grep'D SECTION---
set curl_url to (do shell script "curl " & (the clipboard) & " | grep '170x170-75.jpg'") as text
---DEFINE PREFIX AND SUFIX OF DESIRED TEXT---
set name_delim1 to "alt=\""
set name_delim2 to "\" class="
---CLEAN BETWEEN DELIMS---
set text item delimiters to name_delim1
set interim_text1 to text item 2 of curl_url
set text item delimiters to name_delim2
set file_name to text item 1 of interim_text1
set text item delimiters to ""
---REPLACE : IF FOUND---
set name_delim to text item delimiters of AppleScript
set text item delimiters of AppleScript to ":"
set file_name to text items of file_name
set text item delimiters of AppleScript to " -"
set file_name to "" & file_name
set text item delimiters of AppleScript to name_delim
---DEFINE PREFIX AND SUFIX OF DESIRED URL---
set image_delim1 to "\" class=\"artwork\" src=\""
set image_delim2 to ".170x170-75.jpg\" /></div></a>"
---CLEAN BETWEEN DELIMS---
set text item delimiters to image_delim1
set interim_text2 to text item 2 of curl_url
set text item delimiters to image_delim2
set image_url to text item 1 of interim_text2 as string
set text item delimiters to ""
---DOWNLOAD---
#set file_location to "$HOME:Pictures:Artwork:"
set download_path to (POSIX path of (path to users folder as text)) & "K3FKA/Pictures/Artwork/TV/" & file_name
do shell script "curl " & image_url & " -o " & quoted form of download_path

If you change the filename of the downloaded file from “.jpg” to “.txt” or “.html”, you’ll see that it’s actually a web page saying that the file can’t be found on the server. You’re not actually downloading the .jpg. I’m afraid you’ll have to go back to your script to rework it so that it actually downloads the .jpg file. I think, but can’t be certain, that you’ve stripped the end of the filename (.170x170-75.jpg) from the file you’re trying to download. You need to add that back on after removing it.

So you need to change the line where you set image_url to something like this:

set image_url to ((text item 1 of interim_text2) & ".170x170-75.jpg") as string

Also, you may need to strip “http://” from the url, because curl rejects the “:”.

Thanks emendelson!!!

You’re suggestions really put me on the right path, I decided to scrap everything I have, and restart with what I have learned. Heres the result, cleaner looking to me…

set curl_url to do shell script "curl " & (the clipboard) & " | grep '170x170-75.jpg'"
---CLEAN UP OF CURL---
set as_tid to text item delimiters of AppleScript
set text item delimiters of AppleScript to ".170x170-75"
set curl_url to text items of curl_url
set text item delimiters of AppleScript to ""
set curl_url to "" & curl_url
set text item delimiters of AppleScript to ":"
set curl_url to text items of curl_url
set text item delimiters of AppleScript to " -"
set curl_url to "" & curl_url
---DEFINE IMAGE URL---
set text item delimiters to "\" class=\"artwork\" src=\"http -//"
set interim_text1 to text item 2 of curl_url
set text item delimiters to "\" /></div></a>"
set image_url to text item 1 of interim_text1 as string
---DEFINE FILE NAME---
set text item delimiters to "alt=\""
set interim_text2 to text item 2 of curl_url
set text item delimiters to "\" class="
set file_name to text item 1 of interim_text2
set text item delimiters of AppleScript to as_tid
---DOWNLOAD---
set download_path to (POSIX path of (path to users folder as text)) & "K3FKA/Pictures/Artwork/TV/" & file_name
do shell script "curl " & image_url & " -o " & quoted form of download_path

I would like to enable a notification from growl displaying the preview of the image and sound, confirming the download. I remember seeing some good info regarding Growl notification on the forums, so I will be researching that in the meantime. If anyone has already done something similar, I would really appreciate am example to try and work through.

Thanks for all the help and suggestions, keep em comin!

Ok so I got the Growl notification working…almost :wink:
My goal is to show the image, the file name and the message “Download Complete”

---DOWNLOAD---
set downloadPath to (POSIX path of (path to home folder as text)) & "Pictures/Artwork/TV/" & fileName & ".tiff"
do shell script "curl " & imageURL & " -o " & quoted form of downloadPath
---GROWL---
tell application "Growl"
	set the allNotificationsList to {"Download Complete", "Error"}
	set the enabledNotificationsList to {"Download Complete"}
	register as application "iTunes Artwork" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "iTunes Artwork"
	notify with name "Download Complete" title fileName description "Download Complete!" application name "iTunes Artwork" image (downloadPath as Image)
	notify with name "Error" title "Error:( " description "Something went wrong." application name "iTunes Artwork"
end tell

Result: error “Can’t make "/Users/K3FKA/Pictures/Artwork/TV/Star Wars - The Clone Wars, Season 1.tiff" into type TIFF picture.” number -1700 from “/Users/K3FKA/Pictures/Artwork/TV/Star Wars - The Clone Wars, Season 1.tiff” to TIFF picture

Keep in mind I did change the overall script to add .tiff instead of .jpg because thats what growl seems to be asking for. I’m hoping it’s something simple, I have the message and file name part working just fine.

EDIT: Ok, I apologize for not fully searching the forum before asking my question. I found a solution @ http://macscripter.net/viewtopic.php?id=36585. Making the following changes generates albeit a small preview and the correct file name / message.

notify with name "Download Complete" title fileName description "Download Complete!" application name "iTunes Artwork" image from location downloadPath

Consider that just changing the file extension does not change the contents (type) of the file.
A .mp3 remains a .mp3 even you have changed the extension to .aiff

Thanks Stefan, good to know.

I have wanted to venture further with this script, and after examining the source page a bit more closely I realized theres an opportunity to retrieve each episode description. Which got me thinking, could I then create a text file with all the seasons episode names and description. I’m just starting to mess around and was wondering how would I put

set cURL to do shell script "curl " & (the clipboard) & " | grep 'placed here'"

Thanks