简体   繁体   中英

Applescript to read list of MP3 URLs in .txt file, then download all renaming each file in numerical order (1,2,3…)

I have a .txt file on my Desktop called " URLs.txt " that contains a list of URLs (one per line) with " .mp3 " extension (ie " http://www.example.com/path/number.mp3 "). I need to download all audio files, but at the same time retain the order of files listed in "URLs.txt"...

Could someone please help with an applescript that will:

Read " URLs.txt " line-by-line, download each audio file, but then rename each file in numerical order (on the fly) to retain file list order when downloaded into " URLs " folder on Desktop ? For example:

URLs.txt
http://... 34566.mp3
http://... 234.mp3
http://... 126567.mp3

...becomes...

URLs Desktop folder
1.mp3
2.mp3
3.mp3

I'm running Safari on a Mac and would like to run the script either via Automator or Script Menu .

Any help greatly appreciated!

Thanks,
Dave

I know this is an old thread, but I thought my recent findings might be helpful to someone else looking for the same thing that landed me here. This is what I came up with using Automator instead of AppleScript.

  1. Open Automator
  2. Create a Workflow document
  3. Under the Text Library grab "Get Specified Text" and drag it into your workflow
  4. Then drag in "Extract Data from Text"
  5. Then choose "URLs" from the dropdown
  6. Under the Internet Library get "Download URLs"
  7. Paste your list of URLs into the first box and hit "Run" in he top right corner

This is what your Workflow should look like:

在此处输入图片说明

Hopefully that can be of use to someone.

Judging from your last question, it looks like you could use a shell script instead, and just place it in Automator. In that case, the following Bash script would work:

#!/bin/bash
mkdir -p ~/Desktop/URLs
n=1
while read mp3; do
  curl "$mp3" > ~/Desktop/URLs/$n.mp3
  ((n++))
done < ~/Desktop/URLs.txt

The mkdir -p creates the folder (without erroring if it already exists); n=1 sets up your counter for the filenames. Then while read mp3; do while read mp3; do loops over every line in the file, reading each one into the variable mp3; curl "$mp3" > ~/Desktop/URLs/$n.mp3 downloads the file at that address and stores it in the desired file. Then ((n++)) increments n by one; ((..)) mark off math mode, and ++ is the self-increment operator. Finally, the < ~/Desktop/URLs.txt tells the while loop to pretend its standard input (what it's checking with read ) is from that file. My guess is that this'll be cleaner than using AppleScript, since AppleScript's strengths mainly run towards interoperating with other applications, which this task isn't about.


Edit: While that script works fine from the command line, Automator imposes some sort of restriction which causes it to not finish with large numbers of files. I couldn't find any explanation or a way to circumvent it in my Googling, so I think your best bet is to use AppleScript instead. 1 It's longer, and I think you'll see why I used a bash script before, but here it is:

property desktopPath : path to desktop as string

try
    tell application "Finder" to ¬
        make folder at (path to desktop) with properties {name:"URLs"}
on error number -48
    -- The folder already exists
end try

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

As you can see, it's bulkier, but it's basically the same. We first set desktopPath to be the path to the desktop (surprise), since we'll be using it a lot. The try - on error block attempts to create the URLs directory, ignoring the error if it already exists. We then initialize our counter, and read every paragraph (that is, line) in URLs.txt (on the desktop). We then quote the URL so we can use it in the shell (where a stray & or ; could have an undesired meaning), and make sure that there's actually a URL there, and not empty quotes. (That can happen, for instance, if the file ends with a newline.) We then use curl to download the file, and increment n . If you think you'll have a lot of files, you might add a say "Done!" line to the end of the script, so you'll know when it's done.

You'll notice that I used do shell script and curl to download that a file, rather than calling out to Safari. Whenever I write AppleScripts that need to download a file, I prefer using curl for two reasons. One, I can download the file directly, rather than opening it in Safari and then saving it (which would also reflect itself in ugly AppleScript that I'm not 100% sure how to write); two, you might not have Safari open, either because you have no webpages open (unlikely, at least for me :-)), or because you don't use Safari as a browser. Sure, you do now , but you never know when you might change your mind. 2 I've also seen reference to something called "URL Access Scripting", but I can't tell if that's still supported or not, and I've never used it. Since I can't find any mention of it on Apple's website, I erred on the side of documentation and went with curl instead.


1: One way of doing this is to wrap the whole bash script in a do shell script . That'd work, but it'd be sort of stupid. You could do it if you wanted, though :-)

2: Case in point: I had used Safari since either Jaguar or Panther, and didn't see myself switching—until earlier this semester, when I switched over to Google Chrome in the space of one week.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM