This forum has been archived. All content is frozen. Please use KDE Discuss instead.

How to set an "Activity" from command line

Tags: None
(comma "," separated)
francomartelli
Registered Member
Posts
2
Karma
0
Hello everybody,

These are the system information:

Operating System: Debian GNU/Linux 11
KDE Plasma Version: 5.20.5
KDE Frameworks Version: 5.78.0
Qt Version: 5.15.2
Kernel Version: 5.10.92
OS Type: 64-bit
Processors: 8 × AMD FX(tm)-8350 Eight-Core Processor
Memory: 7.6 GiB of RAM
Graphics Processor: GeForce GT 630/PCIe/SSE2


As you can see I've Nvidia hardware, in order to have things working properly I need to run a script when I resume from suspend to RAM the PC. This is the script:

Code: Select all
# cat 00_resume.sh
#!/bin/sh

###########################################################################################
# Run from systemd subsystem  to place under /usr/lib/systemd/system-sleep/
###########################################################################################

case $1/$2 in
  pre/*)
    ;;
  post/*)
    /usr/bin/sleep 2
    /usr/bin/killall kwin_x11
    /usr/bin/sleep 4
    /usr/bin/su MyUser -c "XDG_RUNTIME_DIR=/run/user/1000 DISPLAY=:0 XDG_CURRENT_DESKTOP=KDE KWIN_TRIPLE_BUFFER=1 /usr/bin/kwin_x11 >/dev/null 2>&1 &"
    ;;
esac


What the script does is to restart kwin_x11. Sadly when it happens all applications reset the Activity values to "All Activity".
Therefore I've to manually change the window's Activity to set it to the proper value:

Code: Select all
$ kactivities-cli --list-activities
[RUNNING] 6ca2f098-6318-4c10-a5ee-8d7616c2cc6f Games (input-gaming)
[CURRENT] 1ed4809f-9cf3-4098-a769-f52cf033dffc Rete (network-wired-activated)
[STOPPED] e67ea8ff-abb3-4aaf-a82f-51314bcb9a51 Virtualizzazione (virtualbox)


Googling on the Internet I found something interesting about the xprop command:

Code: Select all
xprop -name Firefox -f _KDE_NET_WM_ACTIVITIES 8s -set _KDE_NET_WM_ACTIVITIES "1ed4809f-9cf3-4098-a769-f52cf033dffc"


xprop sets the window's propriety _KDE_NET_WM_ACTIVITIES to the value of "Rete" activity but sadly this doesn't work. The Firefox window is still set to "All Activities"

I think that the possible solution is to set the proper Activity of each window via a command in the resume script (00_resume.sh).
Does anybody know how to accomplish this? Thank in advance :)
kde-jon3
Registered Member
Posts
1
Karma
0
This tip led me to a solution (works on at least 22.04):
Code: Select all
xprop -f _KDE_NET_WM_ACTIVITIES 8s -id $(xdotool getwindowfocus) -set _KDE_NET_WM_ACTIVITIES $ACTIVITYIDS


Here's a very novice script to add a toggle feature and manage duplicate IDs, etc.
Code: Select all
#!/bin/bash
# ########################################################################################
#
# Modifies the X-Window _KDE_NET_WM_ACTIVITIES property to change which kde-activities the
# focused current window is assigned to
#
# ARGUMENT:
# * Null argument passed will assign window to all Activities
# * A single activity ID will toggle the window's activity assignment for the associated ID.
# * Multiple activity IDs can be passed as a csv-string to assign the window to the activities
#   in additiona to the activities for which the window is already assigned.
#
# EXAMPLE:
#   windowToActivity                                                                                  (Assign window to All Activities)
#
#   windowToActivity 066f8095-e5ee-e4e4-abc1-716a68840fd9       (Toggle the specific Activity Assignment on/off)
#
#   windowToActivity 066f8095-e5ee-e4e4-abc1-571a688400d9,62c029d9-2f51-b412-78bd-52d587bf72fc  (Add Window to Activities)
#
# ########################################################################################

#
# removeActivityIDFromWindow( $xpropWindowID $activityIDToRemoveString )
function removeActivityIDFromWindow () {
        # Get list of activity-IDs already assigned to the specific window
        local aidsInWindow=$(xprop -id "$1" _KDE_NET_WM_ACTIVITIES | grep -oP "\"[\w\-\,]+\"" | grep -oP "[\w\-\,]+")
        #echo "Existing activity IDs in window[$1]: $aidsInWindow"

        # Explode on comma delimeter into new array
        local arrArgs=(${aidsInWindow//,/ })

        # Filter out duplicates
        local uniqueAidsArray=($(echo "${arrArgs[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))

        # Explode on comma delimeter into array of activity IDs
        local delete=(${2//,/ })
        #echo "activity-IDs to remove from window[$1]: $2"
        # Remove IDs from the existing list
        for del in ${delete[@]} ; do
                uniqueAidsArray=("${uniqueAidsArray[@]/$del}")
        done

        # Convert array back into csv list
        local newActivityList
        newActivityList=$( IFS=,; echo "${uniqueAidsArray[*]}" )

        # Trim the first and last commas off of the list
        newActivityList=$( echo "$newActivityList" | sed 's/[,]*$//' )
        newActivityList=$( echo "$newActivityList" | sed 's/^[,]*//' )

        # Change X-Property _KDE_NET_WM_ACTIVITIES for the specific window
        xprop -f _KDE_NET_WM_ACTIVITIES 8s -id "$1" -set _KDE_NET_WM_ACTIVITIES "$newActivityList"
        #echo "New activity list for window ($1): $newActivityList"
}

#
# addActivityIDToWindow( $xpropWindowID $activityIDsToAddString )
function addActivityIDToWindow () {
        # Get list of activity-IDs already assigned to the specific window
        local aidsInWindow=$(xprop -id "$1" _KDE_NET_WM_ACTIVITIES | grep -oP "\"[\w\-\,]+\"" | grep -oP "[\w\-\,]+")
        #echo "Existing activity IDs in window[$1]: $aidsInWindow"
        #echo "New activity-IDs to add in window[$1]: $2"

        # Append to list
        local arg2="$aidsInWindow,$2"
        #echo "New-Non-Curated activity-ID list in window[$1]: $arg2"

        # Explode on comma delimeter into new array
        local arrArgs=(${arg2//,/ })

        # Filter out duplicates
        local uniqueAidsArray=($(echo "${arrArgs[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))

        # filter out specific values including the all windows
        local delete=( "00000000-0000-0000-0000-000000000000" )
        for del in ${delete[@]} ; do
                uniqueAidsArray=("${uniqueAidsArray[@]/$del}")
        done

        # Convert array back into csv list
        local newActivityList
        newActivityList=$( IFS=,; echo "${uniqueAidsArray[*]}" )

        # Trim the first and last commas off of the list
        newActivityList=$( echo "$newActivityList" | sed 's/,$//' )
        newActivityList=$( echo "$newActivityList" | sed 's/^,//' )

        # Change X-Property _KDE_NET_WM_ACTIVITIES for the specific window
        xprop -f _KDE_NET_WM_ACTIVITIES 8s -id "$1" -set _KDE_NET_WM_ACTIVITIES "$newActivityList"
        #echo "New activity list for window ($1): $newActivityList"
}

# Get Input and validate format
arg1=$( echo $1 | grep -oP '(?P<aid>[\w]{8}(\-[\w]{4}){3}\-[\w]{12})((,\g<aid>)+)?' )
#echo $arg1

# Get xproperty window ID
windowID=$(xdotool getwindowfocus)
#echo "$windowID"

# Determine if any IDs are to be added
if [ "$arg1" ]; then

        # Determine if multiple activity IDs are to be added
        multiple=$( echo $arg1 | grep -oP ',' )

        # Determine if multiple IDs are to be added
        if [ "$multiple" ]; then
                # Add the deduped list of IDs to the windows X-properties
                addActivityIDToWindow $windowID $arg1
                #removeActivityIDFromWindow $windowID $arg1
        else
                # Get list of activity-IDs already assigned to the specific window
                aidsInWindow=$(xprop -id "$windowID" _KDE_NET_WM_ACTIVITIES | grep -oP "\"[\w\-\,]+\"" | grep -oP "[\w\-\,]+")
                #echo "Existing activity IDs in window[$windowID]: $aidsInWindow"

                # Toggle the activity ID in the list (remove it from the list if it exists, add it to the list if it does not exist)
                if [ $( echo "$aidsInWindow" | grep -oP "$arg1" ) ]; then
                        removeActivityIDFromWindow $windowID $arg1
                        #echo "Activity ID ($arg1) already assigned to window[$windowID]; removing it from the list."
                else
                        # Add the ID the window X-Property
                        addActivityIDToWindow $windowID $arg1
                        #echo "Adding ID ($arg1) to window[$windowID]"
                fi
        fi
elif [ "$1" == "" ] ; then
        # Set window to All Activities
        xprop -f _KDE_NET_WM_ACTIVITIES 8s -id $windowID -set _KDE_NET_WM_ACTIVITIES "00000000-0000-0000-0000-000000000000"
        #echo "No argument submitted, setting window[$windowID] x-property '_KDE_NET_WM_ACTIVITIES' to 'All Activities' (00000000-0000-0000-0000-000000000000) "
else
        echo "Argument does not appear to contain a valid Activity ID."
        echo "You can check IDs with: kactivities-cli --list-activities"
fi

dzon
Registered Member
Posts
493
Karma
3
Nice. Good to see some proper scripting from time to time on here.


This realm's name is Maya. And she speaks Hertz. But Ahamkara makes a fuzz about it.
francomartelli
Registered Member
Posts
2
Karma
0
Thanks for the tip kde-jon3 !!!
After a few of investigation on the xdotool manual page I found that the equivalent command of:
Code: Select all
xdotool getwindowfocus

searching the window id by name is:
Code: Select all
xdotool search --onlyvisible --name Firefox

edit:
if you have trouble searching the window name try the --class option instead of --name

therefore now it's easy set the activity value of the Firefox window to "Rete". For example the following command move the Firefox window from "Rete" activity to the "Games" activity:
Code: Select all
xprop -f _KDE_NET_WM_ACTIVITIES 8s -id $(xdotool search --onlyvisible --name Firefox) -set _KDE_NET_WM_ACTIVITIES $(kactivities-cli --list-activities|grep Games|cut -d" " -f2)

and this move back the Firefox window to the "Rete" activity:
Code: Select all
xprop -f _KDE_NET_WM_ACTIVITIES 8s -id $(xdotool search --onlyvisible --name Firefox) -set _KDE_NET_WM_ACTIVITIES $(kactivities-cli --list-activities|grep Rete|cut -d" " -f2)

Thanks again, kind regards


Bookmarks



Who is online

Registered users: bartoloni, Bing [Bot], Google [Bot], Yahoo [Bot]