WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: windowshot script - suggested change  (Read 186 times)

Offline Leee

  • Wiki Author
  • Full Member
  • *****
  • Posts: 234
windowshot script - suggested change
« on: October 18, 2026, 05:32:53 AM »
Hi Rich,

Lately, I've found myself wanting to capture *) full screen captures,  *) window captures and *) arbitrary-area-of-screen captures.  The tools available in Tiny Core (17.1 / x86_64) are sufficient for this purpose - in fact I've placed an icon on my jwm tray to give access to all three:

left click      = screenshot
middle click = windowshot
right click    = grabber

But, if I use them a lot, my home directory gets cluttered up fast.  I'm not using persistent home, so all of that clutter wants to go into mydata.tgz and... just no.

So I created directories on persistent storage for the screen capture files.  Alas, grabber has a little config file where you can set the save path to somewhere other than $HOME but windowshot and screenshot don't.  What I've done locally, and what I propose for the original windowshot and screenshot programs is to add logic allowing an optional argument, the save path, to be passed to these scripts.  Below are the changes I've made to windowshot (not showing the screenshot one because, apparently, I made an unrelated change to that one about fifteen years ago):

Original windowshot:
Code: [Select]
[ -z ${DISPLAY} ] && echo "Requires X" &&  exit 1
which xwininfo > /dev/null
[ "$?" -ne 0 ] && exec popup "Requires xwininfo, see Xorg-7.(5, 6 or 7)-bin.tcz" &&  exit 1
WINDOW_ID=`xwininfo -frame | grep " Window id:" | cut -d " " -f 4`
filename=windowshot-`date "+%Y%b%d-%H%M%S"`.png
/bin/sh -c "imlib2_grab -id $WINDOW_ID $HOME/$filename"
exec popup "Windowshot saved to $HOME/$filename"
Modified windowshot:

Code: [Select]
[ -n "${1}" ] && SAVEPATH="${1}" || SAVEPATH="${HOME}"
[ -d "${SAVEPATH}" ] || exec popup "windowshot: ERROR: ${SAVEPATH} not found"

[ -z ${DISPLAY} ] && echo "Requires X" &&  exit 1
which xwininfo > /dev/null
[ "$?" -ne 0 ] && exec popup "Requires xwininfo, see Xorg-7.(5, 6 or 7)-bin.tcz" &&  exit 1
WINDOW_ID=`xwininfo -frame | grep " Window id:" | cut -d " " -f 4`
filename=windowshot-`date "+%Y%b%d-%H%M%S"`.png

/bin/sh -c "imlib2_grab -id $WINDOW_ID $SAVEPATH/$filename"
[ -f "$SAVEPATH/$filename" ] && exec popup "Windowshot saved to $SAVEPATH/$filename" || exec popup "windowshot: ERROR: $SAVEPATH/$filename not saved"
Only two lines were added and two modified:

The first line of code (added) sets SAVEPATH to the first given argument, if there is one, otherwise defaulting to $HOME.

The second line (added), because I made plenty of typos while playing with this, bails with an error message if the specified save directory doesn't exist.

The next-to-last line (modified) substitutes $SAVEPATH for $HOME

The last line (modified), also substitutes $SAVEPATH for $HOME and I left in a check to only indicate success if  the output file was actually created and to report an error if it was not.
core 17.1 x86_64, jwm

Online Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 13028
Re: windowshot script - suggested change
« Reply #1 on: October 20, 2026, 03:35:31 PM »
Hi Leee
Sorry for the late reply. I've been fighting off a nasty cold.

You may already be aware of this, but both windowshot and grabber
can provide a screenshot.

With windowshot, click on an empty section of the desktop. If wbar is
running, clicking too close to it will copy the wbar since it's a window.

With grabber, doing a save without defining an area saves the whole screen.

I like your idea but I did a little rearranging of your code.
First a few general comments:
1. I try to place checks for missing requirements at the beginning of scripts.
2. I've been trying to be more POSIX aware and compliant when scripting.
3. I've become more cautious about stringing && and || clauses on the same line.

I've attached a copy of the version I modified. Here's a brief description by
line number of what it does:
Line
 1. shebang
 3. Exit if X is not running. No X means text mode so error message gets sent to stdout.
 5. Exit if popup not present. Error message gets sent to dmesg.
 7. Exit if xwininfo not present. The popup command displays the error message.
        The Xorg message has been changed to 7.7 since 7.5 and 7.6 are long gone.
 9. Preset the path to $HOME.
10. If a new path was passed in, convert it to an absolute path and use that.
12. Test that the path exists and is writable. Popup error message and exit if either test fails.
14. Fetch the window ID.
15. Build the file name to save to.
17. Create the image.
19. Preset the result message.
20. Check for existence of image, alter result message if needed.
21. Display result message using popup.


Let me know what you think. Questions are always welcome.