Applescript 读取 .txt 文件中的 MP3 URL 列表,然后下载所有文件并按数字顺序重命名每个文件(1,2,3...)
我的桌面上有一个名为“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道这是一个旧线程,但我认为我最近的发现可能会对其他正在寻找与我相同的东西的人有所帮助。这就是我使用 Automator 而不是 AppleScript 想到的。
这就是您的工作流程应如下所示:
希望这对某人有用。
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.
This is what your Workflow should look like:
Hopefully that can be of use to someone.
从你的上一个问题来看,看起来你可以使用 shell 脚本来代替,然后将它放在 Automator 中。在这种情况下,以下 Bash 脚本将起作用:
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 脚本,但这里是:
如您所见,它更庞大,但基本相同。我们首先将
desktopPath
设置为桌面的路径(令人惊讶),因为我们将经常使用它。try
-on error
块尝试创建URLs
目录,如果错误已存在,则忽略该错误。然后,我们初始化计数器,并读取URLs.txt
(在桌面上)中的每个段落(即行)。然后,我们引用该 URL,以便可以在 shell 中使用它(其中杂散的&
或;
可能具有不需要的含义),并确保确实存在一个 URL在那里,而不是空引号。 (例如,如果文件以换行符结尾,则可能会发生这种情况。)然后,我们使用curl
下载文件,并递增n
。如果您认为您将有很多文件,您可以在脚本末尾添加一个say“Done!”
行,以便您知道它何时完成。您会注意到,我使用了
do shell script
和curl
来下载该文件,而不是调用 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:
The
mkdir -p
creates the folder (without erroring if it already exists);n=1
sets up your counter for the filenames. Thenwhile 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++))
incrementsn
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 withread
) 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:
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. Thetry
-on error
block attempts to create theURLs
directory, ignoring the error if it already exists. We then initialize our counter, and read every paragraph (that is, line) inURLs.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 usecurl
to download the file, and incrementn
. If you think you'll have a lot of files, you might add asay "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
andcurl
to download that a file, rather than calling out to Safari. Whenever I write AppleScripts that need to download a file, I prefer usingcurl
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 withcurl
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.