From be50346186f0769b718b82256a3eac3031e54088 Mon Sep 17 00:00:00 2001
From: Jonathan Wilkes <jon.w.wilkes@gmail.com>
Date: Wed, 9 Nov 2016 21:08:20 -0500
Subject: [PATCH] add case for opening file in a running Pd instance with an
 OSX app bundle

---
 pd/nw/index.js | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/pd/nw/index.js b/pd/nw/index.js
index c05d91d60..72e965dfd 100644
--- a/pd/nw/index.js
+++ b/pd/nw/index.js
@@ -261,11 +261,26 @@ function add_events() {
     );
     // Opening another file
     nw.App.on("open", function(argv_string) {
-        var port, pd_engine_id;
-        port = argv_string.split(" ").slice(-5, -4);
-        pd_engine_id = argv_string.split(" ").slice(-1);
-        pdgui.connect_as_client_to_secondary_instance("localhost", port,
-            pd_engine_id);
+        var port,
+            pd_engine_id,
+            argv = argv_string.split(" ");
+        if (argv.length) {
+            if (argv.length === 1) {
+                // Clicking on a Pd file with an installed OSX app bundle sends
+                // a single argument which is a file:// URI.
+                // With the OSX app bundle it is the GUI which starts the
+                // Pd process. So in this case, we just need to parse the
+                // file and open it.
+                if (argv.slice(0, 7) === "file://") {
+                    pdgui.menu_open(decodeURI(argv.slice(7)));
+                }
+            } else {
+                port = +argv.slice(-5, -4);
+                pd_engine_id = argv.slice(-1);
+                pdgui.connect_as_client_to_secondary_instance("localhost", port,
+                    pd_engine_id);
+            }
+        }
     });
     // Browser Window Close
     gui.Window.get().on("close", function () {
-- 
GitLab