Click-and-drag Action does not select text in Safari. Why?

I would like to use the KM Action “Move and Click and Drag” to select text in Safari. This works as expected in other programs (e.g.: Preview, Skitch, &c.): the action performed by the KM macro exactly mimics the same action performed by a user. But in Safari, when I click, drag, and release, I select the text over which I dragged. The KM Action, in Safari, moves the mouse cursor, but does not select the text over which the cursor has been programmed to move.

Am I doing something wrong? Are my expectations wrong? Is there an easy fix? Is there a work-around?


Hi Kirby,
I am curious about the Preview solution. Do you have something you could post?

Something Chris Stone pointed out to me about Safari is that I needed to use Applescript to bring it to the front.
As in:
tell application "Safari"
end tell
Could this be the issue?

Hi Tunes. There is no “solution”. In every application I tried except Safari, the KM Action “Move and Click and Drag” exactly mimics the same action when done by the user. The universal action of clicking-and-dragging is to select. This works as expected in Preview. What is selected will depend on the type of document in the active window.

I tried your suggestion two ways:
— adding an Action to make Safari active prior to the click-and-drag Action, and
— using AppleScript.
Neither had any effect, afaict. The Action is being executed — I can see the mouse cursor move to the specified start position and then jump the specified number of pixels — but nothing is being selected in the Safari window.

Any other suggestions?

Hey Kirby,

I tried this in Chrome and Safari on the same page.

The macro worked in Chrome and failed in Safari.

Report it as a bug to Peter.

But keep in mind that he might not be able to do anything about it.


1 Like

Thank you Chris. Will do. Thanks for the additional info.

Sent from my mobile. (Edited: removed the smiley, which got rendered here with, as I saw it, a different meaning that I had intended.)

Curiouser and Curiouser.

  1. I reported this as possibly a bug. Peter replied that it was a Safari issue, and suggested finding a work-around.

  2. In trouble-shooting, I noticed that if I execute the Action “Click … Drag” twice, on the second time the items under the click-and-drag area are selected. (The first Action seems to make the second work — they don’t have to be identical.)

So my work-around, which seems robust at this point, is to simply put the Action “Click … Drag” in twice, and copy to the Clipboard after the second one.

If you or anyone can suggest further trouble-shooting methods, I will try to look into this further. I am curious why the action done by the user with a trackpad (in my case) are not initially identical to the mouse-down, etc., actions executed by KM.

Hey Kirby,

The way Safari handles textual display varies according to the underlying HTML and JavaScript, so I find a macro working on some pages and not on others.

This one is sort of hit-and-miss.

Generic-Test 01.kmmacros (3.4 KB)

In general clicking and dragging is not a good way to handle any task — unless of course it's the only way.

This one works every time if I'm precise with the start location.

Keyboard Maestro “Generic-Test 01” Macro

Generic-Test 01.kmmacros (2.9 KB)


1 Like

Hi Chris — these were helpful — thank you for them. I now have something that works for my current need reliably.

What I don’t understand and would like to understand is this: how is it that the user’s mouse cursor action always works as expected (it selects what is under the dragged-over area), but the KM action click-and-drag only sometimes works in Safari? I could understand the macro not doing what I expect when the user action doesn’t do what I expect; I can’t figure out how Safari can treat my use of the mouse cursor differently than the inputs provided by the KM mouse cursor Actions.

Hey Kirby,

Peter can perhaps give an idea of how the nuts and bolts of this work.

But Keyboard Maestro is NOT the user. KM artificially emulates user actions.

Furthermore Peter has used only publicly available APIs in Keyboard Maestro, so he doesn’t have to reengineer everything every time Apple makes a simple change — and not hacking into the system does limit KM in some ways.

Safari is evidently doing something a little different with it’s text display than some other apps, and that is not necessarily documented anywhere — especially in the context of driving the UI with an automation utility.

The best way to work Safari in this respect is to use AppleScript and/or JXA (JavaScript for Automation).

Especially when the structure you’re working with is known.

@ComplexPoint has posted some great examples of this on the forum. They’re somewhat confusing for someone unacquainted with JavaScript, but some can be used anyway with a little patience and tinkering — plus Rob is very helpful when asked.


1 Like