Showing posts with label scripting. Show all posts
Showing posts with label scripting. Show all posts

Thursday, January 24, 2013

The sick and twisted world of KDE/konsole 4.8.4 from dbus

YUCK

For some reason, you have to find yourself when running from a konsole window now.

So the code for opening tabs now has to:

  • Grab a list of all the fricking windows (getWindowId()/getWindows() below) to find my own id.
  • Grab a list of all the fricking sessions (getSessions() below)
  • Open a new tab
  • Grab the session list AGAIN and compare it to find the one we just opened.
  • Only then are you able to send a command to that tab.
Hope you can read some shell script, because this is almost too painful to explain.

#!/usr/bin/env bash

# konsole dbus functions

getWindows() {
    windows=`qdbus org.kde.konsole /konsole org.freedesktop.DBus.Introspectable.Introspect |grep MainWindow | cut -d \" -f 2`
    echo ${windows}
}

getSessions() {
    sessions=`qdbus org.kde.konsole /Sessions org.freedesktop.DBus.Introspectable.Introspect |grep "node name" | cut -d \" -f 2`
    echo ${sessions}
}

getWindowId() {
    windows=`getWindows`
    for i in $windows
    do
        id=`qdbus org.kde.konsole /konsole/$i org.kde.KMainWindow.winId`
        if [ "${WINDOWID}" -eq "$id" ]; then
            echo $i
        fi    
    done
}    
findNewSession() {
    old="$@"
    new=`getSessions`
    for t in $new; do 
        grep -q $t <(echo $old) 
        if [ "$?" -eq "1" ]; then 
            echo $t
        fi
    done
}
sshopentabs () {
    boxlist=$1
    box_name=$2
    comm='ssh -o StrictHostKeyChecking=no -o CheckHostIP=no -o UserKnownHostsFile=/dev/null'
    
    slot=1
    for i in $boxlist
    do
        sessions=`getSessions`
        echo ${KONSOLE_DBUS_SERVICE}
        newtab
        j=`findNewSession $sessions`
        if [ "${box_name}" -eq "" ]; then
            renametab ${j} $i
        else
            renametab ${j} "${box_name} slot ${slot}"
        fi
        sendtab ${j} "${comm} root@${i}"
        let slot=$slot+1
    done

    return ${j}
}
newtab () {
    id=`getWindowId`
    dbus-send --session --dest=${KONSOLE_DBUS_SERVICE} --type=method_call --print-reply /konsole/$id org.kde.KMainWindow.activateAction string:"new-tab"
}

renametab () {
    sessionno=$1
    tabname=$2
    session="/Sessions/${sessionno}"
    dbus-send --session --dest=${KONSOLE_DBUS_SERVICE} --type=method_call --print-reply ${session} org.kde.konsole.Session.setTabTitleFormat int32:0 string:"$tabname"
    dbus-send --session --dest=${KONSOLE_DBUS_SERVICE} --type=method_call --print-reply ${session} org.kde.konsole.Session.setTabTitleFormat int32:1 string:"$tabname"
    dbus-send --session --dest=${KONSOLE_DBUS_SERVICE} --type=method_call --print-reply ${session} org.kde.konsole.Session.setTitle int32:1 string:"$tabname"
} 

sendtab () {
    sessionno=$1
    cmd=$2
    session="/Sessions/${sessionno}"
    dbus-send --session --dest=${KONSOLE_DBUS_SERVICE} --type=method_call --print-reply ${session} org.kde.konsole.Session.sendText string:"$cmd^M"
}

closetab () {
    dbus-send --session --dest=${KONSOLE_DBUS_SERVICE} --type=method_call --print-reply ${KONSOLE_DBUS_SESSION} org.kde.konsole.Session.sendText string:"exit^M"
}

Monday, April 11, 2011

konsole tabs with dbus in kde4.6

In the continuing automation saga with konsole at my job, KDE has come far enough with the dbus interface in 4.6 to allow me to bring up tabs again and perform actions in them with a script.

For this I created the following shell functions:


# konsole dbus functions
newtab()
{
dbus-send --session --dest=${KONSOLE_DBUS_SERVICE} --type=method_call --print-reply /konsole/MainWindow_1 org.kde.KMainWindow.activateAction string:"new-tab"
}

renametab()
{
sessionno=$1
tabname=$2
session="/Sessions/${sessionno}"
dbus-send --session --dest=${KONSOLE_DBUS_SERVICE} --type=method_call --print-reply ${session} org.kde.konsole.Session.setTabTitleFormat int32:0 string:"$tabname"
dbus-send --session --dest=${KONSOLE_DBUS_SERVICE} --type=method_call --print-reply ${session} org.kde.konsole.Session.setTabTitleFormat int32:1 string:"$tabname"
dbus-send --session --dest=${KONSOLE_DBUS_SERVICE} --type=method_call --print-reply ${session} org.kde.konsole.Session.setTitle int32:1 string:"$tabname"
}

sendtab()
{
sessionno=$1
cmd=$2
session="/Sessions/${sessionno}"
" dbus-send --session --dest=${KONSOLE_DBUS_SERVICE} --type=method_call --print-reply ${session} org.kde.konsole.Session.sendText string:"$cmd
}

closetab()
{
dbus-send --session --dest=${KONSOLE_DBUS_SERVICE} --type=method_call --print-reply ${KONSOLE_DBUS_SESSION} org.kde.konsole.Session.close
}



In scripts that I want automation, I include these functions:


# konsole functions
. ~/bin/kons-fn.sh



I can then open a whole bunch of tabs at once and send them a command:



PORTS=( 2022 2021 )

SESSIONS="
A_FU
B_BAR
"

command="telnet ${CONSOLE_SERVER}"

j=1
index=0
for i in ${SESSIONS}
do
newtab
let index=$j-1
let j=$j+1
renametab $j $i
sendtab $j "${command} ${PORTS[${index}]}"
done

closetab
exit 0



However I haven't gotten this to work from starting 'konsole -script' from a shortcut. Launching a script like this creates an infinite number of tabs launching the script in sequence. Very weird stuff.

For now, just start a konsole session and launch the script within the first tab. As you can deduce from the functions, it has to be the first tab you launch konsole with.