Search code examples
applescript

AppleScript: Download URL from file and keep the names


I want to create a simple AppleScript:

  • define a folder (that will get all the images, docs etc.)
  • read a file (.txt for example)
  • download all the URLs into the folder with their original name

Currently, my file has one URL by line, no separator.

I found several scripts but they always do something I don't want, don't need or doesn't work.

I found one that is very close to what I want, but it's editing the names in the loop. I didn't succeed to split the URL and keep the last part. Here it is:

property desktopPath : path to desktop as string

set n to 1
repeat with urlLine in paragraphs of (read alias (desktopPath & "filename.txt"))
    set qURL to quoted form of urlLine
    if qURL ≠ "''" then
        set dest to quoted form of ¬
            (POSIX path of desktopPath & "URLs/" & n & ".jpg")
        do shell script "curl " & quoted form of urlLine & " > " & dest
        set n to n + 1
    end if
end repeat

If you have any other solution, I take.

Thanks in advance


Solution

  • I found a script that is working fine.

    You just need to be sure about the txt file: the list inside should not have any text format. Weirdly I have to do a copy/paste through a text editor to remove every text format.

    1/ Put the txt file on your desktop, 2/ Create a "download" folder on your desktop, 3/ Start the script

    set theList to (path to desktop as text) & "Download.txt" -- File name with the URLs
    set theFolder to (path to desktop as text) & "download" -- Folder name between quotes
    set theFolder to quoted form of POSIX path of theFolder 
    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 " & theFolder & "; " & "curl -O " & quoted form of thisItem
    end repeat