From 1f44801edbbfa5935df1c05072e16b4348260c56 Mon Sep 17 00:00:00 2001 From: Christian Colglazier Date: Thu, 24 Sep 2020 20:46:15 -0400 Subject: [PATCH] Move Firefox windows to proper workspaces --- scripts/start-firefox.py | 61 +++++++++++++++++++++++++++++++++++ scripts/system-start-audio.sh | 4 +-- 2 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 scripts/start-firefox.py diff --git a/scripts/start-firefox.py b/scripts/start-firefox.py new file mode 100644 index 0000000..1d014bb --- /dev/null +++ b/scripts/start-firefox.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 + +from i3ipc import Connection, Event +import os + +# moveWindowToWorkspace() moves a given window to a given workspace. +def moveWindowToWorkspace(window, workspace): + window.command('move window to workspace ' + workspace) + +# getWindows() returns a list of all open windows on the desktop. +def getWindows(i3): + windows = [] + for con in i3.get_tree(): + if con.window and con.parent.type != 'dockarea': + windows.append(con) + return windows + +# getWindowByName() returns a window with the given name. +def getWindowByName(name, windows): + for win in windows: + if name in win.name: + return win + return + +# filterWindowsByClass() returns a filter list of windows by class. +def filterWindowsByClass(windowClass, windows): + return [w for w in windows if w.window_class == windowClass] + +# doesWindowExist() returns if a given window exists. +def doesWindowExist(window): + return window != None + +# launchProgram() launches a program on a given workspace. +def launchProgram(program, workspace): + i3.command('workspace ' + workspace + '; exec ' + program) + +# isProgramRunning() returns if a program is running and if it is +# moves it to a given workspace. +def isProgramRunning(name, windows, workspace): + program = getWindowByName(name, windows) + if doesWindowExist(program): + moveWindowToWorkspace(program, workspace) + return True + return False + +i3 = Connection() +firefoxWindows = filterWindowsByClass('Firefox', getWindows(i3)) + +# Music +if not isProgramRunning('YouTube Music', firefoxWindows, '10'): + launchProgram('exec firefox --new-window music.youtube.com', '10') + +# Stocks +if not isProgramRunning('Robinhood', firefoxWindows, '10'): + os.system('python ~/.config/scripts/launch-stocks-tracker.py') + + +# YouTube +if not isProgramRunning('Subscriptions - YouTube', firefoxWindows, '10'): + launchProgram('firefox --new-window youtube.com/feed/subscriptions', '10') + diff --git a/scripts/system-start-audio.sh b/scripts/system-start-audio.sh index bfed0bb..bb3b4c7 100644 --- a/scripts/system-start-audio.sh +++ b/scripts/system-start-audio.sh @@ -27,9 +27,7 @@ function launchi3() { if [ -z "$skipi3" ]; then echo Opening i3wm sound workspaces sleep .1 && i3-msg 'workspace 5; exec firefox' - sleep .1 && i3-msg 'workspace 10; exec firefox --new-window music.youtube.com' - sleep .1 && python python ~/.config/scripts/launch-stocks-tracker.py - sleep .1 && i3-msg 'workspace 10; exec firefox --new-window youtube.com/feed/subscriptions' + sleep .1 && python ~/.config/scripts/start-firefox.py fi }