3

I want safari to download and save a web page using apple automator. With a Safari window open, I run the following script in AppleScript Editor:

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 displays the correct url, but then I get the following error in safari:

"The document “Python Programming Language – Official Website” could not be saved as “python.htm”."

I click "ok" on that error box and then AppleScript Editor gives me this error in a dialog box:

"AppleScript Error
Safari got an error: AppleEvent handler failed."

AppleScript Editor has a box at the bottom called "Result" and it also displays the following message:

"error "Safari got an error: AppleEvent handler failed." number -10000"

And the web page won't save. I don't know what's causing this b/c I'm 99% sure that I was using this same (or similar) code to save pages in the past.

Strangely, I moved the "get document 1" and "save document 1" lines outside of the "tell application safari" block and it saved the applescript as "python.htm" and it looked all goofy when opened with Firefox. So the problem seems to be that Safari cannot save and not osx generally.

also, I used chmod to change all permissions on the target save directory to all (used 777 argument in bash).

I updated the software restarted several times.

Here's my versions:

Safari: Version 5.1 (6534.50)
OSX Snow Leopard: Version 10.6.8 AppleScript 2.1.2
AppleScript Editor: Version 2.3 (118)
I'm tethered to my cell phone, using it as a mobile access point. I don't know if that matters.

I think this guy got the same error but no answers:

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

Anybody got a guess? Thanks.

Community
  • 1
  • 1
PatentDeathSquad
  • 543
  • 2
  • 7
  • 16
  • Maybe it's a bug of the `save` command. Even `tell application "Safari" to save front document` won't work, it reports -10000 error. – xuchunyang Jul 08 '19 at 20:09

1 Answers1

1

I don't know the real answer, but it has 50+ views and a tumbleweed, so I figured I'd post something.

apparently you're supposed to use 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

(I saw a post on macscripter and they said this was "the only way to go" so maybe there is no way to use "save as" in Safari and this is a common applescript error but it was not answered b/c this is not an applescript board? Anyway, maybe the original post is supposed to work and I just don't know any better. I figured if people were finding this on the board or through google, an answer may be helpful or maybe the mods just want to delete it if this is an obvious solution).

PatentDeathSquad
  • 543
  • 2
  • 7
  • 16
  • No, this is not an obvoius solution. And remember to use `quoted form of my_new_file`. If you do that, your solution will work. :) – fireshadow52 Sep 05 '11 at 22:57
  • 1
    I pasted it from my applescript editor and it was working, so I think it works. On the second-to-last-line, my_new_file is a string variable that was set on the third-to-last-line. So maybe I have too many lines and it's not readable. You could delete everything after "end tell" and replace it with this line: do shell script "curl " & my_html & " >> " & "/Users/you_user_name/python.htm" – PatentDeathSquad Sep 06 '11 at 15:55