在Automator中使用数组变量作为文件名输入

时间:2013-09-16 10:26:38

标签: automator

我正在尝试创建一个工作流,使用Instapaper将URL列表转换为纯文本,然后将文本保存在我的计算机上的文本文档中。

到目前为止,我已经能够获取URL列表,获取每个网页的标题,并将URL转换为纯文本。

我有一个变量“文章标题”中保存的标题列表。然后,每篇文章的纯文本从“从网页获取文本”传递到“新文本文件”

我尝试将“文章标题”变量放在“新建文本文件”操作的“另存为”输入中,但是没有生成任何文件(与我在“另存为”字段中输入通用标题时不同。但是,所有生成的文件名称相同)。我怀疑我不能使用包含数组的变量作为另存为输入。但我希望每个新文件都有相应的名称。

如何让动作迭代标题数组,以便“从网页获取文本”中的每个纯文本项目都与“文章标题”变量中的标题一起保存?

1 个答案:

答案 0 :(得分:2)

让许多人感到沮丧的一件事就是当您想要将多个变量传递给某个操作时遇到的问题。有很多方法可以保存到外部脚本。

但是在这种情况下,一个简单的Applescript与bit of script @adayzdone混合在一起,你会得到你想要的东西。

您只需将URL列表传递给此“运行Applescript”

即可
  on run {input, parameters}
    set docPath to POSIX path of (path to documents folder)

    repeat with i from 1 to count of items of input
        set this_item to item i of input
        set thePage to (do shell script "curl " & quoted form of this_item)
        set theTitle to docPath & "/" & (do shell script "echo " & quoted form of thePage & " | grep -o \\<title\\>.*\\</title\\> | sed -E 's/<\\/?title>//g'")
        set t_text to (do shell script "echo " & quoted form of thePage & "|textutil -format html -convert txt -stdin -output \"" & theTitle & ".txt\"")

    end repeat

end run

enter image description here


** 将文字传递给下一个操作的更新。 **


这将传递来自所有URL的文本内容列表。

它仍将执行上述操作,但现在会将所有URL中的文本内容列表传递给下一个操作。

我已经使用“文本到语音”对其进行了测试,它会读取多个文本内容。

on run {input, parameters}
    set docPath to POSIX path of (path to documents folder)
    set bigList to {}
    repeat with i from 1 to count of items of input
        set this_item to item i of input
        set thePage to (do shell script "curl " & quoted form of this_item)
        set theTitle to docPath & "/" & (do shell script "echo " & quoted form of thePage & " | grep -o \\<title\\>.*\\</title\\> | sed -E 's/<\\/?title>//g'")
        set t_text to (do shell script "echo " & quoted form of thePage & "|textutil -format html -convert txt -stdin -output \"" & theTitle & ".txt\"")
        set t_text_for_action to (do shell script "echo " & quoted form of thePage & "|textutil -format html -convert txt -stdin -stdout")
        copy t_text_for_action to end of bigList
    end repeat
    return bigList --> text list can now be passed to the next action
end run

如果你想测试:我可以建议一个页面上有少量文字的页面如:http://www.javascripter.net/


更新2 - 使用unix命令'say'将文本保存到音频文件。

好的,这里有几件事。

1,由于同样的原因,我在前面的编码中将所有内容保存在一个脚本中。我在这里做了同样的事。即将文本对象和标题一起传递给下一个动作即使不是不可能也会很痛苦。

2,该脚本使用unix命令及其输出选项将文本另存为aiff文件。 它还按标题命名文件。

3, 我有一个问题,而不是保存文件,它开始说文本。 ??? 结果发现,我正在测试的网址(http://www.javascripter.net)上有一个标题标签。所以脚本的@adayzdone grep sed 部分返回“”。它抛出了命令。

我通过使用 grep 命令中的 -i (忽略大小写)选项并使用“|”修复此问题。 (或) sed 中的选项,并添加表达式的大写版本。

4, 返回的标题中还包含其他字符,由于未添加扩展名,系统会将文件保存为可识别文件。

这由一个简单的处理程序修复,该处理程序返回带有允许字符的标题文本。

6,

有效。

on run {input, parameters}
    set docPath to POSIX path of (path to documents folder)
    repeat with i from 1 to count of items of input
        set this_item to item i of input
        set thePage to (do shell script "curl -A \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30\" " & quoted form of this_item)
        set theTitle to replaceBadChars((do shell script "echo " & quoted form of thePage & " | grep -io \\<title\\>.*\\</title\\> | sed -E 's/<\\/?title>|<\\/?TITLE>//g'"))
        set t_text_for_action to (do shell script "echo " & quoted form of thePage & "|textutil -format html -convert txt -stdin -stdout")
        do shell script "cd  " & quoted form of docPath & " ;say -o  \"" & theTitle & "\" , " & quoted form of t_text_for_action
    end repeat
end run

on replaceBadChars(TEXT_)
    log TEXT_
    set OkChars to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "_", space}
    set TEXT_ to characters of TEXT_
    repeat with i from 1 to count of items in TEXT_
        set this_char to item i of TEXT_
        if this_char is not in OkChars then
            set item i of TEXT_ to "_"
        else

        end if
    end repeat
    set TEXT_ to TEXT_ as string

    do shell script " echo " & quoted form of TEXT_
end replaceBadChars