From 4ded064e0d1a8e7bc8a325863d16b21ff3f0bd89 Mon Sep 17 00:00:00 2001
From: Jonathan Wilkes <jon.w.wilkes@gmail.com>
Date: Sun, 2 Oct 2016 12:45:31 -0400
Subject: [PATCH] fix regression where glist_select can send messages to the
 GUI before the window or its elements actually exist. (Same thing happens in
 Vanilla, but tcl/tk silently ignores tk canvas itemconfigure messages for
 non-existent objects)

---
 pd/nw/pdgui.js | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/pd/nw/pdgui.js b/pd/nw/pdgui.js
index 95965b400..ca21dce7a 100644
--- a/pd/nw/pdgui.js
+++ b/pd/nw/pdgui.js
@@ -1833,11 +1833,20 @@ function gui_text_redraw_border(cid, tag, x1, y1, x2, y2) {
 }
 
 function gui_gobj_select(cid, tag) {
-    var g = get_gobj(cid, tag);
-    if (g !== null) {
-        g.classList.add("selected");
-    } else {
-        console.log("text_select: something wrong with group tag: " + tag);
+    var g;
+    // We need to check if the window exists, because Pd will send
+    // messages to select the object before it (or the window) actually exists
+
+    // For example, this happens when using the "Find" menu. If Pd finds the
+    // match in a subpatch that isn't visible, it will open the subpatch and
+    // try to select the matching object before the subpatch has been mapped.
+    if (patchwin[cid]) {
+        g = get_gobj(cid, tag);
+        if (g !== null) {
+            g.classList.add("selected");
+        } else {
+            console.log("text_select: something wrong with group tag: " + tag);
+        }
     }
 }
 
-- 
GitLab