Type, Click and Use Menus In Shortcuts

Shortcuts doesn't seem to allow you to select menu items, simulate key presses or click buttons. But you can do those things with some scripting and add them to your Shortcuts that way. It takes some skill and patience to get what you want.

Click a menu item

var app = Application("System Events").processes["TextEdit"];
app.menuBars[0].menus.byName("File").menuItems[ "New" ].click();

Click a menu item in a submenu

var app = Application("System Events").processes["TextEdit"];
app.menuBars[0].menus.byName("Edit").menuItems[ "Insert" ].menus[ "Insert" ].menuItems[ "Line Break" ].click();

Press a key

Application("System Events").keystroke('a')

Press a key with a modifier

Application("System Events").keystroke('a', { using: ['command down'] })

Press the left arrow key:

Application("System Events").keyCode(123)
// 124=right, 125=down, 126=up, 36=Return

Click General in System Settings

Application("System Settings").activate()
var app = Application("System Events").processes["System Settings"];
// Note: General may not be the 12th item for you,
// you'll need to experiment

Print and then Click PDF in TextEdit:

Application("System Events").keystroke('p', { using: ['command down'] })
Application("System Events").processes["TextEdit"].windows[0].sheets[0].splitterGroups[0].groups[1].buttons[0].click()
// Note: The PDF button may not be at this location
// depending on many factors

Script Editor JavaScript to help find user interface elements:

// Note this is JavaScript so make sure your
// Script Editor window is set to JavaScript
// NOT AppleScript

listObjects(Application("System Events").processes["System Settings"].windows(), "" )

function listObjects(uiObjects, indentStr) {
    for (var i in uiObjects) {
		if(uiObjects[i] instanceof Array) {
			listObjects(uiObjects[i], indentStr + '  ')
		} else if(uiObjects[i] instanceof Object){
			console.log('' + indentStr + i + ' Name:' + uiObjects[i].name() + ', Class:' + uiObjects[i].class() + ', Desc:' + uiObjects[i].description() + ', Value:' + uiObjects[i].value());
			listObjects(uiObjects[i].uiElements(), indentStr + '  ')

Comments: 2 Responses to “Type, Click and Use Menus In Shortcuts”

    Ken Nellis
    1 year ago

    Very clever, Gary. Thank you! I guess we should presume that those indices (e.g. 12 for the General System Settings selection) might change in future macOS updates.

    1 year ago

    Ken: Not just in future updates, but from Mac to Mac. You will have different items in a MacBook vs a Mac mini, for instance. You may see different things depending on your settings and what you have installed too.

Comments Closed.