[qutebrowser] How to run a userscript in Windows

Daniel Schadt daniel at kingdread.de
Tue Sep 6 01:13:57 CEST 2016


This one works for me:

    C:\Users\Daniel\AppData\Local\qutebrowser\data\userscripts\test.bat
    ----
    echo ":message-info Test" >> %QUTE_FIFO%

Running  :spawn --userscript test.bat  correctly shows "Test" as a message.
However, I had to apply a patch to get it to work. The current
implementation
seems to suffer from the same problem that :open-editor once had[1]: The
"FIFO"
file it creates is being held open by qutebrowser, which means that (on
Windows)
other processes can't write to it (stuff like commands) (patch at the
end of the
email).

Unfortunately though, it seems like this is not the cause of your problem. A
failing userscript gives the message "userscript exited with status 1", not
"userscript failed to start". Also, I've tested starting .bat files with
QProcess and the CreateProcess C call, and it worked, so the issue seems
to be
something different, and probably hard to debug, as I can't seem to
reproduce it
on my machine.

What should definitely work is to copy C:\Windows\system32\notepad.exe
to your
userscript directory and then  :spawn --userscript notepad.exe. You
could try
that just for fun, it should open an empty notepad window. If you don't
want to
copy notepad, you can also try
    :spawn --userscript 'C:\Windows\system32\notepad.exe'
and see if an error occurs.

Daniel

[1]: https://github.com/The-Compiler/qutebrowser/issues/1767

Patch:

diff --git a/qutebrowser/commands/userscripts.py
b/qutebrowser/commands/userscripts.py
index 0a8b195..65af026 100644
--- a/qutebrowser/commands/userscripts.py
+++ b/qutebrowser/commands/userscripts.py
@@ -280,14 +280,10 @@ class _WindowsUserscriptRunner(_BaseUserscriptRunner):

     This also means the userscript *has* to use >> (append) rather than >
     (overwrite) to write to the file!
-
-    Attributes:
-        _oshandle: The oshandle of the temp file.
     """

     def __init__(self, win_id, parent=None):
         super().__init__(win_id, parent)
-        self._oshandle = None

     def _cleanup(self):
         """Clean up temporary files after the userscript finished."""
@@ -301,12 +297,7 @@ class _WindowsUserscriptRunner(_BaseUserscriptRunner):
         except OSError:
             log.procs.exception("Failed to read command file!")

-        try:
-            os.close(self._oshandle)
-        except OSError:
-            log.procs.exception("Failed to close file handle!")
         super()._cleanup()
-        self._oshandle = None
         self.finished.emit()

     @pyqtSlot()
@@ -323,7 +314,9 @@ class _WindowsUserscriptRunner(_BaseUserscriptRunner):
         self._kwargs = kwargs

         try:
-            self._oshandle, self._filepath = tempfile.mkstemp(text=True)
+            handle = tempfile.NamedTemporaryFile(delete=False)
+            handle.close()
+            self._filepath = handle.name
         except OSError as e:
             message.error(self._win_id, "Error while creating tempfile: "
                                         "{}".format(e))




More information about the qutebrowser mailing list