/runscript flag doesn't work if Rhino started from subprocess.Popen

I can’t get Rhino to runscript via subprocess.Popen. It opens the file correctly, but doesn’t run any commands. It works fine when run from cmd prompt. Also doesn’t work when run from Cpython. Why does Rhino behave differently when opened via this script than if the same command is run from windows command line?

I’m trying to automate rhino to run scripts on multiple files. I’d rather spawn multiple processes than iterate one by one through each file with a single process.

I vaguely remember a similar problem when trying to automate AutoCAD - starting a batch file worked instead. Is this a MS-Windows specific issue?

import rhinoscriptsyntax as rs
import subprocess
import System

dlg = System.Windows.Forms.OpenFileDialog()
dlg.Multiselect = True
if dlg.ShowDialog()==System.Windows.Forms.DialogResult.OK:
    selected_files = dlg.FileNames

#attempt 1
for selected_file in selected_files:
    subprocess.Popen(["C:\\Program Files\\Rhino 8 WIP\\System\\Rhino.exe", "/nosplash",
                        '/runscript="-RunPythonScript (C:\\folders\\script.py) save exit"',
                        selected_file], shell=True)

#attempt 99 https://tenor.com/view/possum-opossum-mad-angry-gif-13439507
for selected_file in selected_files:
    subprocess.Popen(["C:\\Program Files\\Rhino 8 WIP\\System\\Rhino.exe", '/runscript="save exit"', "/nosplash", selected_file], shell=True)

As an insecure workaround saving the command to temporary .cmd files and Popening new processes to run them instead could work (but I’m still looking for a way to make Popen work).

import rhinoscriptsyntax as rs
import os
import subprocess
import System

dlg = System.Windows.Forms.OpenFileDialog()
dlg.Multiselect = True
if dlg.ShowDialog()==System.Windows.Forms.DialogResult.OK:
    selected_files = dlg.FileNames

for i, selected_file in enumerate(selected_files):
    with open(os.environ['TEMP'] + "\\madness" + str(i) + ".cmd", "w") as madness:
        madness.write('"C:\\Program Files\\Rhino 8 WIP\\System\\Rhino.exe" /nosplash /runscript="-RunPythonScript (C:\\folder\\script.py) save exit" ' + chr(34) + selected_file + chr(34))
        madness.flush()
        subprocess.Popen(madness.name, shell=True)

The solution - add start "" before calling Rhino.exe. I think that it then makes it look up Windows registry data that I assume included parts necessary to make /runscript or RunPythonScript to work.