1

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?

1

2 Answers 2

3

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

4
  • 1
    Your answer is way more formal than mine, but I enjoy using creativity to achieve my goals. Anyway kudos for being well educated. Commented Nov 15, 2018 at 1:46
  • Elegant. Works like a charm.
    – Linda
    Commented Nov 16, 2018 at 3:48
  • I tried your script above, and I'm getting this error: curl: (3) [globbing] unmatched brace in column 1. Not sure what that means. How can I fix it? For sake of simplicity, I created a folder on my desktop named Art and a file named Image list.txt with one URL per line. Thanks in advance! Commented Dec 2, 2018 at 14:43
  • @ notyourguru, What is the exact command line that is throwing the error? You might want to post a copy of the actual script you're using to, e.g. https://paste.ee or https://pastebin.com, so we can see exactly what you're working with to help diagnose the error. Commented Dec 2, 2018 at 17:54
-1

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:

enter image description here

2 - Open Automator, create a new workflow and drag a Run AppleScript action:

enter image description here

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

enter image description here

4 - Place an "EXIT" in the last line of the document so the program will now when the job is done:

enter image description here

5 - Run the workflow

I've tested out and it works fine.

3
  • 1
    Aside from the fact that this particular type of coding is extremely susceptible to breakage while running, UI Scripting in general should only be used as the last resort when no better or more proper way exists! The problem with your solution is it has to be a modal process, meaning once it's started the user must not touch anything else on the system until it's done, no multitasking allowed. Unlike a simple bash script (which this really should be) or wch1zpink's answer, it could/would be running as a background process, allowing multitasking while the images are being downloaded! Commented Nov 15, 2018 at 15:56
  • 1
    While this may have worked for you in your limited testing, I can assure you it easily breaks with a long list of files. Commented Nov 15, 2018 at 15:57
  • @user3439894 I agree. However I provided a decent solution in a short time. This is a one-time task, not an application. Commented Nov 15, 2018 at 19:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .