Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Gnome Shell Shortcuts programmatically

Gnome Shell has great shortcuts, however, I don't find a way to call them programmingly Assume that I want to use a GJS script to start Google Chrome, move it to workspace 1, and maximize it, then start Emacs, move it to workspace 2, and maximize it. This could be done using wm.keybindings: move-to-workspace-1, move-to-workspace-2 and maximize. However, how to call them programmingly?

I notice that in GJS, Meta.prefs_get_keybinding_action('move-to-workspace-1') will return the guint of action move-to-workspace-1, but I did not find any function to call the action. In https://github.com/GNOME/mutter/blob/master/src/core/keybindings.c, I found a function meta_display_accelerator_activate, but I could not find a GJS binding for this function.

So, is there any way to call gnome shell shortcuts programmatically?

like image 594
Zeno Zeng Avatar asked Feb 13 '14 08:02

Zeno Zeng


1 Answers

The best bet to move an application is to grab its Meta.Window object, which is created after it's started.

This would be done through getting the active workspace, starting the application, then getting the application from the active workspace and moving it.

Sample code for a quick implementation:

const workspace = global.screen.get_active_workspace();
const Gio = imports.gi.Gio;

//CLIname: Name used to open app from a terminal
//wsIndex: Workspace you want it on
function openApp(CLIname, wsIndex) {
    let context = new Gio.AppLaunchContext;
    //use 2 to indicate URI support
    //0 is no flags, 1 for terminal window,
    //No 3, 4 for notification support
    //null because setting a name has no use
    Gio.AppInfo.create_from_commandline(CLIname, null, 2).launch([], context);
    //Unfortunately, there is no way I know to grab a specific window if you don't know the index.
    for(let w of workspace.list_windows()) {
        //check if the window title or window manager class match the CLIname. Haven't found any that don't match either yet.
        if(w.title.toLowerCase().includes(CLIname.toLowerCase() || w.get_wm_class().toLowerCase.includes(CLIname.toLowerCase()) {
            //Found a match? Move it!
            w.change_workspace(global.screen.get_workspace_by_index(wsIndex));
        }
    {
}
/*init(), enable() and disable() aren't relevant here*/

To answer the actual question way at the end, it might be possible by forcing the GNOME on-screen keyboard to emit those keys, but that would require matching the right keys and I/O emulation for every keybinding you wish to execute, which can change whenever a user wants it to, from an extension.

like image 100
RivenSkaye Avatar answered Sep 22 '22 04:09

RivenSkaye