Jump to content

Create a Keyboard Hot Key from recorded mouse clicks


Recommended Posts

I don't know if Alfred can do this, and if not, can someone recommend any other app? -- I use an application at work where I have to click and select a menu choice many times a day. It's not a top menu choice like FILE or EDIT, it's actually in a sidebar menu. Is there some way to record the mouse clicks so that I can recreate them with a keyboard hot key? Does that make sense? Thanks.

 

Link to comment

Yes, you can do it with Alfred, specifically through AppleScript.

 

Depending on the application itself, it might support AppleScript (how to find out) in a way that you can tell it to click the exact menus you want by name. If the application does not support it and the menus are always in the same location, you can click specific coordinates on the screen with something like tell application "System Events" to click at {254, 47}. To get those coordinates, press ⌘⇧4 to enter the mode to take screenshots where it will indicate the current cursor position. Use delay to wait some time between clicks. Your final code might look something like this:

tell application "System Events"
  click at {254, 47}
  delay 1
  click at {100, 200}
end tell

Here’s a workflow to get you started.

Link to comment
On 15/10/2016 at 1:43 AM, vitor said:

Depending on the application itself, it might support AppleScript (how to find out) in a way that you can tell it to click the exact menus you want by name.

 

Don't think it's relevant here, but FWIW, as far as clicking items in the menubar goes, the application itself doesn't need to support AppleScript, as this is one of those things, like activate or open, that's handled by the system (System Events does the menu clicking thing).

 

It even works on non-native apps, like Sublime or Atom, as these still use the native menu system under the hood. For example, here's how to show Sublime's console:

 

tell application "Sublime Text"

    activate

end tell

 

tell application "System Events"

    tell process "Sublime Text"

        tell menu bar 1

            tell menu bar item "View"

                tell menu "View"

                    click menu item "Show Console"

                end tell

            end tell

        end tell

    end tell

end tell

 
The major issue with this technique, imo, is that it's tied to a specific language, by which I mean English, not AppleScript.
Edited by deanishe
Link to comment

Vitor, I tried your little script and put in my screen coordinates, but nothing happens. I'm typing the keyword "clicker" in Alfred. I'm totally inexperienced at this scripting so I don't know what else to try or how to troubleshoot. This is a custom application with sidebar menus that I wouldn't think are handled by the system. What I'm understanding is that running this script should produce the same result as me clicking on that menu choice. Attaching  my script and what the menu items look like that I'm working with. Thanks.

 

Screen Shot 2016-10-17 at 11.31.44 AM.png

Screen Shot 2016-10-17 at 11.45.50 AM.png

Link to comment

The coordinates are relative to the screen, not the active window (i.e. your script clicks  70, 150 pixels from the top left of the screen, not the top left of the active window). You need to add your X and Y to the position of the window.

 

This function clicks on the frontmost window using window-relative coordinates:

 

-- Click relative to location of frontmost window

on clickInWindow(X, Y)

    tell current application to set theBounds to the bounds of front window

    set posX to (item 1 of theBounds) + X

    set posY to (item 2 of theBounds) + Y

    tell application "System Events" to click at {posX, posY}

end clickInWindow

 

clickInWindow(70, 150)

delay 1

clickInWindow(160, 230)

 

Link to comment

I'm on the frontmost window, the coordinates are of the screen, which I got using ⌘⇧4 to get my cursor position. So in this little script you posted, am I suppose to insert any information or use it as is. I tried and still nothing happens.

Link to comment

There's no point using it if your window is always in the same position. That script just allows you to click in the right place regardless of where the window is on screen.

 

I think you need to allow Alfred to use Accessibility features (System Preferences > Security & Privacy > Accessibility) to let it run scripts that click on other apps.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...