Forums
New posts
Articles
Product Reviews
Policies
FAQ
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Menu
Log in
Register
Install the app
Install
Forums
Apple Computing Products:
macOS - Apps and Programs
AppleScript Help Needed
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="nabl" data-source="post: 980512" data-attributes="member: 43512"><p>Here's the code that will work just as well for three as for a thousand. <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite1" alt=":)" title="Smile :)" loading="lazy" data-shortname=":)" /></p><p>[code]-- Set the input file's location; a file with one "URL <tab> filename" on each line is expected.</p><p>set tabDelimitedInputFile to choose file with prompt "Choose a file containing tab-separated URL-filename pairs."</p><p>-- Set the location to place the images that are downloaded.</p><p>set downloadFolder to the POSIX path of (choose folder with prompt "Choose a folder to save the download images to.")</p><p></p><p>-- Read in the text from the input file.</p><p>set theFile to (open for access tabDelimitedInputFile)</p><p>set fileContents to read theFile</p><p>close access theFile</p><p></p><p>-- Change the delimeters to tabs to separate the URL and filename on each line (save the old value).</p><p>set oldDelimiters to AppleScript's text item delimiters</p><p>set AppleScript's text item delimiters to tab</p><p></p><p>-- Repeat through each line of the file, downloading the image and naming it as specified.</p><p>repeat with i from 1 to the count of paragraphs in fileContents</p><p> set thisLine to paragraph i of fileContents</p><p> -- Extract the URL and image name.</p><p> set lineItems to the text items of thisLine</p><p> set imageURL to item 1 of lineItems</p><p> set imageName to item 2 of lineItems</p><p> -- Download the image and place it in the specified folder.</p><p> do shell script "curl " & imageURL & " -o " & downloadFolder & imageName</p><p>end repeat</p><p></p><p>-- Reset the text item delimiters</p><p>set AppleScript's text item delimiters to oldDelimiters[/code]</p><p>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 <span style="font-family: 'Lucida Console'">URL <tab> filename</span> 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).</p><p></p><p>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):</p><p>[code]http://images.apple.com/home/images/promo_iphone3gs_20091218.jpg iphone.jpg</p><p>http://images.apple.com/home/images/promo_macbook_20091020.jpg macbook.jpg</p><p>http://images.apple.com/home/images/promo_ipodnano_20091123.jpg ipodnano.jpg[/code]</p></blockquote><p></p>
[QUOTE="nabl, post: 980512, member: 43512"] 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[/code] 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 [font="Lucida Console"]URL <tab> filename[/font] 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[/code] [/QUOTE]
Verification
Name this item. 🍎
Post reply
Forums
Apple Computing Products:
macOS - Apps and Programs
AppleScript Help Needed
Top