run script against output file after writing to disk (openALPR)
Hello,
I'm looking to do some operations against recorded files after they have been fully written to disk. I see that there is the option of using AppleScript as an Action. But I am unclear how to get the filename of the captured event out of it.
Here is my scenario:
I want to use the alpr software to detect license plates.
I want to run this each time a new capture event is written to disk. I do not want to start the action until the recording is finished, and the recording file is closed.
I would like the ability in the SecuritySpy UI to "run script after capture"
I would like to use any type of script, ideally a bash shell script, or even python.
What would be the best way to do this?
Thanks!
I'm looking to do some operations against recorded files after they have been fully written to disk. I see that there is the option of using AppleScript as an Action. But I am unclear how to get the filename of the captured event out of it.
Here is my scenario:
I want to use the alpr software to detect license plates.
I want to run this each time a new capture event is written to disk. I do not want to start the action until the recording is finished, and the recording file is closed.
I would like the ability in the SecuritySpy UI to "run script after capture"
I would like to use any type of script, ideally a bash shell script, or even python.
What would be the best way to do this?
Thanks!
Comments
Please let me know if you need someone to beta test the "run script after capture" functionality!
As it's an advanced feature that won't be useful for most users, I didn't want to clutter the UI with a new setting for it. So, what you need to do is create a script using Script Editor and save it with the name "ProcessCapturedFile" in the ~/SecuritySpy folder (i.e. the SecuritySpy folder within your Home folder). It must have the file extension ".scpt" (this is added by Script Editor by default).
If this script exists, SecuritySpy will invoke it whenever a captured file has finished, and will pass in the camera number, camera name, and file path as parameters. Here's an example script that demonstrates how to get the parameters:
on run args
set cameraNum to item 1 of args
set cameraName to item 2 of args
set filePath to item 3 of args
end run
Note that it will be invoked for all captured files - movies and images. If you want to ignore images, then you'll need to check the file extension for .mov or .m4v files, and ignore .jpg files.
Please test this and let me know if it works as expected.
I put the ProcessCapturedFile.scpt script on the Script folder on my SS user folder
and set the script as action of a webcam. Set the Webcam active with ACTION but nothing happen.
(works with other script, so isn't a SS configuration mistakes.)
on run args
set cameraNum to item 1 of args
set cameraName to item 2 of args
set filePath to item 3 of args
display dialog cameraNum & cameraName & filePath
end run
if I run from shell console
osascript ProcessCapturedFile.scpt 1 2 3
it works.
What I'm wrong?
Thanks
This is my script if anyone is interested!
on run args
set cameraNum to item 1 of args
set cameraName to item 2 of args
set filePath to item 3 of args
set message to "'ALERT ON '" & cameraName
do shell script "/usr/bin/curl -i -X GET https://api.telegram.org/botSECRET/sendMessage\\?chat_id\\=CHATID\\&text\\=" & message
if (filePath contains "jpg") then
do shell script "/usr/bin/curl -s -X POST https://api.telegram.org/botSECRET/sendPhoto -F chat_id=CHATID -F photo='@'" & "\"" & filePath & "\""
else
do shell script "/usr/bin/curl -s -X POST https://api.telegram.org/botSECRET/sendVideo -F chat_id=CHATID -F video='@'" & "\"" & filePath & "\""
end if
end run
yesterday I have putt my webcam in continuous mode with a file creation every hours.
Using the script above, every hour I have a telegram notification.
I think that the ProcessCapturedFile.script trigs on the Continuous capture "file cut" time.
Is it possible to exclude this behavior?
Thanks
Martino
You can distinguish motion-capture movie files from continuous-capture movie file by the presence of the string " M " vs. the string " C " in the file name, for example like this:
if filePath contains " M " then
do something
end if
Note the space characters around the M and C. And make sure that no other folder within the path, nor the camera name itself, contains this string.
EDIT: I'd like to see events like this so I can keep track of recorded files without calling the ++download method.
In the case of the request here: https://www.bensoftware.com/forum/discussion/2679/attaching-video-to-imessage - if the created file was in the event stream, the doorbell push could just invoke sending the last recorded file for camera X. I'd like to integrate something like this into the Go SDK so it can be exposed in other apps. Reading through some of the feature requests in the forums makes me believe that with the right events in the stream, a "sidekick" Go daemon could fit users' needs.
I use the script above for push notification on telegram but I can't find a way to add the cause of the motion detection (like "motion detected human" or "motion detected sound").
Is there any way?
Thanks
M.
In addition, the ProcessCapturedFile script is not the best way to generate notifications for motion detected, because it is only invoked when a captured files has finished, which can be some time after the actual motion event that caused it.
A better way would be to put your custom script into the ~/SecuritySpy/Scripts folder, and then set this as an action for the camera(s) in question. But again there is no way to get the trigger reason from such a script, as yet.
Thanks!
@Ben may the same script works as action father the camera? (the question is if the action pass the 2 argument even if I attach the script as the action or I need to modify the script)
Thanks
@Ben, is there any way to have the path of the captured image and the captured video passed to the apple script as argument?
I know that the 3rd argument is the path but not the name of the file..
Thanks
M.
The parameters passed to the script are now as follows:
1. The camera number
2. The camera name
3. The reason for the trigger, which is a comma-separated list of the following reasons: Motion,Audio,Script,CameraEvent,WebEvent,OtherCamera,Manual,Human,Vehicle
Please confirm that this works as expected. Note that you might have more than one trigger for the event.
As for the ProcessCapturedFile script, the third parameter is the full path to the captured file - including the file name itself (as the final path component).
Or tell me how I ca troubleshoot my script with ss?
If I pas manually arg to my script from shell it works..
but SS do nothing..
Thanks!
/usr/bin/osascript /Users/user1/SecuritySpy/ProcessCapturedFile.scpt 3 Cucina /Volumes/Webcam/Cucina/2019-10-21/21-10-2019 21-05-22 Cucina.jpg
where is the reason arg?
Thanks
So,
i'm trying to adapt my script to run as Action and not as ProcessCapturedFile script
but I can't find any way to retrive the motion capture file path to pass as video file..
may you help me?
Thanks
#CODE
on run args
set cameraNum to item 1 of args
set cameraName to item 2 of args
set reason to item 3 of args
set messaggio to reason & "' in '" & cameraName
tell application "SecuritySpy" to capture image as "/Users/user1/SecuritySpy/Scripts/" & cameraName & ".jpg" camera name cameraName with overwrite
tell application "SecuritySpy"
get current motion capture file camera number cameraNum
set rawcapturefile to result as text
do shell script "/usr/bin/curl -i -X GET https://api.telegram.org/botKEY/sendMessage\\?chat_id\\=CHATID\\&text\\=" & rawcapturefile
end tell
do shell script "/usr/bin/curl -i -X GET https://api.telegram.org/botKEY/sendMessage\\?chat_id\\=CHATID\\&text\\=" & messaggio
delay 3
do shell script "/usr/bin/curl -i -X GET https://api.telegram.org/botKEY/sendMessage\\?chat_id\\=CHATID\\&text\\=" & rawcapturefile
do shell script "/usr/bin/curl -s -X POST https://api.telegram.org/botKEY/sendPhoto -F chat_id=ID -F photo='@/Users/user1//SecuritySpy/Scripts/'" & cameraName & "'.jpg'"
end run
#######
Conversely, the script that is called as an Action does have the trigger reason parameter passed to it, because a trigger always invokes the Action, and the script is run at the time of the trigger. However, Actions are separate from any kind of recording - it's very common to have an Action without a recording - and therefore no file path is passed to this script.
So, you can either be notified of file completion via the ProcessCapturedFile script, or of motion triggers via the Action script. If you want to be notified of a Motion Capture recording, and also obtain the trigger reason, then it's probably best to have your script invoked as an Action, and then use the "get current motion capture file" to check for a recording. Note however that this file won't be usable until recording has finished.