AppleScript Help Needed

Joined
Jan 12, 2010
Messages
12
Reaction score
0
Points
1
I would like to make an AppleScript where I can point to an image url such as http://website.com/image.jpg and download that file to a specified folder on my computer. Ideally, I could set the script to also re-name the downloaded image. For example, rename the above referenced "image.jpg" file to "newimagename.jpg". Any suggestions for this? Seems like it would be an easy one but I am very new to AppleScripts
 
Joined
Dec 13, 2007
Messages
256
Reaction score
10
Points
18
Location
United States of America
Your Mac's Specs
2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork ‘09
Yeah, that's quite simple. I've used the command-line program curl in my script, but you could just as easily tell Safari to download the file (though you'd then have to wait for Safari to launch).
Code:
set imageURL to "http://www.google.com/images/srpr/logo1w.png"
set folderTarget to the POSIX path of the (path to the desktop)
set newImageName to "newimagename.png"
display dialog (folderTarget as string) & newImageName

do shell script "curl " & imageURL & " -o " & folderTarget & newImageName
I've used the Google logo and am saving it to a file called newimagename.png on the desktop. However, there are a few problems with my script. First, you have to put the URL into the script itself, and you also have to specify the filename (and filetype) for it to work. Basically, it's not much more efficient than going to the website and saving the image manually.

If you give a more specific account of what exactly you want to automate, I could help you work toward a much better AppleScript.
 
OP
L
Joined
Jan 12, 2010
Messages
12
Reaction score
0
Points
1
nabl, thanks very much! this is a great start.

quite simply, i have a spreadsheet with 2 columns:

1 column lists the image url, and the 2nd column lists the new image name.

the list has thousands of images, and i'm not sure if an applescript can handle this...although it seems like it could.

in your above applescript example you gave me, could i repeat that code 1,000 times and run the script on 1,000 separate images?
 
Joined
Dec 13, 2007
Messages
256
Reaction score
10
Points
18
Location
United States of America
Your Mac's Specs
2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork ‘09
In which application did you make the spreadsheet? There are quite a few ways this could be done, and yes, it will involve repeating essentially the same steps as above however many times.
 
OP
L
Joined
Jan 12, 2010
Messages
12
Reaction score
0
Points
1
In which application did you make the spreadsheet? There are quite a few ways this could be done, and yes, it will involve repeating essentially the same steps as above however many times.

I have the data in Excel, however I could just as easily export it to tab delimited, csv, or some other format.

If you could please just show me what is code would look like for 3 images, for example, perhaps I could try to make it work for a much larger set...?
 
Joined
Dec 13, 2007
Messages
256
Reaction score
10
Points
18
Location
United States of America
Your Mac's Specs
2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork ‘09
Here's the code that will work just as well for three as for a thousand. :)
Code:
-- Set the input file's location; a file with one "URL <tab> filename" on each line is expected.
set tabDelimitedInputFile to choose file with prompt "Choose a file containing tab-separated URL-filename pairs."
-- Set the location to place the images that are downloaded.
set downloadFolder to the POSIX path of (choose folder with prompt "Choose a folder to save the download images to.")

-- Read in the text from the input file.
set theFile to (open for access tabDelimitedInputFile)
set fileContents to read theFile
close access theFile

-- Change the delimeters to tabs to separate the URL and filename on each line (save the old value).
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to tab

-- Repeat through each line of the file, downloading the image and naming it as specified.
repeat with i from 1 to the count of paragraphs in fileContents
	set thisLine to paragraph i of fileContents
	-- Extract the URL and image name.
	set lineItems to the text items of thisLine
	set imageURL to item 1 of lineItems
	set imageName to item 2 of lineItems
	-- Download the image and place it in the specified folder.
	do shell script "curl " & imageURL & " -o " & downloadFolder & imageName
end repeat

-- Reset the text item delimiters
set AppleScript's text item delimiters to oldDelimiters
Just export your spreadsheet as tab-delimited data in a text file and run the script. It assumes two things: first, that the data is formatted as one URL <tab> filename pair per line, and second, that the filename includes the file extension. If not, let me know and I can help work toward a dynamic solution (using the extension from the URL).

Oh, and here's the data I tested it with (just a few random images from Apple's homepage; of course, be sure to respect their copyright):
Code:
http://images.apple.com/home/images/promo_iphone3gs_20091218.jpg	iphone.jpg
http://images.apple.com/home/images/promo_macbook_20091020.jpg	macbook.jpg
http://images.apple.com/home/images/promo_ipodnano_20091123.jpg	ipodnano.jpg
 
OP
L
Joined
Jan 12, 2010
Messages
12
Reaction score
0
Points
1
nabl, you are awesome! thank you so much.

this does EXACTLY what i need it to do... :) :) :)

i tested it a couple times on a small batch of images and it worked great
 
Joined
Dec 13, 2007
Messages
256
Reaction score
10
Points
18
Location
United States of America
Your Mac's Specs
2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork ‘09
Glad I could help. :) I forgot to give the link before, but if you want to read more about text handling in AppleScript, I recommend this page. I think just about everything I used in the script is mentioned and better explained there.
 
OP
L
Joined
Jan 12, 2010
Messages
12
Reaction score
0
Points
1
I was able to work through a few errors ok, but I got stuck on this one error in particular. (this was the "result" after running script)

Code:
error "sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file" number 2

i presume this has something to do with a filename issue?
 
OP
L
Joined
Jan 12, 2010
Messages
12
Reaction score
0
Points
1
Nevermind! I solved this...

I went into the "replies" section and dug a little to find the problem...

I had removed some characters that were causing problems with filenames: "(", ")", and a couple other characters for example.

I had not removed a "'" character from a filename, which caused that error.
 
OP
L
Joined
Jan 12, 2010
Messages
12
Reaction score
0
Points
1
Glad I could help. :) I forgot to give the link before, but if you want to read more about text handling in AppleScript, I recommend this page. I think just about everything I used in the script is mentioned and better explained there.

thanks for the link. hopefully i can figure out how to do some more cool stuff with applescript. are there any tasks in particular you recommend it for?
 
Joined
Dec 13, 2007
Messages
256
Reaction score
10
Points
18
Location
United States of America
Your Mac's Specs
2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork ‘09
Basically anything you can think of that you do or will need to do more than a few times or has multiple repetitive steps. If you're seriously interested in automating your computer use, check out AppleScript 1-2-3. It's written by Apple's Sal Saghoian along with Bill Cheeseman, who probably know as much as anyone about it. From the excerpts I've read of the book, it seems to present the material very understandably and practically, so you'd probably come out of reading it with a bunch of good ideas. I don't own a copy yet, but I think I'm going to buy one sometime soon.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top