Why does this give me an error?

tell application “Safari”
activate
print document 1
end tell
delay 1
tell application “System Events”
tell process “Safari”
click button “Save as PDF…” of UI element 3 of sheet “Print” of window 1
delay 1
click button “Save” of window “Save to File”
end tell
end tell

Error is: NsReceiverEvaluationScriptError: 4
It give error with every button…
Is there any other way to save to PDF from safari or any other html application?

Thanks

Jukka

Hi jaol,

Right now, I have pdf workflow running:

http://www.apple.com/applescript/print/

so, I can’t go throught the whole process of making your thing work. I don’t like crunching things. When you crunch your script, it’s harder to tell where things aren’t working. If you want a sure thing, then go step by step. What I do is start with the first step and ‘get ui elements’. You might want to jump to your other targets, but step by step is like this. First:

tell application “Safari” to activate
tell application “System Events”
tell process “Safari”
keystroke “p” with command down
end tell
end tell

Next, instead of using the name of the front window use front window and get ui elements:

tell application “Safari” to activate
tell application “System Events”
tell process “Safari”
keystroke “p” with command down
tell front window
UI elements
end tell
end tell
end tell

In the list for the result there is window “Print” and get ui elements of that:

EDITTED: oops, forgot to post this:

tell application “Safari” to activate
tell application “System Events”
tell process “Safari”
keystroke “p” with command down
tell front window
tell window “Print”
UI elements
end tell
end tell
end tell
end tell

Now there is ui element 1 in the result so:

tell application “Safari” to activate
tell application “System Events”
tell process “Safari”
keystroke “p” with command down
tell front window
tell window “Print”
tell UI element 1
UI elements
end tell
end tell
end tell
end tell
end tell

The result now has a bunch of stuff. By trial and error getting ui elements you’ll find your way to where you should click. After a few times, you’ll know your machine and will have few problems with the targets.

Maybe it was that ‘sheet’ part that caused the error.

gl,

Error come at this point:

tell application “Safari” to activate
tell application “System Events”
tell process “Safari”
keystroke “p” with command down
tell front window
UI elements --this line errors…
end tell
end tell
end tell

Looks like I can’t list UI elements?
btw. I change forth line “with” to “using”…

I enabled some access to devices from Universal Access and now it works…
Well, dunno why it was disabled???

Thanks

Jukka