Automator – Save Images from URLs in Text File

automatorgraphicsmacoswebsites

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?

Best Answer

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)

enter image description here

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...

enter image description here