PDA

View Full Version : Strange Delay: Applescript and Safari Automation




horaldo
May 25, 2009, 12:58 AM
Hi all,

Due to some issues with ezproxy, I find that I am constantly needing to clear cookies in Safari 4 Public Beta. Naturally I would like to script this and bind it to a shortcut somehow (as an aside, it seems I am unable to do this without a third-party utility). The code is as follows:

activate application "Safari"

--------
activate application "Safari"

tell application "System Events"

tell process "Safari"

--Open Safari -> Preferences
click menu item "Preferences…" of menu 1 of menu bar item "Safari" of menu bar 1

--Open Security Tab
click button "Security" of tool bar 1 of window 1

--Show Cookies Button in Security Tab
click button "Show Cookies" of group 1 of group 1 of window "Security"

--Remove All in the Security:Show Cookies sheet
click button "Remove All" of sheet 1 of window "Security"

--Click "Remove All" in the pop-up confirmation dialog
click button "Remove All" of window 1


--Done with the Security:Show Cookies sheet
click button "Done" of sheet 1 of window 1

--Done with the Security Tab
click button 1 of window "Security"

end tell

end tell
--------

The issue is that at some point after the confirmation dialog comes up (click button "Remove All" of sheet 1 of window "Security") the script freezes for a couple of seconds. This behavior does not occur when manually activating the buttons. Any ideas?



mysterytramp
May 25, 2009, 06:49 AM
I try to avoid System Events as much as possible. It can't anticipate unexpected changes ... what does it do when the software pops up a window about a new version, for example?

Any chance you can delete the cookies without Safari? Applescript can easily delete a file. Or if that's not doable, maybe you could replace a loaded cookies file with a blank cookies file (stored someplace safe).

mt

horaldo
May 25, 2009, 09:36 PM
I've tried that. It doesn't work. The main problem is the mystifying delay... And I agree; some way to do this without resort to System Events would be preferable...

mysterytramp
May 26, 2009, 05:37 AM
I've tried that. It doesn't work. The main problem is the mystifying delay

Not quite following you here ... the delay appears inside the program. If I were doing this, I'd delete the cookies when Safari isn't running through the Finder, so you shouldn't see a delay in Safari. So let's back up ...

Can you delete the cookies file outside of a Safari session? Because possibly the best solution is to create a script that deletes the file then launches Safari. You should be able to keep that in your Dock. You may notice a delay when Safari launches because it needs to rebuild a cookies file, but it should be very short, and I'd bet unnoticeable.

If you need to do it within Safari, you might try adding "delay n" between the commands (n would be seconds). Counter-intuitive, but it might make the software process System Events better. No firsthand knowledge -- I've done my best to avoid System Events -- but I've seen scripts where folks add the delay and it seems to work more pleasantly.

mt

horaldo
May 26, 2009, 06:50 PM
The script is aimed precisely at my reluctance to close Safari down. As I averred to, the ezproxy cookies need to be removed regularly in order for it to work after some time has passed, and closing in the middle of a research session is not an option. If there is some native way to do this, that would be great. Failing that, an explanation of the delay would help.

This may help; the Event Log records the following:

----
click button "Remove All" of sheet 1 of window "Security" of process "Safari"
missing value
----

The "missing value" appears after the delay... You can use the script as below to test this behaviour without actually deleting your cookies:


---
activate application "Safari"

tell application "System Events"

tell process "Safari"

--Open Safari -> Preferences
click menu item "Preferences…" of menu 1 of menu bar item "Safari" of menu bar 1

--Open Security Tab
click button "Security" of tool bar 1 of window 1

--Show Cookies Button in Security Tab
click button "Show Cookies" of group 1 of group 1 of window "Security"

--Remove All in the Security:Show Cookies sheet
click button "Remove All" of sheet 1 of window "Security"

--Click "Remove All" in the pop-up confirmation dialog
--click button "Remove All" of window 1

--Done with the Security:Show Cookies sheet
--click button "Done" of sheet 1 of window 1

--Done with the Security Tab
--click button 1 of window "Security"

end tell

end tell
---

Cheers,
Lorne

horaldo
May 26, 2009, 06:52 PM
I would venture to add the operation completes, despite the "missing value" error. It just seems to delay/block the script...

mysterytramp
May 27, 2009, 05:13 AM
Have you used UIElementInspector? It could be that the "Remove All" button is logged as something else internally. You may need to refer to it by number or id rather than name.

(This is another reason to avoid System Events)

mt

bittwiddler
Oct 13, 2009, 07:16 PM
Hi,

I was using PithHelmet to easily delete cookies until snow leopard, hopefully it will be back up to speed soon.

I also could not get around the delay using applescript. I tried timeout, I tried sending separate commands via automator still it would take 6 seconds to delete the cookies.

I was trying to use the Reset Safari menu item (check delete cookies) but was having trouble make it work when I found this little gem (I put the author and URL at the end) :

tell application "Safari" to set ResetSafari_loc to localized string "Reset Safari…"
if ResetSafari_loc ends with "…" then
set ResetSafari_loc to text 1 thru -2 of ResetSafari_loc
else if ResetSafari_loc ends with "..." then
set ResetSafari_loc to text 1 thru -4 of ResetSafari_loc
end if

activate application "Safari"
tell application "System Events" to tell process "Safari"
tell menu bar 1
tell menu bar item "Safari" to tell menu 1 (* EDITED *)
-- get name of every menu item
click menu item 9
end tell
end tell

repeat until exists window ResetSafari_loc
delay 0.2
end repeat
tell window ResetSafari_loc
click button 1
end tell
end tell


--Yvan KOENIG (VALLAURIS, France.) samedi 22 août 2009 16:19:30
--http://macscripter.net/viewtopic.php?pid=117178

Works with no delay.

DanShockley
Apr 1, 2013, 05:07 PM
There is a thread that seems to describe the same bug that appears to be limited to specific software. It is on MacScripter.net: Script delays on clicking button (controlling Sys Prefs pane) (http://macscripter.net/viewtopic.php?id=37635).

Note the most recent post that describes a delay in FileMaker. It seems like the only work-around when software exhibits this bug is to do something else to open the new window. If the only way to run the desired function is via a button, one (terrible) option is clicking at coordinates relative to the corner of the front window. Obviously this will easily fail if the content can move relative to the corner or if a new version of the software moves the button desired. Any other ideas?