From 2acec4184ae17bd064feab84792166040e4b09c5 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Mon, 9 Mar 2020 20:17:09 -0400 Subject: [PATCH] Use process id instead of bundle identifier to activate Mac app This prevents a second copy of Zotero from being launched if you're running the Zotero beta and click on a word processor plugin button while a dialog is already open. --- .../zotero/xpcom/utilities_internal.js | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/chrome/content/zotero/xpcom/utilities_internal.js b/chrome/content/zotero/xpcom/utilities_internal.js index 9f70593f05..1f49ee7a12 100644 --- a/chrome/content/zotero/xpcom/utilities_internal.js +++ b/chrome/content/zotero/xpcom/utilities_internal.js @@ -2031,13 +2031,6 @@ Zotero.Utilities.Internal.activate = new function() { return function(win) { if (Zotero.isMac) { - const BUNDLE_IDS = { - "Zotero":"org.zotero.zotero", - "Firefox":"org.mozilla.firefox", - "Aurora":"org.mozilla.aurora", - "Nightly":"org.mozilla.nightly" - }; - if (win) { Components.utils.import("resource://gre/modules/ctypes.jsm"); win.focus(); @@ -2075,7 +2068,13 @@ Zotero.Utilities.Internal.activate = new function() { ); }, false); } else { - Zotero.Utilities.Internal.executeAppleScript('tell application id "'+BUNDLE_IDS[Zotero.appName]+'" to activate'); + let pid = Zotero.Utilities.Internal.getProcessID(); + let script = ` + tell application "System Events" + set frontmost of the first process whose unix id is ${pid} to true + end tell + `; + Zotero.Utilities.Internal.executeAppleScript(script); } } else if(!Zotero.isWin && win) { Components.utils.import("resource://gre/modules/ctypes.jsm"); @@ -2289,16 +2288,26 @@ Zotero.Utilities.Internal.activate = new function() { Zotero.Utilities.Internal.sendToBack = function() { if (Zotero.isMac) { + let pid = Zotero.Utilities.Internal.getProcessID(); Zotero.Utilities.Internal.executeAppleScript(` tell application "System Events" - if frontmost of application id "org.zotero.zotero" then - set visible of process "Zotero" to false + set myProcess to first process whose unix id is ${pid} + if frontmost of myProcess then + set visible of myProcess to false end if end tell `); } } + +Zotero.Utilities.Internal.getProcessID = function () { + return Components.classes["@mozilla.org/xre/app-info;1"] + .getService(Components.interfaces.nsIXULRuntime) + .processID; +}; + + /** * Base64 encode / decode * From http://www.webtoolkit.info/