How to launch a KDE konsole with multiple tabs running various progs?

后端 未结 4 1638
别那么骄傲
别那么骄傲 2021-02-13 05:24

I know how to start a Konsole with one executable running in it, and leave the Konsole open after the program ends. I can do this using a .desktop file and change s

4条回答
  •  别那么骄傲
    2021-02-13 06:28

    This is a solution using qdbus, see D-Bus documentation. The Konsole docs doesn't say much about the interfaces used, so some experimenting is necessary. I've left comments in the code about the things I attempted but that didn't work.

    This works in KDE 5.

    #! /bin/bash
    # Multi command start in various konsole tabs
    
    # List of commands to run, with parameters, in quotes, space-separated; do not use quotes inside (see bash arrays)
    COMMANDS=("/my/prog1 param" "/my/prog2 param2" "/my/prog3 param1 param2 param3")
    
    # KDS=$KONSOLE_DBUS_SERVICE # This is a ref to current Konsole and only works in Konsole
    # KDS=$(org.kde.konsole)    # This is found in some examples but is incomplete
    
    qdbus >/tmp/q0              # Get the current list of konsoles
    /usr/bin/konsole            # Launch a new konsole
    # PID=$!                    # And get its PID - But for some reason this is off by a few
    sleep 1
    qdbus >/tmp/q1              # Get the new list of konsoles
    # KDS=org.kde.konsole-$PID      
    # KDS=org.kde.konsole       # Sometimes
    KDS=$(diff /tmp/q{0,1} | grep konsole)  # Let's hope there's only one
    #echo $KDS
    KDS=${KDS:3}
    echo $KDS
    
    echo $KDS >/tmp/KDS
    echo >>/tmp/KDS
    
    qdbus $KDS >>/tmp/KDS || exit
    echo >>/tmp/KDS
    
    # See note https://docs.kde.org/trunk5/en/applications/konsole/scripting.html about using /Konsole
    qdbus $KDS /Konsole >>/tmp/KDS
    echo >>/tmp/KDS
    
    FirstTime=1
    
    for i in "${COMMANDS[@]}"
    do 
        echo "Starting: $i"
        echo >>/tmp/KDS
        if [ $FirstTime -eq 1 ]
        then
            session=$(qdbus $KDS /Konsole currentSession)
            FirstTime=0
        else
            session=$(qdbus $KDS /Konsole newSession)
        fi
        echo $session >>/tmp/KDS
    
        # Test: Display possible actions
        qdbus $KDS /Sessions/${session} >>/tmp/KDS
    
        # Doesn't work well, maybe use setTabTitleFormat 0/1 instead
        # Title "0" appears to be the initial title, title "1" is the title used after commands are executed. 
        #qdbus $KDS /Sessions/${session} setTitle 0 $i
        #qdbus $KDS /Sessions/${session} setTitle 1 $i
    
        # The line break is necessary to commit the command. \n doesn't work
        qdbus $KDS /Sessions/${session} sendText "${i}
    "
    
        # Optional: will ping when there's no more output in the window
        qdbus $KDS /Sessions/${session} setMonitorSilence true
    done
    

    Update 2016: the structure of qdbus has changed again. Here's an update of the above script (I left out the original since depending on your KDE version you may need one or the other):

    #! /bin/bash
    # Multi command start in various konsole tabs
    
    # List of commands to run, with parameters, in quotes, space-separated; do not use quotes inside (see bash arrays)
    COMMANDS=("echo 1" "echo 2" "echo 3")
    
    # KDS=$KONSOLE_DBUS_SERVICE # This is the ref of the current konsole and only works in a konsole
    # KDS=$(org.kde.konsole)    # This is found in some examples but is incomplete
    
    qdbus >/tmp/q0              # Get the current list of konsoles
    /usr/bin/konsole            # Launch a new konsole
    sleep 1
    qdbus >/tmp/q1              # Get the new list of konsoles
    KDS=$(diff /tmp/q{0,1} | grep konsole)  # Let's hope there's only one
    KDS=${KDS:3}
    echo $KDS
    
    echo $KDS >/tmp/KDS
    echo >>/tmp/KDS
    
    qdbus $KDS >>/tmp/KDS || exit
    echo >>/tmp/KDS
    
    # See note https://docs.kde.org/trunk5/en/applications/konsole/scripting.html about using /Konsole
    qdbus $KDS /konsole >>/tmp/KDS
    echo >>/tmp/KDS
    
    FirstTime=1
    
    for i in "${COMMANDS[@]}"
    do 
        echo "Starting: $i"
        echo >>/tmp/KDS
        if [ $FirstTime -eq 1 ]
        then
            session=$(qdbus $KDS /Windows/1 currentSession)
            FirstTime=0
        else
            session=$(qdbus $KDS /Windows/1 newSession)
        fi
        echo $session >>/tmp/KDS
    
        # Test: Display possible actions
        qdbus $KDS /Sessions/${session} >>/tmp/KDS
    
        # The line break is necessary to commit the command. \n doesn't work
        qdbus $KDS /Sessions/${session} sendText "${i}
    "
    
        # Optional: will ping when there's no more output in the window
        qdbus $KDS /Sessions/${session} setMonitorSilence true
    done
    

提交回复
热议问题