6

It sounds simple; xdotool selects desktop, simulates F5 key press and returns focus to calling window / program.

After googling though it doesn't sound simple anymore.

I need to do this from bash / gnome-shell.

Any ideas?


This project is related to this Q&A where the icons drop off the left screen into a black whole because of different sized monitors:

Thanks to Podesta's help I created this code:

TestIcons () {

    [[ $TestSeconds == "" ]] || [[ $TestSeconds == 0 ]] && TestSeconds=5

    local i Position File
    for (( i=0; i<${#IconsArr[@]}; i=i+ICON_FLD_CNT )) ; do
        File="$ICONS_DIR"/"${IconsArr[((i+ICON_NAME_NDX))]}"
        Position="${IconsArr[((i+ICON_COL_NDX))]},${IconsArr[((i+ICON_ROW_NDX))]}"
        gvfs-set-attribute -t string "$File" \
                'metadata::nautilus-icon-position' "$Position"
    done

    wmctrl -k on        # Show desktop
    xdotool key F5      # Refresh desktop (show icon new positions)
    sleep $TestSeconds  # Pause for view
    wmctrl -k off       # Restore windows

    for (( i=0; i<${#OldIconsArr[@]}; i=i+ICON_FLD_CNT )) ; do
        File="$ICONS_DIR"/"${OldIconsArr[((i+ICON_NAME_NDX))]}"
        Position="${OldIconsArr[((i+ICON_COL_NDX))]},${OldIconsArr[((i+ICON_ROW_NDX))]}"
        gvfs-set-attribute -t string "$File" \
                'metadata::nautilus-icon-position' "$Position"

    done

    wmctrl -k on        # Show desktop
    xdotool key F5      # Refresh desktop (show icon new positions)
    wmctrl -k off       # Restore windows

} # TestIcons

This is what the screen looks like:

iconic 2 optimized.gif

  • What do you mean by selects deskstop? – Podesta May 30 '19 at 07:19
  • @Podesta Moves focus to desktop. Something like moving cursor outside of current window and left clicking. – WinEunuuchs2Unix May 30 '19 at 10:32
  • Something like minimizing all windows would do the trick? I played a bit with xdotool and it seems fairly straight forward. I'll try to post something. Is there anything in particular that you were or imagined you would have issues with? – Podesta May 30 '19 at 10:40
  • @Podesta For years I've never used Show desktop because on restoring Conky doesn't reappear. However that is more a conky problem I should be solving and unrelated to this project. – WinEunuuchs2Unix May 30 '19 at 10:42
  • If you only have one window open, then something like windowminimize should still do the trick without the show desktop shortcut. Otherwise you could also make it a loop to minimize everything. – Podesta May 30 '19 at 10:48
  • @Podesta To give you overview I was planning on rearranging icons, running xdotool to refresh desktop, running progress display with x seconds countdown so user can view, restoring icons, rerunning same xdotool script and then remounting main menu dialog window. Original instructions were for user to press F5 but that is too burdensome. Here's the project: https://askubuntu.com/questions/1146175/desktop-icons-just-dropped-down-left-screen-into-never-never-land – WinEunuuchs2Unix May 30 '19 at 10:52
  • That's interesting I'll take a look. But just a quick question. Are all the screens touching themselves at the border, or did you place some gap between them using xrandr? I've had similar problems, with the borders not wrapping properly if any of the borders weren't touching. Like, if you have a 1px gap anywhere, none of the my monitors would wrap properly.. – Podesta May 30 '19 at 11:01
  • @Podesta When I'm running it I have seven windows open. The window calling it will actually close first and I'm wondering if desktop has focus then anyway? Or does Gnome/Unity give focus to a different opened window? As far as :"touching" each other no window on my desktop ever really "touches" each other. They often overlap though... – WinEunuuchs2Unix May 30 '19 at 11:03
  • I'm not sure if I follow you completely. I'm posting a quick sketch for you to take a look. I believe once you close a window, it gives active status for the last one that was active. On the problem from the other thread, I mean the screens. Like, if you have two 1080p monitors, and arrange them like: xrandr 1 --pos 0x0 and xrandr 2 --pos 1921x0 the boundaries will be all broken. For the boundries to work they would have to start and end in the same pixel, so like xrandr 2 --pos 1920x0 (The y axis is irrelevant, you can change from 0 to anything). The xrandr obviously just a sketch. – Podesta May 30 '19 at 12:13

1 Answers1

6

Script below work of @WinEunuuchs2Unix and is the final solution. My original answer is at the end.

#!/bin/bash

TestIcons () {

    [[ $TestSeconds == "" ]] || [[ $TestSeconds == 0 ]] && TestSeconds=5

    local i Position File
    for (( i=0; i<${#IconsArr[@]}; i=i+ICON_FLD_CNT )) ; do
        File="$ICONS_DIR"/"${IconsArr[((i+ICON_NAME_NDX))]}"
        Position="${IconsArr[((i+ICON_COL_NDX))]},${IconsArr[((i+ICON_ROW_NDX))]}"
        gvfs-set-attribute -t string "$File" \
                'metadata::nautilus-icon-position' "$Position"
    done

    wmctrl -k on        # Show desktop
    xdotool key F5      # Refresh desktop (show icon new positions)
    sleep $TestSeconds  # Pause for view
    wmctrl -k off       # Restore windows

    for (( i=0; i<${#OldIconsArr[@]}; i=i+ICON_FLD_CNT )) ; do
        File="$ICONS_DIR"/"${OldIconsArr[((i+ICON_NAME_NDX))]}"
        Position="${OldIconsArr[((i+ICON_COL_NDX))]},${OldIconsArr[((i+ICON_ROW_NDX))]}"
        gvfs-set-attribute -t string "$File" \
                'metadata::nautilus-icon-position' "$Position"

    done

    wmctrl -k on        # Show desktop
    xdotool key F5      # Refresh desktop (show icon new positions)
    wmctrl -k off       # Restore windows

} 
TestIcons

A few other xdotools commands that can be useful in similar situation:

If your window manager has a show desktop command, it can be useful to minimize, and bring back all windows. Usually it is set to ctrl+alt+d:

xdotool key ctrl+alt+d

If you want to save the currently active window, to later bring it back, you can use:

myWindow="$(xdotool getactivewindow)"

Ti minimize a window you can use. Adding it to a loop allows you to minimize all windows, as an alternative to the show dekstop.

xdotool windowminimize $(xdotool getactivewindow)

To bring the active window you saved back to the focus:

xdotool windowactivate "$myWindow"

To close the active window use:

xdotool getactivewindow windowkill

And most important, all those commands that you can use with active window, you can actually search and apply to specific cases. For example, searching for this specific window, based on the title.

xdotool search --desktop 0 --name "command line - xdotool" windowactivate

There are many different ways of searching for windows, based on the title, the class, if they are visible, etc. It can also manipulate both windows positions and the mouse. The manpage for xdotol is very comprehensive. Combining them with a bash script would give plenty of freedom to achieve most things.

Podesta
  • 1,146
  • Don't worry. I'm not sure if it actually does what you want. I think part of it is that I don't use icons on the desktop, so I'm a bit lost. Posted it just to have something more concrete to discuss and iterate on. – Podesta May 30 '19 at 12:19
  • 1
    Seems to be a glitch in my system when trying to restore windows. I received an "CRTC" error the first time and nothing restored. Second time no error but still nothing restored. I found a better solution wmctrl -k on show desktop and wmctrl -k off restore windows (hide desktop?). If you would like to add that to your answer I'll gladly accept it for all your help. Later I'll change my question to show the final solution with .gif animation. Hopefully tonight :) – WinEunuuchs2Unix May 30 '19 at 23:16
  • Interesting, did you get the error with the all the forms of restoring a window, or it was more with the search command? I don't think I've seen this error you mentioned. I remember in the past xdotool would sometimes get confused with multiple monitors, but the --desktop 0 fixed it for me. I've never heard of wmctrl but seems pretty good too. I'll see if I edit it later, but I'm cool with you editing or simply adding a new answer. I should probably edit the script anyway, as it was mostly to show you a few options instead of a proper script. – Podesta May 30 '19 at 23:54
  • Oh, and I'm looking forward to the gif, I really want to understand what's going on.
  • – Podesta May 30 '19 at 23:54
  • 2
    I've updated the question with the .gif. It's actually hard to record screen when windows are minimized by show desktop command. – WinEunuuchs2Unix May 31 '19 at 01:05
  • Thanks @WinEunuuchs2Unix for both posting the .gif and also posting the script you ended up using. Anyone that ends up here will end up with a pretty good solution! – Podesta Jun 01 '19 at 01:03
  • 1
    I still want to accept your answer but you need to copy and paste my solution into it first. – WinEunuuchs2Unix Jun 01 '19 at 01:04
  • I'll add a 50 bonus if you answer the detailed question! 0:-) – Fabby Jun 01 '19 at 01:07
  • My bad. I added your script at the beginning. I'm not sure if I should leave the one I had posted. While I feel it might be useful, a comment I often see is that askubuntu is not a forum.. You have been around for way longer, so I'll accept what you decide. – Podesta Jun 01 '19 at 01:12
  • Thank you for changing answer so I can accept it. Ignore @Fabby he was drinking and I had asked his help in General Chat Room which I shouldn't have. – WinEunuuchs2Unix Jun 01 '19 at 01:36
  • No problem. I edited it a bit more, just now. It seems cleaner, but again I'll defer to your judgement. If you want to return it to a previous history or edit it further I'll accept the edits. – Podesta Jun 01 '19 at 01:38