我如何编写一个脚本,将Safari中打开的网页保存到某个路径?
(代码将在稍后用于更复杂的脚本,因此使用系统事件的kludgy解决方案不会这样做.)很多谷歌搜索使用保存源函数的脚本让我很不知情,所以答案可能互联网上的第一个.我粘贴了一些下面可能有用的东西.
用于Safari的AppleScript字典中的这两个条目看起来很有用:
document n [另请参阅Standard Suite]:表示窗口中活动选项卡的Safari文档.
特性:
- source(text,r / o):当前在文档中加载的网页的HTML源.
- text(text,r / o):当前加载在文档中的网页文本.对文本的修改不会反映在网页上.
- URL(文本):文档的当前URL.
然后:
save v:保存对象.
save specifier:命令的对象
- [ as text]:保存数据的文件类型.
- [ in alias]:保存对象的文件.
此脚本确实保存了HTML文档,但与使用Safari手动"导出为页面源"功能保存的文件相比,输出看起来很糟糕:
tell application "Safari"
(* Get a reference to the document *)
set myDoc to document of front window
(* Get the source of the page *)
set mySrc to source of myDoc
(* Get a file name *)
set myName to "Message_" & "0001" & ".html" -- the # will be modified later
tell application "Finder"
(* Get a path to the front window *)
set myPath to (target of front window) as string
(* Get a file path *)
set filePath to myPath & myName
(* Create a brand new file *)
set openRef to open for access (myPath & myName) with write permission
(* Save the document source *)
write mySrc to openRef
(* Close the file *)
close access openRef
end tell
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止所写的:
这是我的第一次尝试:
tell application "Safari"
set pageToSaveSafariWindowIn to "Q:Ø:"
set pageToBeSaved to front window
save document pageToBeSaved as source in alias pageToSaveSafariWindowIn
end tell
Run Code Online (Sandbox Code Playgroud)
以下是生成的日志:
tell application "Safari"
get window 1
--> window id 6017
save document (window id 6017) as source in alias "Q:Ø:"
--> error number -1700 from window id 6017 to integer
Run Code Online (Sandbox Code Playgroud)
和
错误"Safari出错:无法将窗口ID 6017转换为整数类型." 从窗口ID 6017到整数的-1700
另一个尝试:
tell application "Safari"
save source of document in "Q:Ø:"
end tell
Run Code Online (Sandbox Code Playgroud)
给出结果日志:
错误"无法获取文档来源." 编号为-1728,来自«class conT»的文件
我找到了我认为更好/更简单的解决方案:
tell application "Safari"
activate
set URL of document 1 to "http://www.apple.com"
delay 5
set myString to source of document 1
end tell
set newFile to POSIX file "/Users/myUsername/test.html"
open for access newFile with write permission
write myString to newFile
close access newFile
Run Code Online (Sandbox Code Playgroud)
笔记:
“文档 1 的来源”似乎仅在网页完全加载后才填充正确的源文本。因此需要延迟。也许您可以使用较低的延迟。
有一些解决方案推荐使用 curl。我没有试过这个,但我认为对于动态生成的页面这可能有问题。
以上适用于 OSX 10.8.4。其他版本未测试。
小智 2
这是保存充满选项卡的窗口的一种方法。最初的 UI 处理程序是由 StefanK aka 编写的。Macscripter 的 Stefan Klieme 名声大噪。它会考虑 webarchives 文件结尾,当 Safari 不确定时,您可以调整是否要覆盖或忽略已写入的文件。它不保存重复的选项卡,您可以设置一个属性来决定保存时是否关闭选项卡。
\n\n请查看 MacScripter,脚本中包含直接链接,以获取任何更新。
\n\n您当然可以使用 wget,但我选择了 UI 脚本,因为 wget 可以下载浏览器中已有的内容,并且编程也很麻烦。
\n\n1\n end Tell\n end if\n 按键返回\n 延迟 0.2\n 如果存在工作表 1 then -- 我们被询问是否要覆盖已保存的选项卡\n if dontOverWriteSavedTabs then\n 按键返回 # if it已经被保存了。我们不覆盖它\n 单击按钮 3\n else\n 击键选项卡\n 击键空格 # 我们要覆盖\n 结束 if\n else\n 尝试\n 将虚拟设置为工作表 1 的焦点\n error\n # 单击应用程序“Safari”面板的按钮 1\n 击键返回\n\n 延迟 0.2\n 如果存在工作表 1 then -- 系统询问我们是否要覆盖已保存的选项卡\n if dontOverWriteSavedTabs then \n 按键返回#(如果已保存)。我们不覆盖它\n 单击按钮 3\n else\n 击键选项卡\n 击键空格 # 我们要覆盖\n end if\n end if\n end try\n end if\n end Tell\n endtell\n endtell\n endtell\n 将 timeNumber 设置为 timeNumber + 1\n 返回 timeNumber\n 结束 saveCurrentTab\n\n 在 downloadWindowInFront() 上\n 告诉应用程序“Safari”\n 激活\n 将 tabCount 设置为计数其窗口的选项卡 1\n 如果 tabCount 0 则将冒号设置为 true\n 如果(aPath 中“/”的偏移量)> 0,则将斜杠设置为 true\n\n 如果冒号和斜杠则\n 返回 null\n else如果是冒号则\n 将 origDelims 设置为“:”\n 否则如果是斜杠则\n 将 origDelims 设置为“/”\n else\n 返回 null\n end if\n 本地tids\n 设置 {tids, AppleScript\'s文本项分隔符} 到 {AppleScript\ 的文本项分隔符,\n\n
享受
\n 归档时间: |
|
查看次数: |
11347 次 |
最近记录: |