我正在尝试创建一个工作流,以使用Instapaper将URL列表转换为纯文本,然后将文本保存在计算机上的文本文档中。
到目前为止,我已经能够获取URL列表,获取每个网页的标题并将URL转换为纯文本。
我将标题列表保存在变量“ Article Titles”中。然后将每篇文章的纯文本从“从网页获取文本”传递到“新文本文件”
我尝试将“文章标题”变量放入“新文本文件”操作的“另存为”输入中,但是没有生成任何文件(不同于我只是在“另存为”字段中输入通用标题的情况。但是,所有生成的文件是相同的名字)。我怀疑我不能使用包含数组的变量作为另存为输入。但我希望每个新文件都有其各自的名称。
我该如何遍历标题数组,以便将“从网页获取文本”中的每个纯文本项与“文章标题”变量中的标题一起保存?
使许多人沮丧的一件事是,当您想将多个变量传递给一个动作时遇到的问题。有多种解决方法,例如保存到外部脚本。
但是在这种情况下,一个简单的Applescript加上脚本@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
**更新以将文本传递到下一个动作。**
这将传递所有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/
好的,这里有几件事。
1,由于同样的原因,我在前面的代码中将所有内容都保存在一个脚本中。我在这里做了同样的事情。例如,即使不是不可能,将文本对象和标题一起传递给下一个动作也是一件很痛苦的事情。
2,脚本使用unix命令及其输出选项将文本另存为aiff文件。它还通过标题为文件命名。
3,我遇到一个问题,而不是保存文件,而是开始讲文本。??? 原来,我在(http://www.javascripter.net)上测试的URL的标题标签带有大写字母。因此脚本的@adayzdone grep和sed部分返回“”。哪个抛出了say命令。
我通过在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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句