1

I have the following Python code running on Python version 2.7 and Windows.

The code basically grabs the URL from my active Google Chrome tab, then runs and passes the URL as an argument to a Windows batch file.

import os
import win32gui
import win32con

hwnd = win32gui.GetForegroundWindow()

omniboxHwnd = win32gui.FindWindowEx(hwnd, 0, 'Chrome_OmniboxView', None)

def getWindowText(hwnd):
    buf_size = 1 + win32gui.SendMessage(hwnd, win32con.WM_GETTEXTLENGTH, 0, 0)
    buf = win32gui.PyMakeBuffer(buf_size)
    win32gui.SendMessage(hwnd, win32con.WM_GETTEXT, buf_size, buf)
    return str(buf)

url = getWindowText(hwnd)

exe = 'run.bat "%s"' %(url)

os.system(exe)

However, running the code from the command line throws the following error:

TypeError: system() argument 1 must be string without null bytes, not str

Is there something that I'm missing out here?

EDIT: Manually inputting a URL string works, i.e. replacing

url = getWindowText(hwnd)

with

url = "https://stackoverflow.com/questions/39976936/"

works, so it seems that the problem lies with how the code grabs the URL string from the window. I took the code from How do I get the URL of the active Google Chrome tab in Windows? and I'm not familiar with it at all. Is there an alternative way to grab the URL?

Community
  • 1
  • 1
  • 3
    Possible duplicate of [TypeError: must be string without null bytes, not str](http://stackoverflow.com/questions/18265084/typeerror-must-be-string-without-null-bytes-not-str) – Jérôme Oct 11 '16 at 12:14
  • I've tried adding r before my exe string, but the error still pops up. – thefinshark Oct 11 '16 at 12:15
  • 1
    The problem is not the string literal `'run.bat "%s"'` but `url`, which probably contains the null-byte. – Ilja Everilä Oct 11 '16 at 12:16
  • I understand where you're coming from, but how should I edit url? Stripping url doesn't seem to have any effect. – thefinshark Oct 11 '16 at 12:18
  • 1
    Try `url.strip('\x00')`. This on the other hand will not help, if the null-byte is somewhere in the middle. Simple `str.strip()` call will remove whitespace, which a null-byte is not. – Ilja Everilä Oct 11 '16 at 12:19
  • 2
    **GetWindowText function** *Copies the text of the specified window's title bar (if it has one) into a buffer. If the specified window is a control, the text of the control is copied.* **However, `GetWindowText` cannot retrieve the text of a control in another application**. And further *To retrieve the text of a control in another process, send a WM_GETTEXT message directly instead of calling `GetWindowText`* –  Oct 11 '16 at 12:23
  • 1
    To remove null bytes you can always do `url.replace('\x00', '')`. But this only hides the issue. You should *not* have null bytes in the first place. There's probably something bogus with the logic used to create the buffer/string. – Bakuriu Oct 11 '16 at 12:27
  • @IljaEverilä, it seems like it didn't work. – thefinshark Oct 11 '16 at 12:28
  • @Bakuriu, I got it from here http://stackoverflow.com/questions/11645123/how-do-i-get-the-url-of-the-active-google-chrome-tab-in-windows. The `replace` doesn't seem to work either. – thefinshark Oct 11 '16 at 12:30
  • You should also include the full traceback, not just the final error in your post. May not be useful in this particular case, but it's a good habit. – Ilja Everilä Oct 11 '16 at 12:45
  • (I would like to retract duplicate flag but I can't.) – Jérôme Oct 11 '16 at 12:58

0 Answers0