How to go to next/previous workspace from command line?

What command can we run from terminal to make Gnome go to the next or previous workspace?

I know this command can toggle Activies view:

dbus-send --session --type=method_call --dest=org.gnome.Shell /org/gnome/Shell org.gnome.Shell.Eval string:'Main.overview.toggle();'

and the thing of interest there is the Main.overview.toggle();.

So maybe I just need to know what the JavaScript API for going to next and previous workspace is. Where are the API docs for this? Google isn’t doing a good job leading me there.

The StepByStepTutorial page says

Apuf…this is very difficult, because of the lack of documentation…So the best way to learn is to see the source code (Yes, the best way…the only way!).

It seems like this is the case.

EDIT: Ah, ok, I found this: https://gjs-docs.gnome.org. For some reason, it isn’t immediately obvious. Huh, and the page at https://developer.gnome.org/references points to https://devdocs.baznga.org/ for JavaScript docs, which is currently a non-existent location.

EDIT: I don’t see anything if I search for “overview” in https://gjs-docs.gnome.org. I was hoping to find info on, for example, Main.overview. Is that supposed to be documented there?

EDIT: I see! I need to explicitly “enable” APIs in order to be able to search for them there.

Which API contains workspace switching stuff? I have a feeling it is in the “Shell” API. Mind giving a hint?

EDIT: Ok, I see global.workspace_manager, but I’m not sure what to do with it yet. It says the type is Meta.WorkspaceManager, but I don’t see any docs for Meta.WorkspaceManager.

But this at least helps me know that I can try global.workspace_manager in lookingglas and see what it has, although I don’t see any method to go to next or previous workspace.

EDIT: Ooooooh, I need to enable the “Meta” APIs in DevDocs. I see Meta 3, Meta 4, and Meta 5. Now I need to figure out which one I need to look at.

I hope I’m not spoiling the fun of figuring it out yourself :smile:

There are no methods to go to the next or previous workspace. That’s mainly because the underlying API supports arbitrary grid layouts, so “next” or “previous” are ambitious: This fun comment has an overview.

What there is however is API to

  • get the currently active workspace
  • find a workspace’s neighbor in a given direction
  • “activate” a workspace (that is, switch to it)

Combining those gives you the desired result:

global.workspace_manager.get_active_workspace().get_neighbor(Meta.MotionDirection.UP).activate(global.get_current_time());
global.workspace_manager.get_active_workspace().get_neighbor(Meta.MotionDirection.DOWN).activate(global.get_current_time());

Note that get_neighbor() will return the workspace itself if there is no other workspace in the specified direction, so the above works on the top- and bottom-most workspace as well.

Awesome, thank you! Not sure how long it would’ve taken to figure that one out. Probably would have had to stumble on it in the source.

I was able to add the following to Gestures:

dbus-send --session --type=method_call --dest=org.gnome.Shell /org/gnome/Shell org.gnome.Shell.Eval string:'global.workspace_manager.get_active_workspace().get_neighbor(Meta.MotionDirection.DOWN).activate(global.get_current_time());'

and same with .DOWN

I’m curious, is it possible to make it also show the workspace list (the mini layout that highlights the workspace that I’m switching to), like when I use the keyboard?

I’m thinking I could just use xdotool instead, which wouuld be easier, but seems like the improper way to do it, because changing the key shortcuts would break the Gesture scripts.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.