简体   繁体   English

Applescript读取.txt文件中的MP3 URL列表,然后下载所有按数字顺序重命名每个文件(1,2,3…)

[英]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 "). 我的桌面上有一个.txt文件,名为“ URLs.txt ”,其中包含URL列表(每行一个),扩展名为“ .mp3 ”(即“ 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"... 我需要下载所有音频文件,但同时保留“ URLs.txt”中列出的文件顺序...

Could someone please help with an applescript that will: 有人可以帮忙提供以下AppleScript:

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 ? 逐行阅读“ URLs.txt ”,下载每个音频文件,然后在下载到桌面上的“ URLs ”文件夹中时,以数字顺序(即时)重命名每个文件以保留文件列表顺序? For example: 例如:

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

...becomes... ...成为...

URLs Desktop folder URL桌面文件夹
1.mp3 1.mp3
2.mp3 2.mp3
3.mp3 3.mp3

I'm running Safari on a Mac and would like to run the script either via Automator or Script Menu . 我在Mac上运行Safari,并且想通过AutomatorScript 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. 这就是我使用Automator而不是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 然后从下拉菜单中选择“ URL”
  6. Under the Internet Library get "Download URLs" 在Internet Library下,获取“下载URL”
  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. 从最后一个问题来看,您似乎可以改用Shell脚本,然后将其放在Automator中。 In that case, the following Bash script would work: 在这种情况下,以下Bash脚本将起作用:

#!/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); mkdir -p创建文件夹(如果已经存在,则没有错误); n=1 sets up your counter for the filenames. n=1为文件名设置计数器。 Then while read mp3; do 然后while read mp3; do while read mp3; do loops over every line in the file, reading each one into the variable mp3; while read mp3; do循环遍历文件中的每一行,将每一行读入变量mp3; curl "$mp3" > ~/Desktop/URLs/$n.mp3 downloads the file at that address and stores it in the desired file. curl "$mp3" > ~/Desktop/URLs/$n.mp3在该地址下载文件并将其存储在所需文件中。 Then ((n++)) increments n by one; 然后((n++)) n加1; ((..)) 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. 最后, < ~/Desktop/URLs.txt告诉while循环假装其标准输入(它使用read检查的read )来自该文件。 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. 我的猜测是,这将比使用AppleScript更加干净,因为AppleScript的优势主要在于与其他应用程序进行互操作,而这并不是该任务。


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. 编辑:虽然该脚本可以从命令行正常运行,但是Automator施加了某种限制,导致其无法处理大量文件。 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. 我在Google搜寻中找不到任何解释或规避它的方法,所以我认为最好的选择是改用AppleScript。 1 It's longer, and I think you'll see why I used a bash script before, but here it is: 1它更长,我想您会明白为什么我以前使用bash脚本,但是这里是:

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. 我们首先将desktopPath设置为桌面路径(惊奇),因为我们将大量使用它。 The try - on error block attempts to create the URLs directory, ignoring the error if it already exists. try - on error块尝试创建URLs目录,而忽略该错误(如果已存在)。 We then initialize our counter, and read every paragraph (that is, line) in URLs.txt (on the desktop). 然后,我们初始化计数器,并读取URLs.txt (在桌面上)中的每个段落(即,行)。 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. 然后,我们对URL进行引用,以便可以在外壳程序中使用它(在其中,杂散的&;可能具有不希望的含义),并确保其中确实存在URL,而不是空引号。 (That can happen, for instance, if the file ends with a newline.) We then use curl to download the file, and increment n . (例如,如果文件以换行符结尾,则会发生这种情况。)然后,我们使用curl下载文件,并递增n If you think you'll have a lot of files, you might add a say "Done!" 如果您认为文件很多,则可以添加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. 您会注意到,我使用了do shell scriptcurl来下载该文件,而不是使用Safari。 Whenever I write AppleScripts that need to download a file, I prefer using curl for two reasons. 每当我编写需要下载文件的AppleScript时,出于两个原因,我更喜欢使用curl 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); 第一,我可以直接下载文件,而不是在Safari中打开文件然后保存(这也会在丑陋的AppleScript中反映出来,我不太确定怎么写); 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. 第二,您可能没有打开Safari,或者是因为您没有打开网页(不太可能,至少对我而言:-)),或者是因为您没有将Safari用作浏览器。 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. 2我也看到过对“ URL访问脚本”的引用,但是我无法确定是否仍然支持该URL,并且我从未使用过。 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. 由于我在Apple网站上找不到任何提及,因此我在文档方面犯了错误,而是改用curl


1: One way of doing this is to wrap the whole bash script in a do shell script . 1:一种方法是将整个bash脚本包装在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. 2:恰当的例子:自Jaguar或Panther以来,我就一直使用Safari,直到我本学期早些时候(我在一周之内切换到Google Chrome浏览器)时,我才发现自己没有切换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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