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

发布于 2024-10-07 08:48:18 字数 779 浏览 9 评论 0原文

我的桌面上有一个名为“URLs.txt”的 .txt 文件,其中包含带有“.mp3”的 URL 列表(每行一个) >”扩展名(即“http://www.example.com/path/number.mp3”)。我需要下载所有音频文件,但同时保留“URLs.txt”中列出的文件的顺序...

有人可以帮助编写一个 applescript 来:

读取“URLs.txt” ” 逐行下载每个音频文件,然后按数字顺序(即时)重命名每个文件,以在下载到桌面<上的“URLs”文件夹时保留文件列表顺序/强>?例如:

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

...变成...

URL 桌面文件夹
1.mp3
2.mp3
3.mp3

我正在 Mac 上运行 Safari,并且希望通过Automator脚本菜单运行脚本。

非常感谢任何帮助!

谢谢,
戴夫

I have a .txt file on my Desktop called "URLs.txt" that contains a list of URLs (one per line) with ".mp3" extension (i.e. "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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光是把杀猪刀 2024-10-14 08:48:18

我知道这是一个旧线程,但我认为我最近的发现可能会对其他正在寻找与我相同的东西的人有所帮助。这就是我使用 Automator 而不是 AppleScript 想到的。

  1. 打开 Automator
  2. 创建一个工作流程文档
  3. 在文本库下抓取“获取指定文本”并将其拖入您的工作流程
  4. 然后拖入“从文本中提取数据”
  5. 然后从下拉列表中选择“URL”
  6. 在 Internet 库下获取“下载 URL”
  7. 粘贴您的将 URL 列表放入第一个框中,然后点击右上角的“运行”

这就是您的工作流程应如下所示:

enter image此处描述

希望这对某人有用。

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:

enter image description here

Hopefully that can be of use to someone.

早乙女 2024-10-14 08:48:18

从你的上一个问题来看,看起来你可以使用 shell 脚本来代替,然后将它放在 Automator 中。在这种情况下,以下 Bash 脚本将起作用:

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

mkdir -p 创建文件夹(如果该文件夹已存在,则不会出错); n=1 设置文件名计数器。然后同时读取mp3; do 循环遍历文件中的每一行,将每一行读入变量 mp3; 卷曲“$mp3”> ~/Desktop/URLs/$n.mp3 下载该地址的文件并将其存储在所需的文件中。然后 ((n++))n 加一; ((..)) 标记数学模式,++ 是自增运算符。最后,< ~/Desktop/URLs.txt 告诉 while 循环假装其标准输入(它使用 read 检查的内容)来自该文件。我的猜测是,这比使用 AppleScript 更清晰,因为 AppleScript 的优势主要在于与其他应用程序的互操作,而本任务与此无关。


编辑:虽然该脚本在命令行中运行良好,但 Automator 施加了某种限制,导致它无法完成大量文件。我在谷歌搜索中找不到任何解释或规避它的方法,所以我认为你最好的选择是使用 AppleScript。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

如您所见,它更庞大,但基本相同。我们首先将 desktopPath 设置为桌面的路径(令人惊讶),因为我们将经常使用它。 try-on error 块尝试创建 URLs 目录,如果错误已存在,则忽略该错误。然后,我们初始化计数器,并读取 URLs.txt(在桌面上)中的每个段落(即行)。然后,我们引用该 URL,以便可以在 shell 中使用它(其中杂散的 &; 可能具有不需要的含义),并确保确实存在一个 URL在那里,而不是空引号。 (例如,如果文件以换行符结尾,则可能会发生这种情况。)然后,我们使用 curl 下载文件,并递增 n。如果您认为您将有很多文件,您可以在脚本末尾添加一个 say“Done!” 行,以便您知道它何时完成。

您会注意到,我使用了 do shell scriptcurl 来下载该文件,而不是调用 Safari。每当我编写需要下载文件的 AppleScript 时,我更喜欢使用 curl,原因有两个。第一,我可以直接下载文件,而不是在 Safari 中打开它然后保存它(这也会反映在丑陋的 AppleScript 中,我不确定如何编写);第二,您可能没有打开 Safari,要么是因为您没有打开网页(不太可能,至少对我来说:-)),要么是因为您不使用 Safari 作为浏览器。当然,现在,但你永远不知道什么时候你可能会改变主意。2我还看到过对“URL 访问脚本”的引用,但我可以不知道是否仍然支持,而且我从未使用过它。由于我在 Apple 网站上找不到任何提及它的内容,因此我在文档方面犯了错误,并使用了 curl 代替。


1: 一种方法是将整个 bash 脚本包装在 do shell 脚本 中。这确实可行,但有点愚蠢。不过,如果你愿意的话,你也可以这样做:-)

2: 举个例子:我从 Jaguar 或 Panther 开始就使用 Safari,并且没有发现自己会切换——直到本学期早些时候,当我一周内就切换到了 Google Chrome。

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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文