Results 1 to 14 of 14
Thread: AppleScript Help Needed
-
01-12-2010, 08:50 PM #1
- Member Since
- Jan 12, 2010
- Posts
- 12
AppleScript Help NeededI 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
-
01-13-2010, 04:40 PM #2
- Member Since
- Jan 12, 2010
- Posts
- 12
Any ideas for this? Any suggestions would be greatly appreciated!
-
01-13-2010, 05:15 PM #3
- Member Since
- Dec 13, 2007
- Location
- United States of America
- Posts
- 256
- 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
If you give a more specific account of what exactly you want to automate, I could help you work toward a much better AppleScript.
-
01-13-2010, 05:20 PM #4
- Member Since
- Jan 12, 2010
- Posts
- 12
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?
-
01-13-2010, 05:34 PM #5
- Member Since
- Dec 13, 2007
- Location
- United States of America
- Posts
- 256
- 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.
-
01-13-2010, 06:01 PM #6
- Member Since
- Jan 12, 2010
- Posts
- 12
-
01-14-2010, 04:14 PM #7
- Member Since
- Dec 13, 2007
- Location
- United States of America
- Posts
- 256
- 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
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
-
01-14-2010, 04:30 PM #8
- Member Since
- Jan 12, 2010
- Posts
- 12
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
-
01-14-2010, 04:44 PM #9
- Member Since
- Dec 13, 2007
- Location
- United States of America
- Posts
- 256
- 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.
-
01-14-2010, 04:45 PM #10
- Member Since
- Jan 12, 2010
- Posts
- 12
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
-
01-14-2010, 04:55 PM #11
- Member Since
- Jan 12, 2010
- Posts
- 12
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.
-
01-14-2010, 05:44 PM #12
- Member Since
- Jan 12, 2010
- Posts
- 12
-
01-14-2010, 07:51 PM #13
- Member Since
- Dec 13, 2007
- Location
- United States of America
- Posts
- 256
- 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.
-
01-14-2010, 07:53 PM #14
- Member Since
- Jan 12, 2010
- Posts
- 12
cool, thanks for the book recommendation...
--
My Mac: MacBook Intel Core 2 Duo 2.2 GHz 4GB RAM OS X 10.6 (Snow Leopard)
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
Is it possible to do this with Applescript ?
By xavierpointcom in forum macOS - Development and DarwinReplies: 0Last Post: 05-19-2011, 06:40 AM -
help needed to link up buttons on HUD Window to Applescript
By nick_harambee in forum macOS - Development and DarwinReplies: 0Last Post: 09-30-2010, 08:26 AM -
AppleScript Help Needed: Delete Watched TV Shows in iTunes
By AustinMatherne in forum macOS - Development and DarwinReplies: 2Last Post: 02-16-2010, 08:48 PM -
iTunes applescript aid needed
By nozomimomi in forum macOS - Apps and GamesReplies: 0Last Post: 06-06-2005, 03:35 PM -
Applescript Help Needed
By pphking in forum Apple DesktopsReplies: 2Last Post: 02-21-2005, 12:37 PM