Skip to content

Instantly share code, notes, and snippets.

@hannahherbig
Created September 9, 2016 05:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hannahherbig/f3da7be23ed8b111f007b0a14a4e6a7b to your computer and use it in GitHub Desktop.
Save hannahherbig/f3da7be23ed8b111f007b0a14a4e6a7b to your computer and use it in GitHub Desktop.
write currently playing song in osu to a file - this code sucks though so don't use it
import ctypes
import time
import re
RE_TITLE = re.compile(r'''^osu\![^\-]+\-\s+(.+)$''')
user32 = ctypes.windll.user32
EnumWindows = user32.EnumWindows
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool,
ctypes.POINTER(ctypes.c_int),
ctypes.POINTER(ctypes.c_int))
GetWindowText = user32.GetWindowTextW
GetWindowTextLength = user32.GetWindowTextLengthW
IsWindowVisible = user32.IsWindowVisible
def get_windows():
titles = []
def foreach_window(hwnd, lParam):
if IsWindowVisible(hwnd):
length = GetWindowTextLength(hwnd)
buf = ctypes.create_unicode_buffer(length + 1)
GetWindowText(hwnd, buf, length + 1)
titles.append(buf.value)
return True
EnumWindows(EnumWindowsProc(foreach_window), 0)
return titles
last_np = ''
while True:
np = ''
for title in get_windows():
m = RE_TITLE.match(title)
if m:
np = m.group(1)
if last_np != np:
print(np)
with open('np.txt', 'w') as f:
f.write(np)
last_np = np
time.sleep(0.2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment