I just tried to write a simple macro to accelerate the deletion of certain cookies from Safari:
It opens Safari’s prefs window,
goes to the ‘Privacy’ tab,
presses the ‘Details…’ button,
pastes-in the search string,
presses the ‘Remove All’ button and
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:
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.
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
------------------------------------------------------------
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.)
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.
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.
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.