Google chrome 从Chrome中选择网页的所有内容,并使用AppleScript保存到文本文件
我正在创建一个Automator工作流,循环浏览Google Chrome窗口的所有选项卡,并将每个选项卡上的网页保存到文本文件中Google chrome 从Chrome中选择网页的所有内容,并使用AppleScript保存到文本文件,google-chrome,applescript,automator,Google Chrome,Applescript,Automator,我正在创建一个Automator工作流,循环浏览Google Chrome窗口的所有选项卡,并将每个选项卡上的网页保存到文本文件中 on run {input, parameters} tell application "Google Chrome" set windowList to every window repeat with theWindow in windowList set tabList to every
on run {input, parameters}
tell application "Google Chrome"
set windowList to every window
repeat with theWindow in windowList
set tabList to every tab in theWindow
repeat with theTab in tabList
select all
copy selection
set theTitle to title of theTab
set theScript to "echo" & (selection as text) & "> $HOME/Desktop/tmp" & quoted form of POSIX path of theTitle & "-clipboard-file.txt"
display dialog theScript
do shell script theScript
end repeat
end repeat
end tell
return input
end run
但是,这会生成空文本文件。
我怀疑复制选择没有与系统粘贴板交互。有没有办法将文本复制到粘贴板或直接导出到文本文件?我采用了一种不同的方法。这个脚本可以工作,但我仍然不知道如何将项目粘贴为列表
tell application "Google Chrome"
every window
URL of every tab of item 1 of result
set the clipboard to the result as text
end tell
do shell script "pbpaste > ~/Desktop/ClipboardFile.txt"
[这已被[编辑]以包括魔法unicode友好强制。请参阅unicode友好注释下的更新行。该行使用著名的AppleeEvent代码选项反斜杠和选项shift反斜杠字符]
似乎您只是想要文本,就像它被复制到剪贴板一样,保存到每个文件中。
我会提醒你不要使用你刚开始使用的方法,只是因为在某些情况下,没有标题会留下一个实际名为.txt的文件,而这个文件是不可见的!因此,我建议使用一个计数变量以及您所拥有的,以防这在我的机器上起作用,并为无标题页面提供保护:
--first, I get the desktop as an old mac style path, using the Finder:
tell application "Finder" to set dt to desktop as string
tell application "Google Chrome"
set windowList to every window
set tabCount to 1
repeat with theWindow in windowList
set tabList to every tab in theWindow
repeat with theTab in tabList
set wHTMLText to execute theTab javascript "document.body.innerText;"
set thetitle to title of theTab
if thetitle is "" then set thetitle to ("untitled" & (tabCount as string))
set filePath to (dt & "tmp:" & thetitle & ".txt")
set myFile to open for access filePath with write permission
--unicode-friendly:
write wHTMLText to myFile as «class utf8»
close access myFile
set tabCount to tabCount + 1
end repeat
end repeat
end tell
[编辑:]哦,如果您想要HTML,请使用document.body.innerHTML;并使用.html扩展名保存。谢谢!这可以通过添加所有文本来实现,但它不支持UTF-8。因此,非ASCII字符被破坏。如何在保存操作中指定编码?对不起,我没有单独问一个问题。对不起,耽搁了。我注意到该变量包含unicode,但当它保存时,会把它搞砸。我要想个办法…记下最新的答案。我希望这对你有用,@kakyo-它似乎对我有用。顺便说一句,@kakyo-如果这有效,请“接受”答案,这样我们都知道它对你有用。