How to make a macro to hold a button in Dictionary app?

I want to make a macro that will press and hold this button in Dictionary macOS app :

The reason is that pressing and holding this button shows history. However you have to be holding this button for the history to show.

I thought I could make this macro :

But it didn't work. I want a macro that will press and hold this button and on the next click of my mouse, release the held button. So I could run macro, it will show history and I can move to pick any of the options of my history or click outside to dismiss the history window.

Thank you for any help.

I was able to get this to work, but not in the way I expected. The first thing I tried was this:

When that didn't work, I next tried this:

But in both cases, instead of holding the mouse button, the actions acted as if the mouse had been clicked, even though it hadn't, and thus the history dropdown never appeared. However, a standard Pause action did work:

Given this, I can only guess that there may be a bug in the mouse button condition (perhaps because we're using a trackpad instead of a mouse? I can't speak for @nikivi, but I certainly am) or, more likely, I'm misunderstanding the way the mouse button condition works.

This is probably best done using UI scripting.

To identify the window elements, you need to use tools like Script Debugger 6 or UI Browser.
You might also be able to use one of the windows tools / macros / scripts published here by @ccstone. Just search on his user name and "window".

1 Like

Thanks for the tip! With Script Debugger’s help I was indeed able to identify the “back” button, and this is definitely more reliable than mouse coordinates:

tell application "System Events"
	tell application process "Dictionary"
		tell window 1
			tell toolbar 1
				tell UI element 1
					tell UI element 1
						click button 1
					end tell
				end tell
			end tell
		end tell
	end tell
end tell

but as far as I can tell, there doesn’t seem to be a way to click and hold on a UI element with System Events like there is in KM, so I’m at a loss as to how to proceed next (though admittedly, I’m even more of an amateur at UI scripting then I am at “regular” AppleScript). Any ideas?

This works for me, running macOS 10.11.6:

tell application "Dictionary" to activate

tell application "System Events"
  tell application process "Dictionary"
    tell window 1
      tell toolbar 1
        tell UI element 1
          tell UI element 1
            --click button 1
            tell button 1 to perform action "AXShowMenu"
          end tell
        end tell
      end tell
    end tell
  end tell
end tell
2 Likes

Aha, so that's how you do it: perform action AXShowMenu. I can confirm that this works on High Sierra 10.13.1 as well (and even better, it makes the history dropdown navigable by keyboard too). Thanks, @JMichaelTX! I'd never used perform action or AXShowMenu in a script before, so I don't think I would have ever been able to figure that out on my own :slightly_smiling_face:

@nikivi, here it is in macro form:

Show History.kmmacros (2.3 KB)

Since you'll be using this in a Dictionary-only macro group, I took the liberty of removing the top "activate" line as it no longer seemed necessary (since Dictionary will always be activated when this is run). I'm still curious as to why the mouse button conditions didn't work as I expected, but thanks to the keyboard navigability this is definitely a much better solution :slightly_smiling_face:

1 Like

While that should probably work, the safe thing to do is to leave the Dictionary activate in the script to make sure it always frontmost. The System Events UI scripting depend on this. There is no harm, no downside, to leaving the activate in place.

Also, if someone wants to grab this script and test/modify it in Script Editor or SD, then it would be needed.

Fair points. I usually don’t like to leave in actions that I consider to be extraneous in macros, but that’s likely just a holdover from macro-based thinking than UI script-based thinking (since the former is far more familiar to me than the later) so I’ve uploaded a revised version of the macro and edited the post.

Thank you a lot @gglick and @JMichaelTX

I forgot to mention one thing and that is I run Dictionary in Full screen mode and in full screen, this macro fails and does nothing. :disappointed:

I agree with @gglick here in that this macro will only be ran in Dictionary macro group and thus there is no need to for activate action indeed. :slightly_smiling_face:

Also this code presses the forward history button :

tell application "Dictionary" to activate

tell application "System Events"
	tell application process "Dictionary"
		tell window 1
			tell toolbar 1
				tell UI element 1
					tell UI element 1
						tell button 2 to perform action "AXShowMenu"
					end tell
				end tell
			end tell
		end tell
	end tell
end tell

Just need to make it so that it works in full screen too.

This was a bit tricky for me to figure out, but I think I got it:

Show History 1.1.kmmacros (4.4 KB)

Here's the UI script that I managed to get working in fullscreen:

tell application "Dictionary" to activate

tell application "System Events"
	tell application process "Dictionary"
		tell window "Dictionary"
			tell UI element 3
				tell toolbar 1
					tell group 1
						tell UI element 1
							tell button 1 to perform action "AXShowMenu"
						end tell
					end tell
				end tell
			end tell
		end tell
	end tell
end tell

And here's one for forward history too while I'm at it (as you can guess, it was a simple substitution of 2 instead of 1 for the key button element):

Forward History.kmmacros (4.4 KB)

Let me know if either of these doesn't work for you on your system. Hopefully they do!

1 Like

This macro works perfectly :

back history.kmmacros (27.4 KB)

However this one for forward history :

forward history.kmmacros (27.4 KB)

Fails for me and only works when not full screen. I am on El Captain, perhaps that's the reason?

Also little side question, @gglick what do you use to quickly share macros with an image overview of the macro together with the download link to the macro?

I am using this macro by @ccstone for this currently :

upload macro to KM forum.kmmacros (3.3 KB)

But it always creates a new thread. Would be quite sick if I could export (upload) a macro in the currently active KM forum prompt I am typing. But that's probably a lot harder.

1 Like

Huh, odd. The forward history macro definitely works for me on High Sierra, so I guess the dictionary app on El Capitan has a different UI element arrangement? Strange that it would work for back though :confused:
Unfortunately I don't have an El Capitan machine here to test on, so I'm afraid @JMichaelTX or someone else who's using El Cap and knows their way around UI scripting will have to troubleshoot. Sorry I couldn't be of more help!

Ironically enough, currently my process for this is entirely manual :sweat_smile:
I've downloaded and tried @JMichaelTX's Upload Macro to New or Existing Topic macro, which works great (and I believe is based on that macro of @ccstone's that you use) but since I usually upload macros to existing topics, I've just found it faster to manually export the macro through the File menu and copy as image from the Edit menu, followed by running the image through @Tom's great Downscale Images for KM Forum macro (which I activate through an Alfred workflow for executing KM macros similar to Trigger Macro by Name, something I'm sure he wouldn't be happy about :wink:).
One of my automation to-dos is actually to find a way to automate this process, which is probably pointlessly reinventing the macro uploading wheel, but I'd like to get something that better suits my own needs (plus, I figure it will be a good exercise, since there's so much I still have to learn compared to the true gurus mentioned above). If you're interested, I'd be glad to share whatever I come up with once I finally get around to coming up with it :slightly_smiling_face:

1 Like

Okay, strangely enough, it started working after I restarted Dictionary. :confetti_ball:

Thank you a lot @gglick. Using Dictionary now is one of the best things ever with these two macros.

My dream of reading all the wiki is no longer a dream. :grin:

1 Like

Weird that a relaunch fixed it. Glad you got it working though! Thank you for coming up with this macro idea too; I often use Dictionary myself, so these macros should come in handy for me as well (plus I got to learn just a bit more about UI scripting :sunglasses::+1:)

1 Like

Oh, no worries! I’m often launching KM macros via LaunchBar. Not any less of a blasphemy than doing it via Alfred, I think :pensive:

Oh, I didn’t mean the act of executing macros through a launcher; I doubt even Peter much cares about that. I just meant the fact that I specifically use Alfred for this rather than LaunchBar, given how you feel about the former compared to the latter :wink:

Hey, I’m not an Alfred hater :wink:

I even have a magasupporter-lifetime-powerpack license (or whatever it is called), bought with version 1, about a decade ago. In fact, IIRC, it was Alfred who brought me on the way to get in deeper touch with LaunchBar. (I already knew LB before that, but never really digged in.)

It’s even installed on my Mac, usually not running, but once in a while I fire it up, to see if there is an update and what they have changed/improved.

Sometimes I even recommend Alfred to other people :sunglasses:

1 Like