New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus • Advice and insight from world-class Apple enthusiasts • Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Apps and Games Discussion of applications and games available for Mac OS X.

AppleScript Help Needed


Post Reply New Thread Subscribe

 
Thread Tools
lovinthemac

 
Member Since: Jan 12, 2010
Posts: 12
lovinthemac is on a distinguished road

lovinthemac is offline
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
QUOTE Thanks
lovinthemac

 
Member Since: Jan 12, 2010
Posts: 12
lovinthemac is on a distinguished road

lovinthemac is offline
Any ideas for this? Any suggestions would be greatly appreciated!
QUOTE Thanks
nabl

 
nabl's Avatar
 
Member Since: Dec 13, 2007
Location: United States of America
Posts: 256
nabl has a spectacular aura about
Mac Specs: 2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork ‘09

nabl is offline
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.
QUOTE Thanks
lovinthemac

 
Member Since: Jan 12, 2010
Posts: 12
lovinthemac is on a distinguished road

lovinthemac is offline
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?
QUOTE Thanks
nabl

 
nabl's Avatar
 
Member Since: Dec 13, 2007
Location: United States of America
Posts: 256
nabl has a spectacular aura about
Mac Specs: 2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork ‘09

nabl is offline
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.
QUOTE Thanks
lovinthemac

 
Member Since: Jan 12, 2010
Posts: 12
lovinthemac is on a distinguished road

lovinthemac is offline
Quote:
Originally Posted by nabl View Post
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...?
QUOTE Thanks
nabl

 
nabl's Avatar
 
Member Since: Dec 13, 2007
Location: United States of America
Posts: 256
nabl has a spectacular aura about
Mac Specs: 2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork ‘09

nabl is offline
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
QUOTE Thanks
lovinthemac

 
Member Since: Jan 12, 2010
Posts: 12
lovinthemac is on a distinguished road

lovinthemac is offline
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
QUOTE Thanks
nabl

 
nabl's Avatar
 
Member Since: Dec 13, 2007
Location: United States of America
Posts: 256
nabl has a spectacular aura about
Mac Specs: 2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork ‘09

nabl is offline
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.
QUOTE Thanks
lovinthemac

 
Member Since: Jan 12, 2010
Posts: 12
lovinthemac is on a distinguished road

lovinthemac is offline
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?
QUOTE Thanks
lovinthemac

 
Member Since: Jan 12, 2010
Posts: 12
lovinthemac is on a distinguished road

lovinthemac is offline
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.
QUOTE Thanks
lovinthemac

 
Member Since: Jan 12, 2010
Posts: 12
lovinthemac is on a distinguished road

lovinthemac is offline
Quote:
Originally Posted by nabl View Post
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?
QUOTE Thanks
nabl

 
nabl's Avatar
 
Member Since: Dec 13, 2007
Location: United States of America
Posts: 256
nabl has a spectacular aura about
Mac Specs: 2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork ‘09

nabl is offline
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.
QUOTE Thanks
lovinthemac

 
Member Since: Jan 12, 2010
Posts: 12
lovinthemac is on a distinguished road

lovinthemac is offline
cool, thanks for the book recommendation...

--
My Mac: MacBook Intel Core 2 Duo 2.2 GHz 4GB RAM OS X 10.6 (Snow Leopard)
QUOTE Thanks

Post Reply New Thread Subscribe


« Transmission Problem | How to search files in FINDER for multiple words »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
newbie question on XCode 3.2: how to populate pop up buttons/link to applescript nickharambee OS X - Operating System 0 10-21-2009 07:10 AM
Logic Express and Applescript via GUI Scripting albertaurus Music, Audio, and Podcasting 0 04-09-2009 01:13 PM
iTunes applescript aid needed nozomimomi OS X - Apps and Games 0 06-06-2005 03:35 PM
Applescript Help Needed pphking Apple Desktops 2 02-21-2005 12:37 PM
[ANN] New comprehensive AppleScript book hanaan OS X - Development and Darwin 0 11-06-2004 07:28 PM

All times are GMT -4. The time now is 06:52 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?