Portfolio

About

Angelo Luchi
Software Engineer
Currently Residing Outside of Los Angeles

Purveyor of servers and API's. Deployer of apps and web tools. Writer of code. Maker of love and peace.

Contact

Links

Google Voice & FileMaker Screen shot 2011-08-11 at 10.39.55 PM

DropBucket dropbucket-5

fmShorten – Social Media Link Sharing Tool fmshorten-2

IP Camera – Image Capture (mac)

Recently a friend of mine from Art Center College of Design in South Pasadena, CA needed a quick app to capture images from an IP Based camera or Security Camera.

I decided that AppleScript was still useful and set out to write a very simple application using the AppleScript editor. This app, upon launch:

  • will prompt the user for a folder to send the images to,
  • the url of the IP based camera,
  • and the cycle rate it should capture still images at.

I didn’t stop there! I decided that some feedback and logging should happen.  So, I added a logger sub-routine (you can access it by going to the console app of your Mac: AppleScript-events.log), Growl support, and some nifty filename generator code using timestamps (it was not as easy as I thought).

I’ve really never written much in AppleScript before, quick little launchers and checks to see if certain applications were installed, so I was surprised when I found out I could build functions in it.

The application and source code as well as an example stop motion of the captured images is included here:

Download the DMG here!


Source Code:

(*
This application was written to help collect camera images
-- set camUrl to "http://www.seattle.gov/trafficcams/images/Fremont_36.jpg?1316748399706"
*)
log_event("**********Startup Initialization**********")
--
property script_name : "IP Camera Image Capture"
property written_by: "Angelo Luchi"
property script_description : "This script will be a basic application for gathering images from an IP camera and storing them on your computer. It will prompt you for a destination folder, camera URL, and interval in seconds."
property camUrl : "http://www.seattle.gov/trafficcams/images/Fremont_36.jpg"
property intervalSeconds : "60"
--
global deskFol1
--
try
--
set growlCheck to createGrowlPipe()
log_event("Register Growl Application: " & growlCheck)
if growlCheck is true then
log_event("Growl Applcaition Name: " & script_name)
end if
--
set deskFol1 to chooseFolder()
log_event("Selected Destination Folder: " & deskFol1)
log_event("POSIX Path: " & POSIX path of deskFol1)
--
enterUrl()
log_event("Property camUrl: " & camUrl)
--
enterInterval()
log_event("Property intervalSeconds: " & intervalSeconds)
--
log_event("Starting Image Capture")
log_event("Trying " & camUrl & " every " & intervalSeconds & " seconds.")
sendNotification("Error Alert", "Trying " & camUrl & " every " & intervalSeconds & " seconds.")
on error
sendNotification("Error Alert", "User Canceled Operations!")
quit application
end try

on idle

set fileName1 to makeFileName()
set destination_file to (deskFol1 & fileName1)
set thisImageSRC to camUrl
set newError to false
--
try
collectURLImage(thisImageSRC, destination_file)
on error
set newError to true
set p to POSIX path of destination_file
do shell script "rm " & p
end try
--

(* FOUND THAT THIS METHOD ISN'T THE BEST

tell application "URL Access Scripting"
try
with timeout of intervalSeconds seconds
download thisImageSRC to destination_file replacing yes
end timeout
on error
set newError to true
try
set p to POSIX path of destination_file
do shell script "rm " & p
on error errMsg
log_event("ERROR: " & errMsg)
end try
end try
end tell
*)
--
if newError is false then
log_event("Collected Image: " & fileName1)
sendNotification("Image Creation", "Collected Image: " & fileName1)
else
log_event("Error fetching URL: " & thisImageSRC)
sendNotification("Error Alert", "Error fetching URL: " & thisImageSRC)
end if
--
return intervalSeconds
end idle

on quit
log_event("**********Terminating Application**********")
continue quit
end quit

-- Misc Functions
--
-- Logger
on log_event(themessage)
set theLine to (do shell script "date +'%Y-%m-%d %H:%M:%S'" as string) & " " & themessage
do shell script "echo " & theLine & " >> ~/Library/Logs/AppleScript-events.log"
end log_event
-- Test for Growl Application
on testGrowl()
tell application "System Events"
set isRunning to (count of (every process whose name is "GrowlHelperApp")) > 0
end tell
return isRunning
end testGrowl

on createGrowlPipe()
set test to testGrowl()
if test is true then
try
tell application "GrowlHelperApp"
set the allNotificationsList to {"Image Creation", "Error Alert"}
set the enabledNotificationsList to {"Image Creation", "Error Alert"}
register as application script_name all notifications allNotificationsList default notifications enabledNotificationsList icon of application "Collect IP Camera Images"
return true
end tell
end try
else
return false
end if
end createGrowlPipe

on sendNotification(notification, msg)
set test to testGrowl()
if test is true then
try
tell application "GrowlHelperApp"
notify with name notification title script_name description msg application name script_name
end tell
end try
end if
end sendNotification

on makeFileName()
set theDate to the current date
copy theDate to b
set the month of b to January
set monthNum to (1 + (theDate - b + 1314864) div 2629728)
if (monthNum < 10) then
set monthNum to "0" & (monthNum as string)
end if
set yearNum to the year of theDate
set dateNum to the day of theDate
if (dateNum < 10) then
set dateNum to "0" & dateNum
end if
set hourNum to the hours of theDate
if (hourNum < 10) then
set hourNum to "0" & hourNum
end if
set minuteNum to the minutes of theDate
if (minuteNum < 10) then
set minuteNum to "0" & minuteNum
end if
set secondNum to the seconds of theDate
if (secondNum < 10) then set secondNum to "0" & secondNum end if -- set theFileName to (yearNum as string) & (monthNum as string) & (dateNum as string) & (hourNum as string) & (minuteNum as string) & (secondNum as string) & ".jpg" return theFileName end makeFileName on chooseFolder() set theVar to (choose folder with prompt "Please choose a folder to save the images to.") as string return theVar end chooseFolder on enterUrl() set theIcon to note repeat display dialog "Please enter a camera URL:" default answer camUrl with icon theIcon set camUrl to text returned of result try if camUrl = "" then error exit repeat on error set theIcon to stop end try end repeat return camUrl end enterUrl on enterInterval() set theIcon to note repeat display dialog "Please enter a time interval in seconds (> 10):" default answer intervalSeconds with icon theIcon
set intervalSeconds to text returned of result
try
if intervalSeconds < 10 then error
set intervalSeconds to intervalSeconds as number
exit repeat
on error
set theIcon to stop
end try
end repeat
return intervalSeconds
end enterInterval

 

 

on collectURLImage(urlAddress, destinationPath)
set input to quoted form of urlAddress
--set temp_file to (path to temporary items)
--set temp_name to do shell script "uuidgen"
set temp_file to POSIX path of destinationPath
set q_temp_file to quoted form of temp_file
--
set cmd to "curl -o " & q_temp_file & " " & input
--
do shell script cmd
end collectURLImage



Leave a Reply