KDE Plasma Activities on stereoides
Published: 2024-04-05 19:38:18

I used to regard KDE Plasma Activities as merely pointless and dull—like fancy workspaces with added overhead. However, I couldn't have been more mistaken!

Regrettably, this functionality isn't readily visible to the average user, which is a drawback for Activities.

Activities support running scripts for four actions:

- Activated: Executes when the activity is selected or focused.
- Deactivated: Triggers when switching to another activity.
- Started: Runs upon activity initiation.
- Stopped: Executes when the activity is halted.

However, there's no graphical user interface (GUI) for this functionality. While there's a [Brainstorming discussion](https://discuss.kde.org/t/script-management-for-activities/3359) in the KDE forum from June 2023, there doesn't seem to be any subsequent development on this front.

So, let's set up scripts manually.

## Setting Up Scripts
First, you need to identify the IDs of the activities you're running. You can do this with the following command:

```bash
kactivities-cli --list-activities
```

This command provides you with a table containing the Activity ID, Activity Name, and Activity Icon. You just need the `Activity ID`. Next, navigate to the folder `.local/share/kactivitymanagerd/activities/` (create it if any part of the path doesn't exist). Inside this folder, create a subfolder with the same name as the `Activity ID` you'd like to script. Then, create subfolders for each action you want to script: `activated`, `deactivated`, `started`, or `stopped`. You can create folders for all of them if needed. Then, any script placed in one of these action folders will be executed accordingly.

## Enhanced Functionality
With these scripts in place, you have fully automated activity switching in Plasma. What's next? For example, you can customize the default browser for each activity.

For this, let's utilize the `utils.sh` script, which you can place wherever you prefer:

```bash
#!/bin/bash

function writeconf() {
  kwriteconfig5 --file ~/.config/kdeglobals --group General --key BrowserApplication "$1"
  xdg-settings set default-web-browser "$1"
  xdg-mime default "$1" x-scheme-handler/https
  xdg-mime default "$1" x-scheme-handler/http
  xdg-mime default "$1" text/html
}

function readconf() {
  kreadconfig5 --file ~/.config/kdeglobals --group General --key BrowserApplication
}
```

Then, create separate scripts for activating Chrome and Firefox:

`activate-chrome.sh` - placed in `activated`:

```bash
#!/bin/bash

source ~/.bin/activities/utils.sh

service=org.kde.ActivityManager
interface=$service.Activities
path=/ActivityManager/Activities
signal=CurrentActivityChanged

curact=$(qdbus $service $path $interface.CurrentActivity)
name="$(qdbus $service $path $interface.ActivityName $curact)"
echo "Switched to activity $name"
echo "Previous browser: $(readconf)"
echo "Setting browser to Chrome"
writeconf google-chrome.desktop
echo "Current Browser: $(readconf)"
```

`activate-firefox.sh` - placed in `deactivated`:

```bash
#!/bin/bash  

source ~/.bin/activities/utils.sh  

service=org.kde.ActivityManager  
interface=$service.Activities  
path=/ActivityManager/Activities  
signal=CurrentActivityChanged  
  
curact=$(qdbus $service $path $interface.CurrentActivity)  
name="$(qdbus $service $path $interface.ActivityName $curact)"  
echo "Switched to activity $name"  
echo "Previous browser: $(readconf)"  
echo "Setting browser to Firefox"  
writeconf firefox.desktop  
echo "Current Browser: $(readconf)"
```

Imagine these scripts being applied to your `Work` activity. When you switch to `Personal`, your default browser switches back to `Firefox`. This setup gives you tailored browser switching for different activities in Plasma.

If you prefer browsers other than Firefox or Chrome, you can easily modify the scripts accordingly.

## Additional Tips
I often find it useful to have certain applications pinned to the taskbar across all activities. By default, applications are only shown in the activity where they were launched. However, you can create a KWin window rule with the condition "All activities" to ensure these applications are always accessible.

## In Conclusion
This setup has been a significant game-changer for me. As someone who heavily uses Slack for both work and personal communication, having automated browser switching based on activities has streamlined my workflow immensely.

Next, I plan to further customize my Slack experience by separating work-related Slack workspaces from others, allowing for even greater efficiency and organization.

## Browser Switching Alternatives
If you're interested in alternative methods for activity switching and scripting, you might want to explore [Browsers](https://browsers.software/), an intuitive context menu that appears when you click a link in an app other than a web browser.