maim icon indicating copy to clipboard operation
maim copied to clipboard

(Question) How do I screenshot only the "current screen"?

Open guihkx opened this issue 8 years ago • 15 comments

I know GithHub isn't a support forum, and I apologize for that. I've tried searching in the docs, but I couldn't find anything that worked for me (or, I'm too stupid -- which can be the case). :thinking:

I have two monitors, the primary is 1920x1080, and the secondary is 1360x768. They're not mirrored. What I want maim to do is: Take a screenshot of the screen my mouse cursor is pointed at. I know I can use coordinates, but how do I know what screen the mouse cursor is pointed at? Is there an easy way to do this and bind it to a keyboard shortcut in XFCE?

Thank you very much!

guihkx avatar May 22 '17 00:05 guihkx

You should be able to do something like maim -x :0.<screen number> screenshot.png. For example, maim -x :0.0 screenshot.png would capture the first configured screen. Additionally, you can probably do maim -x $DISPLAY.0 screenshot.png if you have multiple X display servers running like I do from time to time.

lilyinstarlight avatar May 22 '17 15:05 lilyinstarlight

It didn't work for me, I'm using RandR so maybe that's why? But even if it did work, that's not quite what I wanted... I was looking for a command to detect which screen my mouse cursor is pointed at, and then take a screenshot of only that particular screen. After a bit of googling I found a command that almost do that:

$ xdotool getmouselocation --shell
X=921
Y=481
SCREEN=0
WINDOW=52854360

With two ifs I could make it work easily for me, but I want to use it in a shortcut, so I'd have to create a separated script and I'm hoping to avoid that. Does slop or maim have a feature like xdotool getmouselocation? My current setup looks like this:

screenshot_2017-05-22_18-01-55

$ xrandr -q
Screen 0: minimum 8 x 8, current 3280 x 1080, maximum 16384 x 16384
DVI-I-0 disconnected (normal left inverted right x axis y axis)
DVI-I-1 disconnected (normal left inverted right x axis y axis)
HDMI-0 connected 1360x768+1920+312 (normal left inverted right x axis y axis) 1600mm x 900mm
   1360x768      60.02*+
   1920x1080     60.00    59.94    50.00    23.97    60.00    50.04  
   1280x720      60.00    59.94    50.00  
   1024x768      60.00  
   800x600       60.32  
   720x576       50.00  
   720x480       59.94  
   640x480       59.94    59.93  
DP-0 disconnected (normal left inverted right x axis y axis)
DVI-D-0 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 531mm x 298mm
   1920x1080     60.00 + 119.98*   99.93  
   1440x900     119.85  
   1280x1024    119.96    75.02    60.02  
   1024x768     119.99    75.03    60.00  
   800x600      119.97    75.00    60.32  
   640x480      120.01    75.00    59.94  
DP-1 disconnected (normal left inverted right x axis y axis)

So a very useful command would be something like slop --screen=2 and it'd output the exact coordinates only for my secondary screen. In my case that'd be: 1360x768+1920+312

Thank you!

guihkx avatar May 22 '17 20:05 guihkx

I made this script which replaces any root window selections with the monitor that the mouse was on: https://gist.github.com/naelstrof/544d76b0649512ee2458da4e6569278f You can probably alter it to do exactly what you want, it's written in pure bash and xutils. I tried commenting it pretty well, feel free to ask questions on how to alter it to do what you want! Lines 29 through 47 are of particular interest to you.

Making it made me realize that Gnome introduced a new feature that bugs out slop, which has been fixed. So thanks! You may need to update slop to the newest tag if you want to select the root window with slop like I do in this script.

naelstrof avatar May 22 '17 21:05 naelstrof

Also, slop is unaware of monitor geometry. So slop wouldn't be useful for selecting monitors in particular. I may add a flag for this functionality though, seeing as making/using the script above is a little overkill for such simple functionality...

naelstrof avatar May 22 '17 21:05 naelstrof

Thanks, but I'm getting this error using your script:

X Error of failed request:  BadMatch (invalid parameter attributes)
  Major opcode of failed request:  73 (X_GetImage)
  Serial number of failed request:  21
  Current serial number in output stream:  21

I made this ugly, bad and non-portable script for myself:


#!/usr/bin/env sh

# Change the coordinates for your primary and secondary monitor here
MONITOR_1='1920x1080+0+0'
MONITOR_2='1360x768+1920+312'

eval $(xdotool getmouselocation --shell)

max_width_monitor1=$(echo $MONITOR_1 | grep -Po '^\d+')

# Cursor is at the primary monitor
if [ $X -lt $max_width_monitor1 ]; then
    coords=$MONITOR_1
# Cursor is at the secondary monitor
else
    coords=$MONITOR_2
fi

maim -g${coords} -u "$HOME/Screenshot_$(date +%s).png" $@

I really hope you add this feature to slop, and also an option to not ask me to choose a window, just try to detect it on its own...

Thanks for the help guys!

guihkx avatar May 22 '17 22:05 guihkx

Ah yes, the error is the bug I fixed! You'll have to update slop for it to work properly. (Or you can remove the -i flag in the maim command.) Most distributions won't notice that slop was updated for a while, so you might have to install it yourself.

You can use my script to improve your own. You'd essentially want to copy lines 29 through 47 to a new file. With any of the variables I used defined at the top.

naelstrof avatar May 22 '17 22:05 naelstrof

I'm using v4.4.62, isn't it the last one? Anyway, I recorded a quick video showing a segfault when I move the window slightly to the right in my primary monitor, and also the error I mentioned earlier. I've clicked on the xfce4-panel (I probably shouldn't do that, but anyway).. I'm not too proficient in shell script so even if I wanted I couldn't fix it myself. lol

Here's the video: https://youtu.be/7UUYtn-pIXg

guihkx avatar May 22 '17 22:05 guihkx

I'm talking about slop, and the newest slop is v5.3.38.

But don't worry about that. This script will work instead for you: https://gist.github.com/naelstrof/f9b74b5221cdc324c0911c89a47b8d97

naelstrof avatar May 23 '17 00:05 naelstrof

Thank you very much for the script, but the $XMOUSE and $YMOUSE variables are undefined for me, do I have to install something?

guihkx avatar May 23 '17 00:05 guihkx

My bad I just copied pieces of the last script to make this one. Simply forgot to copy those variables over.

It's updated now, check it out.

naelstrof avatar May 23 '17 00:05 naelstrof

Worked like a charm!

You can close this now if you will.

Thank you, Dalton.

guihkx avatar May 23 '17 00:05 guihkx

Glad it works for you! I'll leave this open until I get around adding an option for slop to only select monitors.

naelstrof avatar May 23 '17 00:05 naelstrof

That latest gist is failing for me with 2 monitors. It always captures a screenshot of both displays together, but the output of the script is different depending on which monitor I run it from.

I'm running Debian unstable (Bash 5.0.0).

xrandr output:

Screen 0: minimum 8 x 8, current 4488 x 1440, maximum 16384 x 16384
DVI-I-0 disconnected (normal left inverted right x axis y axis)
DVI-I-1 connected 1920x1080+2568+0 (normal left inverted right x axis y axis) 480mm x 270mm
   1920x1080     60.00*+
   1680x1050     59.95  
   1600x900      60.00  
   1440x900      59.89  
   1280x1024     60.02  
   1280x720      60.00  
   1024x768      60.00  
   800x600       60.32  
   640x480       59.94  
DP-0 disconnected (normal left inverted right x axis y axis)
HDMI-0 connected primary 2560x1440+0+0 (normal left inverted right x axis y axis) 553mm x 311mm
   2560x1440     59.95*+
   2048x1152     60.00  
   1920x1200     59.88  
   1920x1080     60.00    59.94    50.00    29.97    25.00    23.98    60.05    60.00    50.04  
   1680x1050     59.95  
   1600x1200     60.00  
   1280x1024     75.02    60.02  
   1280x720      60.00    59.94    50.00  
   1200x960      60.00  
   1152x864      75.00  
   1024x768      75.03    60.00  
   800x600       75.00    60.32  
   720x576       50.00  
   720x480       59.94  
   640x480       75.00    59.94    59.93  
DP-1 disconnected (normal left inverted right x axis y axis)

I took the gist as is, but I also added an echo in there to get some data. Here's the output of running the script on both monitors:

  • HDMI-0's output of the gist: 2560x1440+0+0
  • DVI-1's output of the gist: 1920x1080+2568+0

HDMI-0 is physically positioned on the left, and DVI-1 is to its right.

Any thoughts?

nickjj avatar Jan 19 '19 23:01 nickjj

For when you get to implementing it, here's my idea of how it might work. With the idea of being helpful:

select monitor: Option for Slop. Using libxrandr, you look into the existing monitors and return the size and position of the monitor the user has provided (maybe with name) or digit (I believe they go 0, 1, etc) and if the x display option is also select, look for the mentioned monitor only in the selected display Option for Maim. It will just use Slop to get the coordinates and make a screenshot of it. Drawing a rectangle around the whole monitor, not just a window.

select screen: Option for Slop. Maybe should be changed to just give coordinates of the whole X screen whose name the user has input? Option for Maim. I think it's perfect as it is. Making a screenshot of the whole X screen whose name the user has input.

ifohancroft avatar Feb 22 '20 04:02 ifohancroft

I'm talking about slop, and the newest slop is v5.3.38.

But don't worry about that. This script will work instead for you: https://gist.github.com/naelstrof/f9b74b5221cdc324c0911c89a47b8d97

Executing the script through CLI works, captures one screen only, but when executing using sxhkd keybind it always captures both two screens. Any ideas why it is happening?

Edit: Nevermind, it is working after restarting the machine.

flame-0 avatar Jul 24 '22 05:07 flame-0