run-or-focus 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/bin/sh
  2. # Copyright © 2012 Alex Kost
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. #### HELP MESSAGE ##############################################
  14. hlp='Usage: run-or-focus APP_NAME [APP_CLASS]
  15. Set focus on application APP_CLASS (recognized by wmctrl) or run
  16. APP_NAME if it does not exist.
  17. If APP_CLASS is not specified, use APP_NAME (case is ignored)'
  18. if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
  19. echo "$hlp"
  20. exit 0
  21. fi
  22. ################################################################
  23. app_exec=$1
  24. if [ -z "$2" ]; then
  25. app_wm_class=$app_exec
  26. else
  27. app_wm_class=$2
  28. fi
  29. if [ -z "$(wmctrl -xl | grep -i $app_wm_class)" ]; then
  30. $app_exec &
  31. else
  32. wmctrl -x -a $app_wm_class
  33. fi