LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How do I capture a screen with xwd? (https://www.linuxquestions.org/questions/linux-software-2/how-do-i-capture-a-screen-with-xwd-4175679441/)

lucmove 07-26-2020 11:05 PM

How do I capture a screen with xwd?
 
I need to capture a window with xwd:

$ xwd -out /home/me/dump

I run it, I get a crosshair pointer, click on the target window and the screenshot is dumped. I can view it with xwud.

Now, I need to do that non-interactively.

$ xprop WM_NAME

I get a crosshair pointer, click on the target window and the output tells me:

WM_NAME(STRING) = "Name of application"

So I run:

$ xwd -name "Name of application" -out /home/me/dump

Then I try to view it with xwud and the result is a completely black screen. There seems to be a thin gray frame around it.

What am I doing wrong?

shruggy 07-27-2020 02:32 AM

I'd try to do the same with xwininfo and window id:
Code:

$ xwininfo||grep -Po 'Window id: \K\w+'
0x1600003
$
xwd -id 0x1600003 -out /home/me/dump


lucmove 07-27-2020 03:18 PM

Quote:

Originally Posted by shruggy (Post 6149691)
I'd try to do the same with xwininfo and window id:
Code:

$ xwininfo||grep -Po 'Window id: \K\w+'
0x1600003
$
xwd -id 0x1600003 -out /home/me/dump


I just tried that. The result is a completely black screen with a thin gray edge/frame.

crts 07-27-2020 09:03 PM

What are you trying to achieve?

You can make screenshot of the entire screen with
Code:

xwd -root > screenshot
You can take a screenshot of the currently active window

Code:

id=$(xprop -root _NET_ACTIVE_WINDOW | cut -d ' ' -f5)
xwd -id $id > screenshot

If firefox is my current active window then this returns:
Code:

WM_NAME(STRING) = "LinuxQuestions.org - Reply to Topic - Mozilla Firefox"
This is the title that is displayed in the window title bar, it changes when I select a different tab. It is not necessarily the application name. If I open multiple xterms then they will all have
Code:

WM_NAME(STRING) = "xterm"
What are the selection criteria for the window that needs its screenshot taken?

PS: I tested above commands on FVWM2. If your desktop reliably displays the application name in the windows title bar then you could reliably identify a window by its application name. However, if there are multiple instances open then it would still be ambiguous.

lucmove 07-28-2020 12:14 AM

Quote:

Originally Posted by crts (Post 6150034)
You can make screenshot of the entire screen with
Code:

xwd -root > screenshot

Capturing the root/current screen doesn't work for me. I need the script to be able to identify the correct window and capture it.

The -root option would work fine for me if I could specify a virtual desktop. In fact, I keep the application running in virtual desktop 2 while I do something else in virtual desktop 1.

Quote:

Originally Posted by crts (Post 6150034)
You can take a screenshot of the currently active window
Code:

id=$(xprop -root _NET_ACTIVE_WINDOW | cut -d ' ' -f5)
xwd -id $id > screenshot


I did that and got a good screenshot for my terminal session. That is not what I want. So I ran:
sleep 2; id=$(xprop -root _NET_ACTIVE_WINDOW | cut -d ' ' -f5)
(alt+tabbed to the desired application)
xwd -id $id > screenshot

And I got a completely black screen again. :(

Quote:

Originally Posted by crts (Post 6150034)
If firefox is my current active window then this returns:
Code:

WM_NAME(STRING) = "LinuxQuestions.org - Reply to Topic - Mozilla Firefox"
This is the title that is displayed in the window title bar, it changes when I select a different tab. It is not necessarily the application name. If I open multiple xterms then they will all have
Code:

WM_NAME(STRING) = "xterm"
What are the selection criteria for the window that needs its screenshot taken?

I've tried WM_NAME and I get a completely black screen. That is in the description of the problem in my original post.

I've tried other applications too. I always end up with a black screen.

I searched a little more and found this command:

$ xwininfo -id $id | grep 'Map State'

The window I want is in virtual desktop 2 and it returns
Map State: IsUnMapped

However, I tried using the id of another application that is running in virtual desktop 1:
Map State: IsViewable

But when I screenshot that application with xwd, I get a black screen too!

I am stumped.

ondoho 07-28-2020 02:51 AM

Quote:

Originally Posted by lucmove (Post 6149925)
I just tried that. The result is a completely black screen with a thin gray edge/frame.

It is possible that the application in question paints its window in such a way that xwd cannot "see" it.
You could try a different screenshooter, but:
Quote:

Originally Posted by lucmove (Post 6150078)
The -root option would work fine for me if I could specify a virtual desktop. In fact, I keep the application running in virtual desktop 2 while I do something else in virtual desktop 1.

Oh, I get it now.
This is a continuation of this thread. It's a horrible approach to a problem I'm sure can be solved better. But OP refuses to identify that problem and keeps clinging to their cludge.

lucmove 07-28-2020 08:28 AM

Quote:

Originally Posted by ondoho (Post 6150115)
It's a horrible approach to a problem I'm sure can be solved better.

What would be the good approach to the problem? How can it be solved better? If you are sure can be solved better, that means you already have the good solution in mind. Are you willing to part with that information?

ondoho 07-28-2020 12:50 PM

Quote:

Originally Posted by lucmove (Post 6150222)
What would be the good approach to the problem? How can it be solved better? If you are sure can be solved better, that means you already have the good solution in mind. Are you willing to part with that information?

Quote:

Originally Posted by ondoho (Post 6150115)
But OP refuses to identify that problem

...

pan64 07-28-2020 01:13 PM

you can have a screenshot from the visible desktop (or area). The "other" virtual desktops are simply not available. X only draws what is displayed, but does not care/process anything outside (as long as they are hidden).

crts 07-28-2020 01:14 PM

Quote:

Originally Posted by ondoho (Post 6150115)
Oh, I get it now.
This is a continuation of this thread.

Thanks for the reference, there is some important info in the linked thread.

@OP: The fact that the other application is running in a different desktop is quite crucial. As has been pointed out, you can only take a screenshot of the current desktop.
For possible workarounds I found this thread. I have not tried the suggestions there since I never had any need to take "Off-screen" screenshots. My first idea, however, was also to start the application on another virtual display, possibly with vncserver.

Screen scraping should not be the first tool of choice to solve a problem. However, if you refuse to share what you are actually trying to achieve then you will probably keep getting only vague ideas of what a solution might look like.

lucmove 07-28-2020 02:11 PM

Quote:

Originally Posted by crts (Post 6150323)
@OP: The fact that the other application is running in a different desktop is quite crucial. As has been pointed out, you can only take a screenshot of the current desktop.
For possible workarounds I found this thread. I have not tried the suggestions there since I never had any need to take "Off-screen" screenshots. My first idea, however, was also to start the application on another virtual display, possibly with vncserver.

I found that thread too and it seems it might be useful, but I don't know how. I installed xvfb and read the manual, but couldn't figure out what to do with it. Apparently, I can create another kind of virtual screen with it that perhaps I could capture, but how? How do I transfer the application from my current screen/virtual desktop into the xvfb virtual screen?

Quote:

Originally Posted by crts (Post 6150323)
Screen scraping should not be the first tool of choice to solve a problem. However, if you refuse to share what you are actually trying to achieve then you will probably keep getting only vague ideas of what a solution might look like.

You can't be serious. The two threads (this one and my referenced one) are very clear:
- an application is running;
- it displays notifications; I need to act upon them as soon as possible but they don't occur very often, not even every day;
- it is running in another virtual workspace; it would be inconvenient to run it into the same workspace I use regularly;
- I need to capture screenshots of that application programatically, non-interactively, and detect changes; that is VERY CLEAR;
- even in my main/active workspace, I can't capture windows.

pan64 07-29-2020 02:01 AM

there should be a better way to communicate with that "another" application. Probably a log file or other thing which can be processed too.
Again, actually there is nothing to capture, so you need to find a different way.

crts 07-29-2020 04:15 AM

Quote:

Originally Posted by lucmove (Post 6150354)
You can't be serious. The two threads (this one and my referenced one) are very clear:
...

You did NOT say that the application is running in another Desktop in your initial post. You did NOT say which application you are using so I had to take a guess on the selection criteria. This is what I was referring to.
You later stated that it is running in a different Desktop. At this point your goal kind of became a "moving target".

By the way, here is some output that I get for Firefox:
Code:

$ xwininfo -root -tree |grep -i firefox
    0x1a0005a "Advanced question: finding pid of an x window": ("Popup" "Firefox")  319x44+619+90  +619+90
    0x1a00182 "Firefox": ("Popup" "Firefox")  374x335+350+376  +350+376
    0x1a0002d "Search": ("Popup" "Firefox")  73x44+732+211  +732+211
    0x1a0017e "Firefox": ("Popup" "Firefox")  993x98+280+130  +280+130
    0x1e00001 "/usr/lib64/firefox/firefox": ("/usr/lib64/firefox/firefox" "/usr/lib64/firefox/firefox")  10x10+10+10  +10+10
    0x1a000f3 "Find the previous occurrence of the phrase": ("Popup" "Firefox")  300x44+490+710  +490+710
    0x3e00001 "/usr/lib64/firefox/firefox": ("/usr/lib64/firefox/firefox" "/usr/lib64/firefox/firefox")  10x10+10+10  +10+10
    0x3c00001 "/usr/lib64/firefox/firefox": ("/usr/lib64/firefox/firefox" "/usr/lib64/firefox/firefox")  10x10+10+10  +10+10
    0x3600001 "/usr/lib64/firefox/firefox": ("/usr/lib64/firefox/firefox" "/usr/lib64/firefox/firefox")  10x10+10+10  +10+10
    0x3400001 "/usr/lib64/firefox/firefox": ("/usr/lib64/firefox/firefox" "/usr/lib64/firefox/firefox")  10x10+10+10  +10+10
    0x3200001 "/usr/lib64/firefox/firefox": ("/usr/lib64/firefox/firefox" "/usr/lib64/firefox/firefox")  10x10+10+10  +10+10
    0x3000001 "/usr/lib64/firefox/firefox": ("/usr/lib64/firefox/firefox" "/usr/lib64/firefox/firefox")  10x10+10+10  +10+10
    0x1a001af "Firefox": ("Popup" "Firefox")  190x296+443+73  +443+73
    0x2a00001 "/usr/lib64/firefox/firefox": ("/usr/lib64/firefox/firefox" "/usr/lib64/firefox/firefox")  10x10+10+10  +10+10
    0x1a00018 "Firefox": ("Popup" "Firefox")  360x59+356+185  +356+185
    0x1a00112 "Firefox": ("Popup" "Firefox")  429x514+312+150  +312+150
    0x2e00001 "/usr/lib64/firefox/firefox": ("/usr/lib64/firefox/firefox" "/usr/lib64/firefox/firefox")  10x10+10+10  +10+10
    0x1a000a1 "Firefox": ("Popup" "Firefox")  200x200+0+0  +0+0
    0x1a0009d "Firefox": ("Popup" "Firefox")  429x226+76+150  +76+150
    0x1a00029 "Firefox": ("Popup" "Firefox")  200x200+0+0  +0+0
    0x1a0001c "Firefox": ("Popup" "Firefox")  224x82+127+150  +127+150
    0x2000001 "/usr/lib64/firefox/firefox": ("/usr/lib64/firefox/firefox" "/usr/lib64/firefox/firefox")  10x10+10+10  +10+10
    0x1a0000d (has no name): ("Toplevel" "Firefox")  200x200+0+0  +0+0
    0x1a0000a "Firefox": ()  10x10+-100+-100  +-100+-100
    0x1a00007 "Firefox": ()  10x10+-100+-100  +-100+-100
    0x1a00001 "Firefox": ("firefox" "Firefox")  10x10+10+10  +10+10
          0x1a00003 "LinuxQuestions.org - Reply to Topic - Mozilla Firefox": ("Navigator" "Firefox")  993x761+0+0  +280+32

And worse, you cannot even reliably use the PID to identify a window:
Code:

$ xprop -id 0x1a00003 _NET_WM_PID
_NET_WM_PID(CARDINAL) = 6 # Wrong! This is not Firefox's PID

As you can see, (reliably) identifying the window of an applicaton is not a trivial task.
If only you were willing to provide the application name so we could look for something that might be unique ...

lucmove 07-29-2020 12:18 PM

The application is proprietary and meant for internal use, provided by an employer for a very specific task. You will not find it on the Internet and I am not at liberty to disclose a lot about it. I would pounce at the opportunity to detect the notifications in an easier way if there was any. Capturing its screen every so often is my only chance.

Note that bringing it to my current virtual workspace from the secondary one does not help. Even with other common/popular applications. That is reported above in the thread.

pan64 07-29-2020 01:15 PM

Quote:

Originally Posted by lucmove (Post 6150641)
Capturing its screen every so often is my only chance.

That means you have to display it (I mean not on a virtual desktop). Probably you can use a virtual display (Xvfb) which will not actually show anything, but you can try to capture the content. https://unix.stackexchange.com/quest...my-xvfb-buffer

By the way I would still try to use a log file or some other way.


All times are GMT -5. The time now is 06:33 AM.