javajunkie314

runon: Run an X program on a different workspace

Nov 13th, 2012
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.71 KB | None | 0 0
  1. #!/bin/zsh
  2.  
  3. # Usage:
  4. #
  5. # runon <workspace> <command...>
  6. #
  7. # This will run the given command, then move the resulting window to the
  8. # workspace specified. There are two formats for workspace:
  9. #   +n or -n: Move the window n desktops to the right (+) or left (-)
  10. #   name:     Move the window to the desktop with the given name
  11. #
  12.  
  13. # Check for the required parameters
  14. if [ $# -lt 2 ]
  15. then
  16.     echo 'Workspace and command are required.' >&2
  17.     exit 1
  18. fi
  19.  
  20. # Get the workspace from the arguments, then remove it
  21. workspace=$1; shift 1
  22.  
  23. # If the workspace starts with a + or a -, assume it's an offset
  24. if [[ "$workspace" =~ '^[+-][[:digit:]]*$' ]]
  25. then
  26.     # Get the current desktop id
  27.     wmctrl -d | awk "/*/ {print \$1}" | read current
  28.  
  29.     # Add the offset to that id
  30.     desk=$((current + workspace))
  31.  
  32.     # If that desktop id does not exist, error out
  33.     if ! wmctrl -d | grep -q "^$desk"
  34.     then
  35.         echo "Offset does not resolve to a valid workspace" >&2
  36.         exit 1
  37.     fi
  38. else
  39.     # Resolve a workspace to desktop id
  40.     if ! wmctrl -d | awk "{if (\$9 == \"$workspace\") print \$1}" | read desk
  41.     then
  42.         # If that fails, error out
  43.         echo "Unknown workspace $workspace" >&2
  44.         exit 1
  45.     fi
  46. fi
  47.  
  48. # Start the command, capturing its PID
  49. $@ &; pid=$!
  50.  
  51. # Loop while that command is running
  52. while jobs %1
  53. do
  54.     # If it has any windows, move them to the requested workspace then
  55.     # terminate the loop
  56.     if wmctrl -l -p | awk "{if (\$3 == $pid) print \$1}" | read xid
  57.     then
  58.         wmctrl -i -r $xid -t $desk
  59.         break
  60.     fi
  61.  
  62.     # Wait a little while for a window
  63.     sleep 1
  64. done &>/dev/null &
  65.  
  66. # Wait for the command to exit
  67. wait %1 &>/dev/null
Add Comment
Please, Sign In to add comment