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?