Results 1 to 14 of 14
  1. #1
    AppleScript Help Needed

    Member Since
    Jan 12, 2010
    Posts
    12
    AppleScript Help Needed
    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

  2. #2
    AppleScript Help Needed

    Member Since
    Jan 12, 2010
    Posts
    12
    Any ideas for this? Any suggestions would be greatly appreciated!

  3. #3
    AppleScript Help Needed
    nabl's Avatar
    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
    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.

  4. #4
    AppleScript Help Needed

    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?

  5. #5
    AppleScript Help Needed
    nabl's Avatar
    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.

  6. #6
    AppleScript Help Needed

    Member Since
    Jan 12, 2010
    Posts
    12
    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...?

  7. #7
    AppleScript Help Needed
    nabl's Avatar
    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
    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

  8. #8
    AppleScript Help Needed

    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

  9. #9
    AppleScript Help Needed
    nabl's Avatar
    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.

  10. #10
    AppleScript Help Needed

    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
    i presume this has something to do with a filename issue?

  11. #11
    AppleScript Help Needed

    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.

  12. #12
    AppleScript Help Needed

    Member Since
    Jan 12, 2010
    Posts
    12
    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?

  13. #13
    AppleScript Help Needed
    nabl's Avatar
    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.

  14. #14
    AppleScript Help Needed

    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

  1. Is it possible to do this with Applescript ?
    By xavierpointcom in forum OS X - Development and Darwin
    Replies: 0
    Last Post: 05-19-2011, 06:40 AM
  2. help needed to link up buttons on HUD Window to Applescript
    By nick_harambee in forum OS X - Development and Darwin
    Replies: 0
    Last Post: 09-30-2010, 08:26 AM
  3. AppleScript Help Needed: Delete Watched TV Shows in iTunes
    By AustinMatherne in forum OS X - Development and Darwin
    Replies: 2
    Last Post: 02-16-2010, 08:48 PM
  4. iTunes applescript aid needed
    By nozomimomi in forum OS X - Apps and Games
    Replies: 0
    Last Post: 06-06-2005, 03:35 PM
  5. Applescript Help Needed
    By pphking in forum Apple Desktops
    Replies: 2
    Last Post: 02-21-2005, 12:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •