diff --git a/pd/nw/pdgui.js b/pd/nw/pdgui.js
index e3231e5039df36d3c20a7a6558fedfe7c955e277..23087bb349356137231f749b2ce13a0ca7a0989a 100644
--- a/pd/nw/pdgui.js
+++ b/pd/nw/pdgui.js
@@ -947,11 +947,8 @@ exports.set_app_quitfn = function(quitfn) {
     nw_app_quit = quitfn;
 }
 
-function open_file(file) {
-    var filename = defunkify_windows_path(file),
-        directory = path.dirname(filename),
-        basename = path.basename(filename),
-        cyclist;
+function import_file(directory, basename)
+{
     if (basename.match(/\.(pat|mxb|help)$/) !=null) {
         post("warning: opening pat|mxb|help not implemented yet");
         if (pd_nt == 0) {
@@ -976,9 +973,19 @@ function open_file(file) {
         //    puts stderr "converted Max binary to text format: $directory/$basename"
         //}
     }
+}
+
+function process_file(file, do_open) {
+    var filename = defunkify_windows_path(file),
+        directory = path.dirname(filename),
+        basename = path.basename(filename),
+        cyclist;
+    if (do_open) import_file(directory, basename);
     if (basename.match(/\.(pd|pat|mxt)$/i) != null) {
-        pdsend("pd open", enquote(basename),
-            (enquote(directory)));
+        if (do_open) {
+            pdsend("pd open", enquote(basename),
+                   (enquote(directory)));
+        }
         set_pd_opendir(directory);
         //::pd_guiprefs::update_recentfiles "$filename" 1
         // update the recent files list
@@ -987,6 +994,17 @@ function open_file(file) {
     }
 }
 
+function open_file(file) {
+    process_file(file, 1);
+}
+
+function gui_process_open_arg(file) {
+    // AG: This is invoked when the engine opens a patch file via the command
+    // line (-open). In this case the file is already loaded, so we just
+    // update the opendir and the recent files list.
+    process_file(file, 0);
+}
+
 function open_html(target) {
     nw_open_html(target);
 }
diff --git a/pd/src/s_main.c b/pd/src/s_main.c
index 91ae1cfe4582aa1f1bbf2b7d62e8f2dafffe3e66..b0b63c42d1923d9603ab4bc40bdd806f386565c5 100644
--- a/pd/src/s_main.c
+++ b/pd/src/s_main.c
@@ -171,8 +171,7 @@ static void openit(const char *dirname, const char *filename)
     {
         close (fd);
         glob_evalfile(0, gensym(nameptr), gensym(dirbuf));
-        gui_vmess("gui_set_current_dir", "xs",
-            0, filename);
+        gui_vmess("gui_process_open_arg", "s", filename);
     }
     else
         error("%s: can't open", filename);