Button gets pressed, but then macro fails with ‘failed to find button’

I just tried to write a simple macro to accelerate the deletion of certain cookies from Safari:

  1. It opens Safari’s prefs window,
  2. goes to the ‘Privacy’ tab,
  3. presses the ‘Details…’ button,
  4. pastes-in the search string,
  5. presses the ‘Remove All’ button and
  6. confirms the modal dialog

The problem comes with point 5: It does press the ‘Remove All’ button (you can tell this because the confirmation dialog pops up), but then it breaks with these errors:

23.9.15 23:49:59,417 Keyboard Maestro Engine[83445]: Keyboard Maestro Engine: Assertion Failed: err == 0 , file: /Users/peter/Keyboard Maestro/Project/Source/Actions/APressButton.mm:85, value: -25204

23.9.15 23:49:59,425 Keyboard Maestro Engine[83445]: Keyboard Maestro Engine: Press Button ‘Remove All’ failed to find button. Macro ‘Delete Cookies’ cancelled (while executing Press Button ‘Remove All’).

I don’t understand this error message, because apparently it already did press the button. [Please note also the strange file path in the first error, which does not exist on my computer.]

When I uncheck ‘Failure Aborts Macro’ at the ‘Press Button 'Remove All'’ action, the macro stalls at this point for about 6 seconds and then continues successfully.

KM 7.0.1; OS 10.11 (15A282b)

Here is the macro. If somebody wants to help me and test the macro, make sure the string for %Variable%Search Term% matches an existing (and dispensable) cookie in your Safari.

Thanks in advance for any help.

Tom

Hey Tom,

I spent a long time testing this with AppleScript & System Events.

The upshot is that the click event sent to the Remove Now button takes an inexplicable amount of time to return – on my system it takes about 6 seconds.

It’s very unlikely Peter can do anything to Keyboard Maestro to change this behavior.

It’s possible the problem might be corrected in El Capitan, but we won’t count our chickens…

I’m including the AppleScript in this post, so people might learn by example how to perform your task and how it can be used as a diagnostic tool.

-Chris


DO NOT RUN THIS CODE UNLESS YOU KNOW WHAT YOU’RE DOING.


------------------------------------------------------------
# AppleScript
# This script will delete cookies in Safari corresponding to the variable 'searchValue'.
# Satimage.osax and LapTime.osax needed for diagnostics.
# But will run without them.
# Diagnostics commented-out.
------------------------------------------------------------
set searchValue to "21"
set theDelay to 0.1

tell application "System Events"
  tell application process "Safari"
    set frontmost to true
    click menu item "Preferences…" of menu 1 of menu bar item "Safari" of menu bar 1
    
    set _cntr to 0
    tell front window
      tell toolbar 1
        repeat while title ≠ missing value and _cntr < 20
          # NULL
          set _cntr to _cntr + 1
          delay theDelay
        end repeat
        
        if button "Privacy" exists then click button "Privacy"
        
      end tell
    end tell
    
    set _cntr to 0
    tell window "Privacy"
      tell group 1 of group 1
        repeat while (exists of button "Details…") is false and _cntr < 20
          # NULL
          set _cntr to _cntr + 1
          delay theDelay
        end repeat
        
        if button "Details…" exists then
          click button "Details…"
        else
          beep 2
          return
        end if
        
      end tell
      
      tell sheet 1
        set value of text field 1 to searchValue
        
        set _cntr to 0
        repeat while (enabled of button "Remove All") is false and _cntr < 10
          # NULL
          set _cntr to _cntr + 1
          delay theDelay
        end repeat
        
        ############################################################
        # Problem Area
        ############################################################
        # set lap1 to lapStart() of me
        log "Remove-all-button start"
        if enabled of button "Remove All" is true then
          click button "Remove All"
          keystroke (character id 13)
        end if
        log "Remove-all-button end"
        # set lap1Stop to lapStop(lap1) of me
        # log "Elapsed Time: " & lap1Stop
        ############################################################
        
        click button "Done"
        
      end tell
      
    end tell
    
  end tell
end tell

tell application "Safari" to tell window "Privacy" to close

------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------
on lapStart()
  return start timer
end lapStart
------------------------------------------------------------
on lapStop(tmrNumber)
  return format ((stop timer tmrNumber) / 1000) into "##.####"
end lapStop
------------------------------------------------------------

I forgot to mention the solution is to use plain old clicks at the end.

Check to see if button Remove All is enabled.

If not then use a Press-Button action for Done.

If so then use a Click action to click Remove All and then Remove Now. (These buttons should always be in fixed positions.)

After that you can probably use a Press-Button action for Done.

I would use a little AppleScript in an Execute an AppleScript action rather than a keystroke to close the Preferences window at the end.

tell application "Safari" to close window "Privacy"

You’re less likely to have an accident and close the wrong window that way.

-Chris

Thanks a lot for looking into the problem!

Yep, I tried your AppleScript, and it’s the same delay.

I also found these: I, II, III

So, it seems there really isn’t much we can do about it.

As you said, the best workaround seems to be clicking the button coordinates of ‘Remove All’, which works flawlessly. The subsequent buttons (‘Remove Now’ and ‘Done’) respond correctly again to Press Button actions.

Thanks for the hint to use AppleScript to close the window, instead of ⌘W.

(It is strange that KM doesn’t provide a native action for closing a window of a named application, considering that there are already actions for quitting a specific application and for scaling the window of a specific application. Closing windows is certainly one of the most frequently needed things.)

Tom

Hey Tom,

Not so strange.

All applications will respond to AppleScript activate, run, and quit events even if they're not scriptable, but an app has to be scriptable to respond to a close window event – because you have to be able to talk to the requisite object.

In many cases you can use System Events to click a window's close-button, but not all apps are are accessibility-friendly. This lack of uniformity I'm sure figures into Peter's design decision in this case.

-Chris

This makes sense, yes.

In case anybody is interested, I’ve made the macro above functional. It accepts now a list of search values via user input and will delete all matching website data. (The values will be remembered.) Very handy.

You can download it here.

Note: If some matches get jumped, try to enable the delays, which are currently disabled.

Thanks again for your help.

Tom

Just want to add that the problem, IME, is not specific to an app or a button. I stumbled on this a few times, and finally resorted to always using reference coordinates and a Move Or Click Mouse Action instead of a Press A Button Action. This first tripped me over the “New Folder” button on “File ▹ Save” dialogs; after I encountered it elsewhere I no longer used the Press A Button Action. Note that it often worked for me, but since I had no way to predict when it wouldn’t work, no expertise to trouble-shoot, and no desire to substitute Move Or Click Mouse Actionon Action for the Press A Button Action on an “As needed” basis, I stopped using it. Additionally, iirc, it was always apparent, through the visual representation of a button press, that the specified button had been pressed. The problem was that either nothing happened, or it happened after a delay that was both unreasonable and difficult to work around. Odd (to me) was that adding a Pause Action after the Press A Button Action did not reliably fix the problem.

Tom — thanks for posting the macro. Useful :blush: .

I uploaded a fixed version. (Strangely there was a ⌘A keystroke missing. Should now work also sans delays [disabled]).

1 Like