Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I use Cheese as my webcam software. I am trying to figure out a way to have it :

  • Start
  • Click a pic
  • Exit

when a script is executed. The script should not be asking for permissions and there shouldn't be any interruptions. So far,

#!/bin/bash 
cheese

I could only get it to do step 1. How do I do steps 2 and 3? The doc files don't mention an such option and I don't want to change the source code. (I don't mind camorama either)

share|improve this question
    
Have you seen this SO question or this one? –  jasonwryan Jul 7 '12 at 1:45
    
I hadn't. I believed this SE is the only one which would have relevant questions. Anyways, the mplayer solution doesn't work for me. It outputs a blank (green) png. –  user14517 Jul 7 '12 at 6:58
add comment

1 Answer 1

Voilá! Steps 2 & 3 follow:

This works, although it's awfully timing critical, tweak as you see fit, tried to comment it decently so you can see what's going on.

You'll need to install xdotool for this to work, as we're simulating keypresses to take the picture and exit (package 'xdotool')

Oh, and you'll need to turn off the 'countdown' feature in preferences, otherwise it'll likely CTRL-Q (quit) out of the program before it actually takes the shot.


#!/bin/bash
#
# L Nix <lornix@lornix.com>
# takeapic : take a photo with Cheese, using default settings, then exit
#
# start cheesing (2> because mine whines about cheesy stuff (ha!))
cheese 2>/dev/null &
# give WM some time to start up program (fails without this)
sleep 5
# set so we can determine if valid window(s) exist(s)
WINDOWIDS=""
# wait for up to 90 seconds (tweak this)
COUNTDOWN=90
while [ ${COUNTDOWN} -gt 0 ]; do
    WINDOWIDS=$(xdotool search --class "cheese" 2>/dev/null)
    if [ -n "${WINDOWIDS}" ]; then
        break
    fi
    sleep 1
    COUNTDOWN=$(( ${COUNTDOWN} - 1 ))
done
# did we get anything?
if [ -z "${WINDOWIDS}" ]; then
    echo "Cheese never started, something's wrong"
    exit 1
fi
# the shutter button is ALT-T
for WIDS in ${WINDOWIDS}; do
    # if you combine these like xdotool allows, it fails
    xdotool windowfocus ${WIDS} 2>/dev/null
    xdotool key alt+t 2>/dev/null
done
# pause a moment while taking photo
sleep 1
# now CTRL-Q out of the application
for WIDS in ${WINDOWIDS}; do
    xdotool windowfocus ${WIDS} 2>/dev/null
    xdotool key ctrl+q 2>/dev/null
done
#
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.