0

I am using 'imagesnap' command (via shell script) for capturing image from webcam in my OS X application in background using launchd.

FileName: CaptureCameraPic

#!/bin/bash

timestamp=$(date +"%Y%m%d%H%M%S")

homeDirectory=$HOME/Documents

# Create Project Directory

if [ -d "$homeDirectory/MyApp" ]; then
echo "MyApp Directory exists"
cd $homeDirectory/MyApp
homeDirectory=$homeDirectory/MyApp

else
echo "MyApp Directory does not exists"

echo "Creating MyApp Directory"

cd $homeDirectory/
mkdir MyApp

echo "Created MyApp Directory successfully"

cd $homeDirectory/MyApp
homeDirectory=$homeDirectory/MyApp

fi

# Camera
if [ -d "$homeDirectory/Camera" ]; then
# Control will enter here if $DIRECTORY exists.
echo "Camera Directory exists"
cd $homeDirectory/Camera
camera_filename="IMG_${timestamp}.jpg"
echo "Take picture $camera_filename"

imagesnap "$camera_filename"

else
echo "Directory does not exists"
echo "Create directory"
cd $homeDirectory
mkdir Camera
cd $homeDirectory/Camera
camera_filename="IMG_${timestamp}.jpg"
echo "Take picture $camera_filename"

imagesnap "$camera_filename"

fi

And my plist (test.plist) looks like

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>GroupName</key>
    <string>user</string>
    <key>UserName</key>
    <string>root</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>test</string>
    <key>ProgramArguments</key>
    <array>
        <string>Path_to_ CaptureCameraPic </string>
    </array>
    <key>OnDemand</key>
    <false/>
    <key>Nice</key>
    <integer>1</integer>
    <key>StartInterval</key>
    <integer>10</integer>
    <key>StandardErrorPath</key>
    <string>Path_to_Error_log.log</string>
    <key>StandardOutPath</key>
    <string>Path_to_Output_log.log</string>
</dict>
</plist>

I kept both the script and plist file in application bundle path.

In my os x application, on login button click, I copied the plist to /Library/LaunchDaemons/ .

The script starts running (using launchctl load path_to_plist_in_LaunchDaemon_folder) on (I can see logs print in output file, like folder creation, etc).

But for 'imagesnap', it is throwing error

imagesnap: command not found

But when I am running this script on terminal like

chmod +x CaptureCameraPic
./CaptureCameraPic

It is working fine.

Please need help.

1 Answers1

0

As @Rogier suggests, you need the full path to imagesnap in your script. You can find that with:

which imagesnap

or

find /usr /opt -name imagesnap -type f -print

Then you would use the following in your script:

/path/to/imagesnap ...

Also, there is a lot of messing around in your script, you can do things much more simply like this:

#!/bin/bash

# Set a variable just once so there is less maintenance
photodir="$HOME/Documents/MyApp/Camera"

# Make the directory if it doesn't exist, don't complain if it does
mkdir -p "$photodir"

cd "$photodir"
if [ $? -ne 0 ]; then
   echo "Failed to set working directory to $photodir"
   exit 1
fi
...
/path/to/imagesnap ...

Also, your timestamp is pretty horrible - imagine having to parse it later! Put some separators in it - maybe this:

date +"%Y-%m-%d_%H:%M:%S"
2017-01-27_10:24:35 
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432