Your best friend for file transfer.

Fetch application logoFetch

AppleScript to Download From a List of URLs (5 posts)

  • Started 8 years ago by Gregory
  • Latest reply 8 years ago from Gregory
  • Gregory Member

    Hello everyone,

    I'm new to AppleScript. How would I write a script that tells Fetch to download from a list of URLS that are contained in a text file on my Mac? I'd like the downloaded files to be placed in a specific folder.

    Regards,

    Gregory

    Posted 8 years ago #

  • Jim Matthews Administrator

    The AppleScript syntax for downloading a URL to a specific folder looks like:

    tell application "Fetch"
    duplicate remote item aURL to beginning of alias "Macintosh HD:Users:joeuser:Downloads:Fetch Downloads:"
    end tell

    Where aURL is the URL of the file to download, and "Macintosh HD:Users:joeuser:Downloads:Fetch Downloads:" is the path to local folder where you want the file downloaded.

    Do you need help getting the URLs out of the text file?

    Thanks,

    Jim Matthews
    Fetch Softworks

    Posted 8 years ago #

  • Gregory Member

    Hi Jim,

    Thank you for the tip. It will take a little time for me to get used to AppleScript's syntax. It is like HyperCard but not quite. As a long-time HyperCard and LiveCode scripter (the modern incarnation of HyperCard), I did not expect to see the word "alias" in your code (I will look that up in the AppleScript guide), but was more surprised to see the use of "beginning" because I would have expected the file's name to be extracted from the URL and then "appended" to or placed "after" the path to the local downloads folder.

    I would appreciate a little help with getting the URLs out of the text file. Being able to study a more complete example will help me learn more quickly. My text file is comma-delimited and of the form:

    description of file 1, date file 1 created, file 1 URL
    description of file 2, date file 2 created, file 2 URL
    description of file 3, date file 3 created, file 3 URL

    and so on. I guess I would be pulling out the URL that is in the third item of each line and using in a repeat loop. I might also pull out the first and second items to combine them into unique filenames to save the downloaded files.

    Thanks once again,

    Gregory

    Posted 8 years ago #

  • Jim Matthews Administrator

    Hi,

    The duplicate command is a bit funny in its use of that "beginning of" syntax. A simpler alternative would be:

    download remote item aURL to alias "Macintosh HD:Users:joeuser:Downloads:Fetch Downloads:"

    Here's code that will pull URLs out of a text file:

    set fileLocation to choose file
    set fileRef to open for access fileLocation
    set fileContents to read fileRef
    close access fileRef

    set textLines to every paragraph of fileContents
    repeat with currentLine in textLines
    set lineWords to every word of currentLine
    if (count of lineWords) > 2 then
    set currentURL to last item of lastWords
    display dialog "Here is a URL: " & currentURL
    end if
    end repeat

    You'd want to replace that display dialog line with code to do something with the URL.

    I hope that helps,

    Jim Matthews
    Fetch Softworks

    Posted 8 years ago #

  • Gregory Member

    Thanks, Jim!

    I can wrap my head around this. Will give it a whirl. My problem with downloading public documents programmatically, using LiveCode or Mathematica, from the Security and Exchange Commission's (SEC) public FTP site has been that after a few hundred or few thousand files (it varies), the downloads stop coming. I cannot really tell if this is because of a session time-out or because the SEC's server does not like robotic downloads. I'm hoping that the SEC will view requests from Fetch more hospitably.

    Gregory

    Posted 8 years ago #

Reply

  • Or nickname, if you prefer.
  • This will be kept confidential.
  • This is to ensure that you’re a person, not a spambot.