nsIMacDockSupport

Provides access to the Dock on Mac OS X.
1.0
66
Introduced
Gecko 2.0
Inherits from: nsIMacDockSupport Last changed in Gecko 11.0 (Firefox 11.0 / Thunderbird 11.0 / SeaMonkey 2.8)

Implemented by: @mozilla.org/cookie-monster;1. To create an instance, use:

var dock = Components.classes["@mozilla.org/widget/macdocksupport;1"]
                    .getService(Components.interfaces.nsIMacDockSupport);

See Working with the Mac OS X Dock for details and examples.

Method summary

void activateApplication(in boolean aIgnoreOtherApplications);

Attributes

Attribute Type Description
badgeText AString Text to display in a badge on the application's dock icon. This can be used, for example, to display the number of unread messages in an email client.
dockMenu nsIStandaloneMenu The menu to display when the user right-clicks on the application's icon in the dock.

Methods

activateApplication()

Activates the application, making it the frontmost application. The application should call this to activate itself when one of its dock menu items are selected, since doing so does not automatically activate the application.

void activateApplication(
  in boolean aIgnoreOtherApplications
);
Parameters
aIgnoreOtherApplications
If true, the application is activated regardless of the state of other applications. If false, the application is only activated if other applications are not currently active.

About dockMenu

By default Firefox adds two menu items to the dock menu. The menu items are "New Window" and "New Private Window". If permanent private browsing mode is enabled then the "New Private Window" menu item is hidden. This is created by using the dockMenu attribute of nsIMacDockSupport here: http://mxr.mozilla.org/mozilla-release/source/browser/base/content/browser.js#1562

This is seen here:

Graphic of default native menu created on browser startup

If you were to copy and follow that example you would replace the default native menu. This can be done like this:

var dockSupport = Cc['@mozilla.org/widget/macdocksupport;1'].getService(Ci.nsIMacDockSupport);
console.log('dockSupport:', dockSupport);

var win = Services.wm.getMostRecentWindow('navigator:browser');
var macMenu = win.document.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', 'menupopup');
macMenu.setAttribute('id', 'myMacMenu');
var macMenuItem = win.document.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', 'menuitem');
macMenuItem.setAttribute('label', 'Show Most Recent Window');
macMenuItem.setAttribute('id', 'myMacMenuItem');
macMenuItem.addEventListener('command', function(){
  var dockSupport = Cc['@mozilla.org/widget/macdocksupport;1'].getService(Ci.nsIMacDockSupport);
  dockSupport.activateApplication(true);
  Services.wm.getMostRecentWindow(null).focus()
})
macMenu.appendChild(macMenuItem);
var mainPopupSet = win.document.getElementById('mainPopupSet');
mainPopupSet.appendChild(macMenu);

let dockMenuElement = macMenu; //document.getElementById("menu_mac_dockmenu");66
let nativeMenu = Cc["@mozilla.org/widget/standalonenativemenu;1"].createInstance(Ci.nsIStandaloneNativeMenu);

console.log('dockMenuElement:', dockMenuElement);

nativeMenu.init(dockMenuElement);
dockSupport.dockMenu = nativeMenu;

This replaces the default menu with this one menuitem that says "Show Most Recent Window". Notice the activateApplication(true), if this is not done then Firefox will not be activated. The result of this code is shown below.

Graphic of native menu replacement

Notice how the "New Window" and "New Private Window" menuitem's are missing. Therefore, if you would like to add or remove items to the menu it is recommended to manipulate the default menu item which is on the hidden window of Firefox. Note: The hidden window of Firefox (Services.appShel.hiddenDOMWindow) loads on browser startup, so if you would like to access it on startup of the browser make sure to check and wait for the window to be loaded. This can be done by adding a event listenr for load of the hidden window.

This is the recommended way to add to the native menu, just maniuplate the default menu DOM element.

var macMenuItem = Services.appShell.hiddenDOMWindow.document.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', 'menuitem');
macMenuItem.setAttribute('label', 'Show Most Recent Window');
macMenuItem.setAttribute('id', 'myMacMenuItem');
macMenuItem.addEventListener('command', function(){
  var dockSupport = Cc['@mozilla.org/widget/macdocksupport;1'].getService(Ci.nsIMacDockSupport);
  dockSupport.activateApplication(true);
  Services.wm.getMostRecentWindow(null).focus()
})
Services.appShell.hiddenDOMWindow.document.getElementById('menu_mac_dockmenu').appendChild(macMenuItem)

This adds the "Show Most Recent Window" menuitem from the previous example as a third item. Two types of elements:

  • menu (if you would like to have sub menu items, make sure to set a label on the "menu" element)

  • menuitem (like in example above)

Elements can be hidden by adding the hidden attribute to the menu or menuitem and setting it to true.

See also