LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-24-2019, 11:15 PM   #1
goosesensor
LQ Newbie
 
Registered: Jan 2019
Location: Santa Cruz, CA
Distribution: Arch
Posts: 11

Rep: Reputation: Disabled
Lightbulb Application launcher script - instance & workspace management


Hello,

I use XFCE and for a long time I have been employing a "launcher" script in a panel I use as a "Dock." Basically when you click a given panel item (an xfce4-panel "Launcher" item) it runs a script launch.sh that launches the specified program (using Devilspie to specify a default workspace for the program) and then switches to that workspace. So if I want to launch Firefox, the script launches it, devilspie sees this and puts it on workspace #4, and the script switches to workspace 4 showing my fresh Firefox instance. This works pretty well, but I'd like to modify its behavior a bit.

I'd like it so that if an instance of the program is already running, instead of launching a new instance, the script will just switch the current workspace to the running instance and activate the window.

Something like this:

Code:
Click dock icon -> runs launch.sh with <appname/path> <preferred workspace> <filename/args>
    if (app instance already running)
        find latest instance (window?)
        switch to that workspace and activate that window
    else
        launch the program in its default workspace and switch to that workspace
        $ (how it currently functions)
I've experimented which various programs like xdotool and wmctl to piece together the needed pieces but have come up empty.

Something like:
- Get a list of instances of windows for program X
- Find the window ID of the latest window instance from that program
- Switch to the workspace
- Activate that window

Any ideas?
 
Old 01-25-2019, 10:08 PM   #2
goosesensor
LQ Newbie
 
Registered: Jan 2019
Location: Santa Cruz, CA
Distribution: Arch
Posts: 11

Original Poster
Rep: Reputation: Disabled
Here is what I have come up with. Hopefully it will help somebody else out.
Note I am no longer using devilspie for this.

Comments or suggestions welcome.

Code:
#!/bin/bash


# Usage:
# launch.sh [-e <exec path>] [-c <instance search class>] -w [<workspace>] [-m <MODE>] [<program args>]

# WORKSPACE is 1-indexed
# MODE is "auto" or "raw". defaults to "raw"
# 	'auto' will try to switch to an existing open instance of EXEC by searching for CLASS before \
#		launching a new instance
#	'raw' just launches it as if you clicked on it.
# CLASS defaults to EXEC is none is specified.


# known issues:
#	spotify does not like to activate
#	gnome-calculator also does not like to activate


# this was very helpful: https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash


ARGS=() # arguments without switches -- pass to new instance of the program

function parseArgs() {
	POSITIONAL=()
	while [[ $# -gt 0 ]]
	do
		key="$1"

		case $key in
   	 	-e|--executable)
    			EXEC="$2"; shift; shift;;
   	 	-c|--class)
    			CLASS="$2"; shift; shift;;
    		-w|--workspace)
    			WORKSPACE="$2"; shift; shift;;
           	-m|--mode)
                        MODE="$2"; shift; shift;;
#		-a|--args)
#			ARGS="$2"; shift; shift;;
    		*) # unknown option
    			POSITIONAL+=("$1") # save it in an array for later
			#ARGS+=("$1")
			ARGS=("${ARGS}${1} ")
			printf "ARG: %s\n" "$1"
			shift;;
                --default)
                        shift;;
		esac
	done

	set -- "${POSITIONAL[@]}" # restore positional parameters
}

parseArgs "$@"

if [ "$CLASS" == "" ]; then
	CLASS="$EXEC"
fi

if [ "$MODE" == "" ]; then
      	MODE="raw"
fi


#printf "EXEC='%s', 'CLASS=%s', 'WORKSPACE=%d', 'MODE=%s', 'ARGS=%s'\n" "$EXEC" "$CLASS" "$WORKSPACE" "$MODE" "$ARGS"


CLASS_COUNT=$(xdotool search --class "$CLASS" | wc -l)

# if there are args, just open a new instance.  or hope that the app handles its own instancing.
# (Example: launch firefox with some URL argument)
if [ $MODE == "raw" ] || [ $CLASS_COUNT == 0 ] || [ "$ARGS" != "" ]; then
        #echo "LAUNCHING NEW"

      	if [ "$ARGS" == "" ]; then
       		$EXEC &
      	else
              	$EXEC "$ARGS" &
      	fi

	WORKSPACE_ZERO=$(($WORKSPACE - 1))
        if [ $WORKSPACE_ZERO -ne "-1" ]; then
                wmctrl -s $WORKSPACE_ZERO
        fi
else
	#echo "SWITCHING TO INSTANCE"

        wmctrl -a $CLASS
fi

exit 0
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Long lag between clicking the Application Launcher icon and the launcher appearing in KDE alex14641 Slackware 2 03-28-2016 05:36 PM
[SOLVED] xfce4 workspace switcher only 1 workspace available. percy_vere_uk Linux From Scratch 15 03-16-2016 01:15 PM
[SOLVED] Switch to instance instead of creating a new instance of a program javascriptninja Linux - Newbie 8 01-28-2012 03:39 PM
[SOLVED] WorkSpace hangs switch to 2nd WorkSpace also hung ken poy Linux - Newbie 2 08-14-2010 08:07 AM
workspace names in "switcher workspace boxes" problem petesasha Linux Mint 0 11-20-2009 01:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration