From 1c66d7e9e185f72310dead3f1868e9ef6cde1b52 Mon Sep 17 00:00:00 2001
From: Jonathan Wilkes <jon.w.wilkes@gmail.com>
Date: Sun, 20 Nov 2016 14:05:13 -0500
Subject: [PATCH] fix #163: Opening pd file in Mac Finder hangs Purr-Data

---
 pd/nw/index.js | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/pd/nw/index.js b/pd/nw/index.js
index ee164b871..b914ec74f 100644
--- a/pd/nw/index.js
+++ b/pd/nw/index.js
@@ -29,7 +29,7 @@ function set_vars(win) {
     // If the GUI was started by Pd, our port number is going to be
     // the first argument. If the GUI is supposed to start Pd, we won't
     // have any arguments and need to set it here.
-    if (have_args()) {
+    if (have_args() && gui.App.argv.length > 1) {
         port_no = gui.App.argv[0]; // fed to us by the Pd process
         // looks like this is the same as pwd below
         gui_dir = gui.App.argv[3];
@@ -91,7 +91,7 @@ function nw_window_focus_callback() {
 
 function connect() {
     var gui_path;
-    if (have_args()) {
+    if (have_args() && gui.App.argv.length > 1) {
         // Pd started the GUI, so connect to it on port provided in our args
         pdgui.post("Pd has started the GUI");
         pdgui.connect_as_client();
@@ -101,6 +101,23 @@ function connect() {
         gui_path = gui_path.substr(0, gui_path.lastIndexOf('/'));
         pdgui.post("GUI is starting Pd...");
         pdgui.connect_as_server(gui_path);
+        if (have_args()) {
+            // Quick bugfix for OSX-- handle case where user clicks on a
+            // file and Pd hasn't been started yet.
+            window.setTimeout(function () {
+                if (gui.App.argv[0].slice(0, 7) === "file://") {
+                // 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.
+                // Selecting multiple files and clicking "Open" will trigger
+                // a separate "open" event for each file, so luckily we don't
+                // have to parse them.
+                    pdgui.menu_open(decodeURI(argv_string.slice(7)));
+                }
+            }, 2000);
+        }
     }
 }
 
-- 
GitLab