diff --git a/pd/nw/index.js b/pd/nw/index.js
index fa929b0ff42e9a7fa1110f8fdf0f2d083774293b..33ea2c1c053b6dc5a17db506ca2707b38c60bc73 100644
--- a/pd/nw/index.js
+++ b/pd/nw/index.js
@@ -3,9 +3,15 @@ var gui = require("nw.gui");
 var pdgui = require("./pdgui.js");
 var pd_menus = require("./pd_menus.js");
 
-// we're using pwd in fileDialog. If you start Pd by clicking the app bundle
-// in OSX, the PWD environment variable doesn't exist. In that case we use
-// HOME instead.
+// We're using the following pwd variable as the default dir in which various
+// file dialogs open, so we need to initialize it in some way. The following
+// provides a reasonable default (use the PWD if it's available, otherwise
+// fall back to HOME, or HOMEPATH on Windows). This will be updated later by
+// the engine through pdgui.gui_set_cwd() once the engine has completed its
+// startup.
+
+// If you start Pd by clicking the app bundle in OSX, the PWD environment
+// variable doesn't exist. In that case we use HOME instead.
 var pwd = process.env.PWD !== undefined ? process.env.PWD : process.env.HOME;
 
 // Windows doesn't have either of the environment variables above, so we
@@ -35,7 +41,7 @@ function set_vars(win) {
         pd_engine_id = gui.App.argv[4];
     } else {
         // If we're starting Pd, this is the first port number to try. (We'll
-        // increment it if that port happens to be taken.
+        // increment it if that port happens to be taken.)
         port_no = 5400;
     }
     pdgui.set_port(port_no);
@@ -89,8 +95,7 @@ function connect() {
         pdgui.connect_as_client();
     } else {
         // create a tcp server, then spawn Pd with "-guiport" flag and port
-        gui_path = window.location.pathname;
-        gui_path = gui_path.substr(0, gui_path.lastIndexOf('/'));
+        gui_path = process.cwd();
         // On OSX, if the user double-clicks a pd file we get a single
         // argument prefixed with "file://". If so, we parse it and
         // feed it to the Pd instance as an argument to the "-open" flag
@@ -283,7 +288,7 @@ function add_events() {
                 // 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)));
+                pdgui.menu_open(decodeURI(argv_string.slice(7)));
         } else {
                 // Otherwise we assume that the Pd process tried to
                 // open the GUI, supplying us with a port number and
diff --git a/pd/nw/pdgui.js b/pd/nw/pdgui.js
index a90235bac994f58b372108603769a169fb2f36d1..301661e3d9d99241e65eb3391949e9d145b4cbb7 100644
--- a/pd/nw/pdgui.js
+++ b/pd/nw/pdgui.js
@@ -1095,19 +1095,9 @@ function external_doc_open(url) {
 exports.external_doc_open = external_doc_open;
 
 function gui_set_cwd(dummy, cwd) {
-    // The check for "darwin" is a quick workaround for getting
-    // the OSX App bundle to start the pwd in the user's working directory
-    // instead of deep inside the App bundle itself.  However, this may
-    // become a problem when people try to install Purr Data "Linux"-style,
-    // that is, install it system-wide from the command line instead of
-    // in an App bundle.
-
-    // Also, there is a general problem that we're setting the pwd from
-    // two different places-- index.js in set_vars() and s_inter.c with
-    // this call.  That's unnecessarily complex and hard to follow. It
-    // should be simplified
-    if (cwd !== "." && process.platform !== "darwin") {
+    if (cwd !== ".") {
         pwd = cwd;
+        post("working directory is " + cwd);
     }
 }
 
@@ -1442,7 +1432,7 @@ function spawn_pd(gui_path, port, file_to_open) {
     if (platform === "darwin") {
         // OSX -- this is currently tailored to work with an app bundle. It
         // hasn't been tested with a system install of pd-l2ork
-        pd_binary = path.join("bin", "pd-l2ork");
+        pd_binary = path.join(gui_path, "bin", "pd-l2ork");
         if (file_to_open) {
             flags.push("-open", file_to_open);
         }
@@ -1451,6 +1441,13 @@ function spawn_pd(gui_path, port, file_to_open) {
         flags.push("-nrt"); // for some reason realtime causes watchdog to die
     }
     post("binary is " + pd_binary);
+    // AG: It isn't nice that we change the cwd halfway through the startup
+    // here, but since the GUI launches the engine if we come here (that's how
+    // it works on the Mac), we *really* want to launch the engine in the
+    // user's home directory and not in some random subdir of the OSX app
+    // bundle. Note that to make that work, the pd-l2ork executable needs to
+    // be invoked using an absolute path (see above).
+    process.chdir(process.env.HOME);
     var child = cp.spawn(pd_binary, flags, {
         stdio: "inherit",
         detached: true