Skip to content

Instantly share code, notes, and snippets.

@Ptujec
Last active February 21, 2023 19:24
Show Gist options
  • Save Ptujec/80aa113794d8fba1d5ac5a5caffd3ef1 to your computer and use it in GitHub Desktop.
Save Ptujec/80aa113794d8fba1d5ac5a5caffd3ef1 to your computer and use it in GitHub Desktop.
Menu Bar Item Navigation in LaunchBar
/*
Menu Bar Items Action for LaunchBar
using executable from Alfred Workflow: https://github.com/BenziAhamed/Menu-Bar-Search
I acutally forked it and made some changes: https://github.com/Ptujec/Menu-Bar-Search
by Christian Bender (@ptujec)
2023-02-17
*/
function run() {
var menuPath = Action.path + '/Contents/Resources/menu';
var output = LaunchBar.execute(menuPath);
output = JSON.parse(output);
// File.writeJSON(output, Action.supportPath + '/test.json');
var recentItems = Action.preferences.recentItems;
var resultAll = [];
var resultRecent = [];
output.items.forEach(function (item) {
// Icon fix for Apps without a working icon. LB usually takes the icon from the App path. But it is not working if that is no icns file in the app bundle
var icon = item.icon.path;
if (icon.includes('Wrapper')) {
icon = 'iconTemplate';
}
var sub = item.uid.split('>').splice(1).join(' > ');
var appId = item.uid.split('>')[0];
var uid = item.uid;
var pushData = {
title: item.title,
subtitle: sub,
icon: icon,
action: 'click',
actionArgument: {
uid: uid,
appId: appId,
},
actionRunsInBackground: true,
};
if (item.badge != undefined) {
pushData.badge = item.badge;
}
// Priorize recent item
var recent = false;
if (recentItems != undefined) {
recentItems.forEach(function (item) {
if (item.uid == uid) {
recent = true;
}
});
}
if (recent == true) {
// pushData.label = 'recent';
resultRecent.push(pushData);
} else {
// Exclude certain menu trees
if (
!sub.includes('> Services') &&
!sub.includes('> Dienste') &&
!sub.includes('> Open Recent') &&
!sub.includes('Verlauf >') &&
!sub.includes('> Benutzte Dokumente')
) {
resultAll.push(pushData);
}
}
});
var result = resultRecent.concat(resultAll);
return result;
}
function click(dict) {
// Hide LaunchBar Interface if not frontmost
if (dict.appId != 'at.obdev.LaunchBar') {
LaunchBar.hide();
}
// Remember Used Menu Item (per App)
var recentItems = Action.preferences.recentItems;
if (recentItems == undefined) {
recentItems = [];
}
recentItems.forEach(function (item, index) {
if (item.appId == dict.appId) {
recentItems.splice(index, 1);
}
});
recentItems.push({
appId: dict.appId,
uid: dict.uid,
});
Action.preferences.recentItems = recentItems;
// Click Menu Item
var levels = dict.uid.split('>');
if (levels.length === 5) {
var clickMenu =
'menu item "' +
levels[4] +
'" of menu "' +
levels[3] +
'" of menu item "' +
levels[3] +
'" of menu "' +
levels[2] +
'" of menu item "' +
levels[2] +
'" of menu "' +
levels[1] +
'" of menu bar item "' +
levels[1] +
'" of menu bar 1 ';
} else if (levels.length === 4) {
var clickMenu =
'menu item "' +
levels[3] +
'" of menu "' +
levels[2] +
'" of menu item "' +
levels[2] +
'" of menu "' +
levels[1] +
'" of menu bar item "' +
levels[1] +
'" of menu bar 1 ';
} else {
var clickMenu =
'menu item "' +
levels[2] +
'" of menu "' +
levels[1] +
'" of menu bar item "' +
levels[1] +
'" of menu bar 1 ';
}
var appleScript =
'tell application "System Events" to tell (process 1 where frontmost is true) to click ' +
clickMenu;
LaunchBar.executeAppleScript(appleScript);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment