Strange Delay: Applescript and Safari Automation

Discussion in 'Mac Programming' started by horaldo, May 24, 2009.

  1. macrumors newbie

    Joined:
    May 24, 2009
    #1
    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?
     
  2. macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #2
    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
     
  3. thread starter macrumors newbie

    Joined:
    May 24, 2009
    #3
    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...
     
  4. macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #4
    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
     
  5. thread starter macrumors newbie

    Joined:
    May 24, 2009
    #5
    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
     
  6. thread starter macrumors newbie

    Joined:
    May 24, 2009
    #6
    I would venture to add the operation completes, despite the "missing value" error. It just seems to delay/block the script...
     
  7. macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #7
    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
     
  8. macrumors newbie

    Joined:
    Oct 13, 2009
    #8
    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.
     
  9. macrumors newbie

    Joined:
    Sep 24, 2008
    #9
    System Events 6-second delay bug when a click opens a new window

    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).

    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?
     

Share This Page