Need a Apple Script to sent a Webcam image every Day @ 10.00 am

Hello,
well i’m a noob when it comes to Apple Script…
I have try’ed Hazel but it was unsuccessfull …
So my hope is that i can do it also without a Helper Software…
Here is what i want to do:

I have a Webcam that saves every 60 sec a jpg in a folder for every day.
Now i want to get @ 10.00am every day the latest saved image from the cam and sent it by mail to specific address.

I hope that someone can help me or point me in the right direction…

So my Idea is:
use iCal to run a Script every day… that part is clear…
but then i need a Script that look in my Webcam Folder with Subfolders (every day a subfolder) and find the last created image and sent this to a email address …

many many thxxx

Stefan

If you already own Hazel, you can schedule a script to run from there. This should get you going:


set myFolder to "/Users/MadMac/Desktop/webcam folder"
set latestFile to first item of (every paragraph of (do shell script "ls -t " & quoted form of myFolder))
--return POSIX file myFolder
tell application "System Events"
	set latestFile to first item of (folder myFolder) whose name = latestFile
	set latestFile to latestFile as alias
end tell

tell application "Mail"
	activate
	set mymail to make new outgoing message at the beginning of outgoing messages with properties {subject:"Webcam"}
	tell mymail
		make new to recipient at beginning of to recipients with properties {address:"first@email.com, second@email.com"}
		make new attachment with properties {file name:latestFile}
		set content to "This is my content"
	end tell
	--show message window (otherwise it's hidden)
	set visible of mymail to true
	--bring Mail to front
	activate
	send mymail
end tell

Hey Many Thx for the Quick reply !!! :smiley:

works fine so far with only one folder …
but i have a subfolder for every day where the webcam images are stored in…
so the main folder is webcam
and inside i have a folder for every day like 2012-10-02, 2012-10-03 …
So the Path looks like this: /Users/MadMac/Pictures/webcam/2012-10-03
Is it possible that the Script looks in the subfolders ?

thank you very much!

well after searching the forums here i found something that seams to work but i’m not sure if it is ok :o

property sourceFolder : "/Users/MadMac/Pictures/Axis"

do shell script "/usr/bin/find " & quoted form of sourceFolder & " -iname \\*.jpg"
set allFiles to paragraphs of result

repeat with thisFile in allFiles
	set thisFile to POSIX file thisFile
	--whatever else
end repeat

tell application "System Events"
	--set thisFile to first item of (folder sourceFolder) whose name = thisFile
	set thisFile to thisFile as alias
end tell

tell application "Mail"
	activate
	set mymail to make new outgoing message at the beginning of outgoing messages with properties {subject:"Webcam"}
	tell mymail
		make new to recipient at beginning of to recipients with properties {address:"first@email.com, second@email.com"}
		make new attachment with properties {file name:thisFile}
		set content to "This is my content"
	end tell
	--show message window (otherwise it's hidden)
	set visible of mymail to true
	--bring Mail to front
	activate
	--send mymail
end tell

See if this one works better…

set myFolder to "/Users/MadMac/Desktop/webcam folder/"
set filePath to (do shell script "find " & quoted form of myFolder & " \\! -name \".*\" -type f -print0 | xargs -0 ls -t | head -n 1")
tell application "System Events" to set latestFile to file filePath as alias

tell application "Mail"
	activate
	set mymail to make new outgoing message at the beginning of outgoing messages with properties {subject:"Webcam"}
	tell mymail
		make new to recipient at beginning of to recipients with properties {address:"first@email.com, second@email.com"}
		make new attachment with properties {file name:latestFile}
		set content to "This is my content"
	end tell
	--show message window (otherwise it's hidden)
	set visible of mymail to true
	--bring Mail to front
	activate
	send mymail
end tell

:slight_smile: Wow thank you very much !!!
works perfect so far !!!
many many thx for taking the time to help me!
last request:
Is it possible to insert the current date in the subject of the Mail ?
like

{subject:"Webcam", & current date }

– doesn’t work…
and the content to: “Here are the actual image” + Name of the file i sent

thank you!!!

Stefan

Good luck :slight_smile:

set myFolder to "/Users/MadMac/Desktop/webcam folder/"
set filePath to (do shell script "find " & quoted form of myFolder & " -iname \\*jpg -print0 | xargs -0 ls -t | head -n 1")

tell application "System Events"
	set latestFile to file filePath as alias
	set fileName to name of latestFile
end tell

set mySubject to "Webcam " & (current date) as text
set myContent to "Here is the actual image: " & fileName as text


tell application "Mail"
	activate
	set mymail to make new outgoing message at the beginning of outgoing messages with properties {subject:mySubject}
	tell mymail
		make new to recipient at beginning of to recipients with properties {address:"first@email.com, second@email.com"}
		set content to myContent
		make new attachment with properties {file name:latestFile}
	end tell
	--show message window (otherwise it's hidden)
	set visible of mymail to true
	--bring Mail to front
	activate
	send mymail
end tell

adayzdone
you are my hero :slight_smile:

thank you so much for your help!
i hope i can give it back one time…
all is working perfect now :smiley:
and finally i understand much more now about Apple Script

thank you!

Stefan

Happy to help. Just posted one more edit to include content line.

hi adayzdone,
well i just tryed it out on my Mac and it takes now a image of my Webcam folders…
not the first & not the last…
hmmm i don’t know where the problem is… maybe you

well on my Home Computer it works perfect… checked that several times… but on my Remote computer where i have 100 Day folders with images not…

thank you!

Stefan

Is the folder structure different? Try replacing the set filePath line with the one from post 4. If that does not work, post an example of actual folder and file names.

Hi,
this seams to fix the problem…
but i’m not 100% sure because my next remote session is on tuesday …

set filePath to (do shell script "find " & quoted form of myFolder & " \\! -name \".*\" -type f -print0 | xargs -0 ls -t | head -n 1")

thank you!

Stefan

Hello,
so today i have access to my Remote Mac.
I have still this Error that it find’s the wrong file…
strange is, that it takes a file from the beginning… creation date of the file is ok
so also if i change the
set filePath to (do shell script “find " & quoted form of myFolder & " -iname \jpg -print0 | xargs -0 ls -t | head -n 1")
to
set filePath to (do shell script "find " & quoted form of myFolder & " \! -name ".
" -type f -print0 | xargs -0 ls -t | head -n 1”)

there is hoc change… the script takes only the file from 11-08-2012 13-01-23 Axis.jpg … doesn’t matter when i start the Script… at Midnight or early … the Script find only this file 11-08-2012 13-01-23 Axis.jpg strange…

here is a link to the Screenshot from the folder… http://imageshack.us/photo/my-images/837/bildschirmfoto20121009u.png/

here is the Script i use:

set myFolder to "/Users/holle/Pictures/SecuritySpy/Capture folder/Axis"
--set filePath to (do shell script "find " & quoted form of myFolder & " -iname \\*jpg -print0 | xargs -0 ls -t | head -n 1")
set filePath to (do shell script "find " & quoted form of myFolder & " \\! -name \".*\" -type f -print0 | xargs -0 ls -t | head -n 1")

tell application "System Events"
	set latestFile to file filePath as alias
	set fileName to name of latestFile
end tell


--Mail Stuff
set myAddress to "first@email.com, second@email.com" as text
set mySubject to "Test " & (current date) as text
set myContent to "Test Content " & fileName as text

tell application "Mail"
	activate
	set mymail to make new outgoing message at the beginning of outgoing messages with properties {subject:mySubject}
	tell mymail
		make new to recipient at beginning of to recipients with properties {address:myAddress}
		set content to myContent
		make new attachment with properties {file name:latestFile}
	end tell
	--show message window (otherwise it's hidden)
	set visible of mymail to true
	--bring Mail to front
	activate
	--send mymail
end tell

thank you very much for taking time !

Stefan

The original script used ls -t which sorted by time modified. Maybe ls -U which uses time of file creation, instead of last modification for sorting, will work?

set myFolder to "/Users/holle/Pictures/SecuritySpy/Capture folder/Axis"
--set filePath to (do shell script "find " & quoted form of myFolder & " -iname \\*jpg -print0 | xargs -0 ls -U | head -n 1")
set filePath to (do shell script "find " & quoted form of myFolder & " \\! -name \".*\" -type f -print0 | xargs -0 ls -U | head -n 1")

tell application "System Events"
	set latestFile to file filePath as alias
	set fileName to name of latestFile
end tell


--Mail Stuff
set myAddress to "first@email.com, second@email.com" as text
set mySubject to "Test " & (current date) as text
set myContent to "Test Content " & fileName as text

tell application "Mail"
	activate
	set mymail to make new outgoing message at the beginning of outgoing messages with properties {subject:mySubject}
	tell mymail
		make new to recipient at beginning of to recipients with properties {address:myAddress}
		set content to myContent
		make new attachment with properties {file name:latestFile}
	end tell
	--show message window (otherwise it's hidden)
	set visible of mymail to true
	--bring Mail to front
	activate
	--send mymail
end tell

Hi,
well thxx but if i use ls -U :

set filePath to (do shell script "find " & quoted form of myFolder & " \\! -name \".*\" -type f -print0 | xargs -0 ls -U | head -n 1")

i get the first image not the last …
How can i get now the last ?

many thxxx

Stefan

Why not the AppleScript sorting?


set myFolder to ((path to pictures folder as text) & "SecuritySpy:Capture folder:Axis:")
tell application "Finder"
	set latestFile to item 1 of (sort (get files of folder myFolder) by creation date) as alias
	set fileName to name of latestFile
end tell

I always forget that.

Try this:

set filePath to (do shell script "find " & quoted form of myFolder & " \\! -name \".*\" -type f -print0 | xargs -0 ls -U | tail -n 1")
set myFolder to "/Users/holle/Pictures/SecuritySpy/Capture folder/Axis"
--set filePath to (do shell script "find " & quoted form of myFolder & " -iname \\*jpg -print0 | xargs -0 ls -U | tail -n 1")
set filePath to (do shell script "find " & quoted form of myFolder & " \\! -name \".*\" -type f -print0 | xargs -0 ls -U | tail -n 1")

tell application "System Events"
	set latestFile to file filePath as alias
	set fileName to name of latestFile
end tell


--Mail Stuff
set myAddress to "first@email.com, second@email.com" as text
set mySubject to "Test " & (current date) as text
set myContent to "Test Content " & fileName as text

tell application "Mail"
	activate
	set mymail to make new outgoing message at the beginning of outgoing messages with properties {subject:mySubject}
	tell mymail
		make new to recipient at beginning of to recipients with properties {address:myAddress}
		set content to myContent
		make new attachment with properties {file name:latestFile}
	end tell
	--show message window (otherwise it's hidden)
	set visible of mymail to true
	--bring Mail to front
	activate
	--send mymail
end tell
set filePath to (do shell script "find " & quoted form of myFolder & " \\! -name \".*\" -type f -print0 | xargs -0 ls -U | tail -n 1")

works but no it takes a invisible .dat file from my Software…

so it’s possible only sort out the jpg ?

Did you try the commented line?

set filePath to (do shell script "find " & quoted form of myFolder & " -iname \\*jpg -print0 | xargs -0 ls -U | tail -n 1")

:slight_smile: Finally Works perfect so far !!!

Problem solved:D

many many thxxxx adayzdone !!!

& StefanK for suggestion… !

Stefan