Skip to content

Instantly share code, notes, and snippets.

@RobTrew
Last active January 16, 2023 20:15
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save RobTrew/575111796d0e3bf9bf3a to your computer and use it in GitHub Desktop.
Save RobTrew/575111796d0e3bf9bf3a to your computer and use it in GitHub Desktop.
Yosemite JXA Javascript Function for clicking application sub-menu items
// Click an OS X app sub-menu item
// 2nd argument is an array of arbitrary length (exact menu item labels, giving full path)
// e.g. menuItemClick("InqScribe", ['View', 'Aspect Ratio', 'Use Media Ratio'])
// Note that the menu path (spelling & sequence) must be exactly as in the app
// See menuItemTestClick() below for a slower version which reports any errors
// For macOS Yosemite to Sierra
(function () {
'use strict';
// menuItemClick :: String -> [String] -> IO Bool
var menuItemClick = function (strAppName, lstMenuPath) {
var intMenuPath = lstMenuPath.length;
if (intMenuPath > 1) {
var appProcs = Application('System Events')
.processes.where({
name: strAppName
}),
procApp = appProcs.length ? appProcs[0] : undefined;
if (procApp) {
Application(strAppName)
.activate();
lstMenuPath.slice(1, -1)
.reduce(function (a, x) {
return a.menuItems[x].menus[x];
}, procApp.menuBars[0].menus.byName(lstMenuPath[0]))
.menuItems[lstMenuPath[intMenuPath - 1]].click();
return true;
}
return false;
}
return false;
};
// e.g.
//menuItemClick('InqScribe', ['Transcript', 'Transcript Settings...']);
menuItemClick("InqScribe", ['View', 'Aspect Ratio', 'Use Media Ratio'])
})();
@kjaymiller
Copy link

Is it okay to use this code? There is no license associated with it, so I have to assume no.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment