The i3 FAQ has migrated to https://github.com/i3/i3/discussions. All content here is read-only.
Ask Your Question
0

i3-py executing commands out of order

asked 2014-07-15 15:31:13 +0000

hbaughman gravatar image

I working on a script to switch the workspaces on my displays (similar to: http://i3wm.org/docs/user-contributed...). Approximately one time in five that I run the script, it executes my commands out of order causing some workspaces to be moved twice and others not at all. I was able to fix this by sleeping for 0.1s on each loop, but this is obviously not the ideal way to solve this bug.

How should I be ensuring that each line succeeds before the next is run?

#!/usr/bin/python2.7
from time import sleep
import i3

# save focused workspace
def find_active_workspace():
    workspaces = i3.get_workspaces()
    for workspace in workspaces:
        if workspace['focused'] == True:
            return workspace['name']
    else:
        return False
active_workspace = find_active_workspace()

# move everything
outputs = i3.get_outputs()
for output in range(0, 4):
    i3.workspace(outputs[output]['current_workspace'])
    i3.command('move', 'workspace to output up')
    sleep(0.1)

# restore focus to previously focused
sleep(0.1)
if active_workspace:
    i3.command("workspace", active_workspace)

I'm new to scripting so any other comments on the code would also be appreciated.

Thanks!

edit retag flag offensive close merge delete

Comments

1

i3-py is no longer maintained and has a lot of bugs. The current best scripting library is [i3ipc-python](https://github.com/acrisci/i3ipc-python). Even calling out to i3-msg would be better.

TonyC gravatar imageTonyC ( 2014-07-15 21:22:39 +0000 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-11-23 09:45:43 +0000

Michael Rose gravatar image

updated 2015-11-23 09:58:45 +0000

Some things are actually easier in shell example in fish

function get-ws-in
    i3-msg -t get_workspaces  | jq .[]  | jq -r "select(.$argv[2] == $argv[3]).$argv[1]"
end

function swap-windows
    set workspaces (get-ws-in name visible true)
    set outputs (get-ws-in output visible true)
    i3-msg "[workspace=$workspaces[1]] move container to workspace leftws"
    i3-msg "[workspace=$workspaces[2]] move container to workspace rightws"
    i3-msg "[workspace=leftws] move container to output $outputs[2]"
    i3-msg "[workspace=rightws] move container to output $outputs[1]"
end

or simplified to

function swap-windows2
    set workspaces (get-ws-in name visible true)
    set outputs (get-ws-in output visible true)
    i3-msg "[workspace=$workspaces[1]] move container to workspace ph"
    i3-msg "[workspace=$workspaces[2]] move container to output $outputs[1]"
    i3-msg "[workspace=ph] move container to output $outputs[2]"
end

This saves one swap by moving everything on left monitor to ph, everything on right monitor to left monitor then everything on ph to right monitor.

Basically i3 msg can match not only attributes inherent to the window like its class but for example based on its workspace. Since you can tell i3 do something to everything in workspace foo its trivial to say move everything in foo to placeholder1 everthing in bar to placeholder2 then move everything in placeholder1 to left output everthing in placeholder2 to right output.

edit flag offensive delete link more

Comments

Just as a note, the workspace criterion needs a recent i3 version. Your average 4.8 that comes with Debian won't be able to use it.

Airblader gravatar imageAirblader ( 2015-11-24 07:12:29 +0000 )edit

Question Tools

Stats

Asked: 2014-07-15 15:31:13 +0000

Seen: 941 times

Last updated: Nov 23