Skip to content

Automatically Capture and Email Webcam Snapshots upon Lid Open #33

@beowolx

Description

@beowolx

Hi folks!

I recently noticed that DND do not take snapshot pictures upon lid open anymore (not sure why?) and there are no examples of how to automate the sending of emails when DND activates.

So I wrote my own script and want to share it with the community. Follow the step-by-step below to:

  1. Make DND take a picture when Lid Open
  2. Send an email with the picture and DND logs

Step-by-Step Guide

Step 1: Install Required Dependencies

First, install imagesnap, a command-line tool that captures images from your Mac's webcam.

Open your Terminal and run:

brew install imagesnap

Step 2: Create pics folder

Go to /Library/Objective-See/DND folder and create a pics folder. That's where the pictures will be stored.

Step 3: Create the Automation Script

Open Script Editor (Applications → Utilities → Script Editor), and paste the following AppleScript:

-- Define folder paths and email address
set baseFolder to "/Library/Objective-See/DND"
set picsFolder to baseFolder & "/pics"
set logPath to baseFolder & "/DND.log"
set destEmail to "YOUR_EMAIL@email.com"

-- Generate a timestamp and build the photo path
set timestamp to do shell script "date +%Y-%m-%d_%H-%M-%S"
set photoPath to picsFolder & "/dnd_snapshot_" & timestamp & ".jpg"

-- Capture a snapshot
do shell script "/opt/homebrew/bin/imagesnap -w 2 " & quoted form of photoPath

-- Create and send the email via the Mail app
tell application "Mail"
	set newMessage to make new outgoing message with properties {subject:"DND Alert: Lid Opened", content:"Lid opened. See attached snapshot and log.", visible:false}
	tell newMessage
		make new to recipient at end of to recipients with properties {address:destEmail}
		delay 1
		try
			make new attachment with properties {file name:(photoPath as POSIX file)} at after the last paragraph
			make new attachment with properties {file name:(logPath as POSIX file)} at after the last paragraph
		end try
	end tell
	send newMessage
end tell

Replace YOUR_EMAIL@email.com with your actual email address.

Step 3: Save the Script as an Application

  • In Script Editor, click File → Export....
  • Set the File Format to Application.
  • IMPORTANT: You must save it in /Applications.

Step 4: Run the script using Script Editor to activate permissions

Run the script, just click in the arrow in the "Script Editor" so you are prompted to authorise imagesnap and the script to run.

Step 5: Configure Objective-See's Do Not Disturb (DND)

  • Open Do Not Disturb preferences.
  • In the "Action" settings, select your saved automation application (DNDAlert.app). Add this command: open /Applications/DNDAlert.app
  • Save your changes.

Step 6: Test Your Setup

  • Trigger DND (by closing and opening your MacBook lid).
  • Check your email inbox: You should receive an email with a webcam snapshot and DND logs attached.

Done!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions