Files
wiki/x11/ahk.py
2025-11-11 02:38:21 +09:00

87 lines
3.1 KiB
Python
Executable File

#!/usr/bin/env python3
import os, sys, subprocess
os.environ['DISPLAY'] = ':0'
def cmd(*args): return subprocess.run([str(a) for a in args], capture_output=True, text=True).stdout.strip()
def tile_window(position):
window_id = subprocess.run(['xdotool', 'getactivewindow'],
capture_output=True, text=True).stdout.strip()
cmd('wmctrl', '-i', '-r', window_id, '-b', 'remove,maximized_vert,maximized_horz')
if position == 'maximize':
cmd('wmctrl', '-i', '-r', window_id, '-b', 'add,maximized_vert,maximized_horz')
return
SCREEN_WIDTH, SCREEN_HEIGHT, TASKBAR = 3840, 2160, 48
work_height = SCREEN_HEIGHT - TASKBAR
quarter = SCREEN_WIDTH // 4
positions = {
'left': (0, 0, quarter, work_height),
'center': (quarter, 0, quarter * 2, work_height),
'right': (quarter * 3, 0, quarter, work_height),
}
x, y, width, height = positions[position]
if 'xfce4-terminal' in cmd('xprop', '-id', window_id, 'WM_CLASS').lower():
height -= 10
cmd('xdotool', 'windowsize', window_id, width, height)
cmd('xdotool', 'windowmove', window_id, x, y)
def XButton1():
for line in cmd('xdotool', 'getmouselocation', '--shell').splitlines():
if 'WINDOW=' in line:
window = line.split('=')[1]
cmd('xdotool', 'windowactivate', '--sync', window)
classname = cmd('xdotool', 'getwindowclassname', window)
name = cmd('xdotool', 'getwindowname', window)
if classname == 'Xfce4-terminal': key = 'alt+F4'
elif name.startswith('New tab - obsidian'): key = 'alt+F4'
else: key = 'ctrl+w'
cmd('xdotool', 'key', key)
def popup(message): cmd("zenity", "--info", f"--text={message}", "--timeout=1")
def F1():
import random,time
SCREEN_WIDTH, SCREEN_HEIGHT, TASKBAR = 3840, 2160, 48
work_height = SCREEN_HEIGHT - TASKBAR
quarter = SCREEN_WIDTH // 4
x, y, w, h = 0, 0, quarter, work_height
cmd("chromium-browser",
"--disable-features=ExtensionManifestV2Unsupported,ExtensionManifestV2Disabled", "--new-window", "https://yauk.net")
time.sleep(0.1)
window_id = cmd('xdotool', 'getactivewindow')
cmd('xdotool', 'windowsize', window_id, w, h)
cmd('xdotool', 'windowmove', window_id, x, y)
def F12():
if ret := subprocess.run(['wmctrl', '-x', '-a', 'obsidian']).returncode:
cmd('dbus-launch', 'flatpak', 'run', 'md.obsidian.Obsidian')
def F10():
cmd('killall', 'xbindkeys')
cmd('xbindkeys')
popup('Reload')
def Win():
cmd('xfce4-popup-whiskermenu')
def Win_1():
cmd("chromium-browser",
"--disable-features=ExtensionManifestV2Unsupported,ExtensionManifestV2Disabled")
if __name__ == '__main__':
match sys.argv[1]:
case 'Win+Up': tile_window('maximize')
case 'Win+Left': tile_window('left')
case 'Win+Down': tile_window('center')
case 'Win+Right': tile_window('right')
case 'Win': Win()
case 'Win+1': Win_1()
case 'F1': F1()
case "XButton1": XButton1()
case "XButton2": cmd("xdotool", "key", "ctrl+Tab")
case "F10": F10()
case "F12": F12()