使用 Automator 将任何 .txt 文件传递​​到现有的 AppleScript

Pass any .txt file to existing AppleScript using Automator

我目前有一个 AppleScript,它可以将包含长列表的硬编码纯文本文件读入一个变量,然后将其作为段落读入另一个变量。该脚本非常适合我的目的。这基本上就是我正在使用的:

set fileHandler to (read POSIX file "/path/to/my/file.txt")
set newList to paragraphs of fileHandler

repeat with i in newList
     # do stuff
end repeat

省略的脚本打开 Safari 位置,使用 i 作为该页面上的变量调用 JavaScript,将结果写入新的纯文本文件,关闭 Safari window ,然后重复。它一直持续到到达列表的末尾,然后在 repeat 运行 之外的 do shell script 稍微清理了新的文本文件。

问题是每次我想 运行 使用不同列表的脚本时,我都必须打开硬编码文件并粘贴到列表中。我宁愿将任何 .txt 文件放到环绕我当前脚本的 Automator 应用程序中。

我已经尝试使用 "Combine Text Files",我觉得我已经很接近了,但是我不能完全按照我喜欢的方式传递 .txt 文件的内容。我不能同时通过它,除非我做错了什么。 "Get Value of Variable"/"Set Value of Variable" 在我的列表中添加了一个额外的 "Text" 项目,我不明白。

理想情况下,我想做这样的事情:

set fileHandler to (read POSIX file arg) -- the dropped text file
set newList to paragraphs of fileHandler

repeat with i in newList
     # do stuff
end repeat

...但不幸的是,它不是那样工作的。

如果可以的话,我真的不想重新发明一个全新的脚本。任何建议都会很棒。谢谢。

实际上不需要Automator。在脚本编辑器中将此代码保存为应用程序(包)并将文件拖放到其中。

on open theFiles
  repeat with aFile in theFiles
    set newList to paragraphs of (read aFile)

    repeat with i in newList
        # do stuff
    end repeat
  end repeat
end open