Applescript:Safari无法保存网页(AppleEvent处理程序失败错误)

时间:2011-08-03 20:10:32

标签: safari applescript web-crawler appleevents

我希望safari能够使用apple automator下载并保存网页。打开Safari窗口后,我在AppleScript编辑器中运行以下脚本:

tell application "Safari"
    set URL of document 1 to "http://www.python.org"
    delay 6
    get document 1
    delay 6
    save document 1 in "/mydir/" & "python" & ".htm"
end tell

Safari显示正确的网址,但之后我在Safari中收到以下错误:

“文档”Python编程语言 - 官方网站“无法保存为”python.htm“。”

我在该错误框上单击“确定”,然后AppleScript编辑器在对话框中给出了此错误:

“AppleScript错误
Safari出错:AppleEvent处理程序失败。“

AppleScript编辑器底部有一个名为“Result”的框,它还显示以下消息:

“错误”Safari出错:AppleEvent处理程序失败。“number -10000”

网页不会保存。我不知道是什么导致了这个b / c我99%确定我过去使用相同(或类似)代码来保存页面。

奇怪的是,我将“获取文档1”和“保存文档1”行移到“tell application safari”块之外,它将applescript保存为“python.htm”,用Firefox打开时看起来很傻。所以问题似乎是Safari无法保存而不是osx。

另外,我使用chmod将目标保存目录的所有权限更改为all(在bash中使用777参数)。

我更新了软件几次重启。

这是我的版本:

Safari:版本5.1(6534.50)
OSX Snow Leopard:版本10.6.8 AppleScript 2.1.2
AppleScript编辑器:版本2.3(118)
我拴在我的手机上,用它作为移动接入点。我不知道这是否重要。

我认为这个家伙得到了同样的错误,但没有答案:

Why does this AppleScript get an "AppleEvent handler failed" error on Mac OS X 10.5 Leopard?

有人猜到了?感谢。

1 个答案:

答案 0 :(得分:1)

我不知道真正的答案,但它有50多个视图和风滚草,所以我想我会发布一些东西。

显然你应该使用curl:

tell application "Safari"
    set URL of document 1 to "http://www.python.org"
    delay 6
    get document 1
    set my_html to URL of document 1
end tell

set my_new_file to "/Users/your_user_name/python.htm"
set my_script to "curl " & my_html & " >> " & my_new_file

do shell script my_script

(我在macscripter上看了一篇帖子,他们说这是“唯一的方法”,所以也许没有办法在Safari中使用“另存为”,这是一个常见的AppleScript错误,但没有回答b / c这不是一个苹果板吗?无论如何,也许原来的帖子应该可行,我只是不知道更好。我想如果人们在董事会或通过谷歌找到这个,答案可能会有所帮助或者可能是mods只是想删除它,如果这是一个明显的解决方案)。