Automator: Save images from a list of URLs in a text file
سؤال
I have a text file with a list of URLs of images from a website. I would like to download them to a folder named Art on my computer.
I have tried Get Contents of TextEdit Document and then Extract URLs from Text, but then I don't understand how to parse each URL and save the image before moving to the next URL.
How can I batch download several images from their URLs?
المحلول
Let's assume for a second that your image URL's are in a text file located on your desktop... "Image list.txt"
Let's assume each image URL in that file is on a separate line
Let's assume that the "Art" folder is located on your desktop (folder for the downloaded images)
This AppleScript code is all you need
set theList to (path to desktop as text) & "Image list.txt"
set artFolder to (path to desktop as text) & "Art"
set artFolder to quoted form of POSIX path of artFolder
set theImages to read alias theList as list using delimiter linefeed -- get the lines of a file as a list
repeat with i from 1 to count of theImages
set thisItem to item i of theImages
do shell script "cd " & artFolder & "; " & "curl -O " & quoted form of thisItem
end repeat
Actually, here is an even better solution. Save this following AppleScript code in Script Editor.app as an application. Now you will have two options.
Double clicking on the app in Finder will open a dialog asking you to choose the text file containing the image URLs, then will proceed to download the images.
OR
You can drag the text file containing the image URLs directly onto the app’s icon, in Finder, which will then go ahead and process and download the images in that text file. (AKA Droplet)
on open theFiles
-- Handle the case where the script is launched by dropping
-- a .txt file, containing image URLs, directly onto this app's icon
set artFolder to (path to desktop as text) & "Art"
set artFolder to quoted form of POSIX path of artFolder
set theImages to read alias theFiles as list using delimiter linefeed
repeat with i from 1 to count of theImages
set thisItem to item i of theImages
do shell script "cd " & artFolder & "; " & "curl -O " & quoted form of thisItem
end repeat
end open
on run
-- Handle the case where the script is launched without any dropped files
set theList to (choose file with prompt ¬
"Choose Your Text File Containing Image URLs" of type {"txt"} ¬
default location (path to desktop) ¬
invisibles false ¬
without multiple selections allowed) as text
set artFolder to (path to desktop as text) & "Art"
set artFolder to quoted form of POSIX path of artFolder
set theImages to read alias theList as list using delimiter linefeed
repeat with i from 1 to count of theImages
set thisItem to item i of theImages
do shell script "cd " & artFolder & "; " & "curl -O " & quoted form of thisItem
end repeat
end run
Here is a visual of the droplet in action...
نصائح أخرى
I'm assuming you have a text file with one URL per line. That is important for the following workflow. I'm also assuming you have pure jpg/png URLs, linking direct to the images and not html's URL pages with images inside.
I'll address Safari because chances are high that the browser is installed in your system, but other browsers should work the same way.
1 - Open Safari and go to preferences, general, and set your default File Download Location to your ART folder:
2 - Open Automator, create a new workflow and drag a Run AppleScript action:
3 - Paste the following code:
on run
set the clipboard to {}
tell application "TextEdit" to activate
tell application "System Events"
key code 126 using command down -- go to the start of the doc (command+up)
delay 0.1
key code 124 using {command down, shift down} -- select the first line (commmand+shift+right arrow)
delay 0.1
key code 8 using command down -- copy URL (command+C)
delay 0.1
end tell
repeat while (the clipboard) is not "EXIT"
tell application "Safari" to activate
tell application "System Events"
key code 37 using command down -- Open Location (Command+L)
delay 0.1
key code 9 using command down -- paste URL (Command+V)
delay 0.1
key code 36 -- enter
delay 3 -- three seconds to begin loading the image, adjust if necessary
key code 1 using command down -- Save (command+S)
delay 0.1
key code 36 -- enter
repeat while exists window "Save" of application process "Safari"
delay 0.5 -- wait the save to end
end repeat
end tell
tell application "TextEdit" to activate -- back to textEdit
tell application "System Events"
key code 125 -- go to next line
delay 0.1
key code 123 using command down -- go to the start of the line (command + left key)
delay 0.1
key code 124 using {command down, shift down} -- select the line (commmand+shift+right arrow)
delay 0.1
key code 8 using command down -- copy URL (Command+C)
delay 0.1
end tell
end repeat
end run
4 - Place an "EXIT" in the last line of the document so the program will now when the job is done:
5 - Run the workflow
I've tested out and it works fine.