Skip to content

Instantly share code, notes, and snippets.

@WRonX
Last active November 24, 2022 14:36
Show Gist options
  • Save WRonX/79e3a2f55dfcb2d8a71c8e9340adb60d to your computer and use it in GitHub Desktop.
Save WRonX/79e3a2f55dfcb2d8a71c8e9340adb60d to your computer and use it in GitHub Desktop.
Learning Python: a script for activating Slack after some idle period (on Windows).
import time
import pyautogui as gui
import win32api
from pygetwindow._pygetwindow_win import Win32Window
def getIdleSeconds():
return int((win32api.GetTickCount() - win32api.GetLastInputInfo()) / 1000.0)
def getSlackWindow():
windows = gui.getWindowsWithTitle('Slack |')
if not windows:
return None
return windows[0]
def pingSlack():
slackWindow = getSlackWindow()
if not type(slackWindow) is Win32Window:
justMove()
return None
try:
slackWindow.activate()
except:
slackWindow.minimize()
slackWindow.restore()
slackWindow.activate()
with gui.hold('ctrlleft'):
gui.press('k')
gui.write('@me')
gui.press('enter')
gui.write('\b')
def justMove():
gui.move(10, 10)
time.sleep(1)
gui.move(-10, -10)
while True:
if getIdleSeconds() > 300:
activeWindow = gui.getActiveWindow()
activeWindow.activate()
pingSlack()
time.sleep(300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment