So, I have this camera system which automatically uploads Videos to an FTP server when it detects motion.
Those files, I want to move to another FTP server automatically, (my Home Assistant server), which then pings my phone with a notification.
To Download and Upload between the two FTP servers, I'm using a tool called FavoriteFTP for Mac OS X. It allows me to set up profiles which you can run through a command line.
I then sat up 2 "Apps" through Automator to launch the different Profiles in Favorite FTP. One for Downloading all files from the remote FTP server and one for Uploading those files to another FTP (My Home Assistant Server).
Last but not least, I made a third Automator App, to transcode the files from AVI to MP4 using HandbrakeCLI.
So here are the 3 Automator Apps:
This is the ftpdownload app:
/Applications/FavoriteFTP.app/Contents/MacOS/FavoriteFTPPro -profile "Download from FTP"
This is the ftptranscode app:
SRC="/Users/frederik/cctv/motion"
DEST="/Users/frederik/cctv/motion"
DEST_EXT=mp4
HANDBRAKE_CLI=HandBrakeCLI
for FILE in "$SRC"/.avi
do
filename=$(basename "$FILE")
extension=${filename##.}
filename=${filename%.}
/usr/local/bin/HandBrakeCLI -i "$FILE" -o "$DEST"/"$filename".$DEST_EXT
done
find /Users/frederik/cctv/motion -name '.avi' -exec rm -r {} ;
And this is the ftpsync app (upload to second FTP):
/Applications/FavoriteFTP.app/Contents/MacOS/FavoriteFTPPro -profile "Upload CH1 to HASS
What my AppleScript then does, is running these 3 Apps in an infinite loop
on idle
tell application "System Events"
tell application "ftpdownload"
run
delay 10
end tell
tell application "ftptranscode"
run
delay 20
end tell
tell application "ftpsyncapp"
run
end tell
tell application "ftpsyncappCH2"
run
end tell
return 10
end tell
end idle
So to summarize: ftpdownload is the app which downloads all AVI files from the remote FTP server ftptranscode is the app which converts those files into MP4 ftpsyncapp is the app which then uploads those files to the second FTP server and ftpsyncappCH2 does the same, but only with files with the word "CH2" in the filenames
Here's the problem After a day or two, I get an error from one of the Automator Scripts (see image below). However I just get a single Window Popup. So Im thinking it's just one of the 3 automator Apps.
https://i.imgur.com/bmz7Xxv.png
It seems that there's a limit on how many times I can loop this (I just need this running forever)
So right now, I have to click "OK" every day, which is a bit annoying.
How do I avoid getting this error message once a day, so my scripts can run in a loop forever?
I'm going to offer two suggestions.
First: Instead of using automator, create a stay-open script application that uses the do shell script
function of AppleScript to do the work. The idle handler for that would look something like this:
on idle
do shell script "/Applications/FavoriteFTP.app/Contents/MacOS/FavoriteFTPPro -profile 'Download from FTP'"
do shell script "
SRC='/Users/frederik/cctv/motion'
DEST='/Users/frederik/cctv/motion'
DEST_EXT=mp4
HANDBRAKE_CLI=HandBrakeCLI
for FILE in \"$SRC\"/.avi
do
filename=$(basename \"$FILE\")
extension=${filename##.}
filename=${filename%.}
/usr/local/bin/HandBrakeCLI -i \"$FILE\" -o \"$DEST\"/\"$filename\".$DEST_EXT
done
find /Users/frederik/cctv/motion -name '.avi' -exec rm -r {} ;"
do shell script "/Applications/FavoriteFTP.app/Contents/MacOS/FavoriteFTPPro -profile 'Upload CH1 to HASS'"
do shell script "/Applications/FavoriteFTP.app/Contents/MacOS/FavoriteFTPPro -profile 'Upload CH2 to HASS'"
-- check every 30 seconds
return 30
end idle
Note that do shell script
will in most cases wait for a response from the called script before advancing to the next applescript command, so the delay
commands wouldn't be necessary. If the Favorite FTP is running its commands on detached background threads that might cause issues, but that seems unlikely.
Also, I used a 30 second idle period because I didn't want to re-invoke the FavoriteFTP app too often. By default, script apps have a one second idle interval; returning 30 makes that a 30 second interval, which ought to be enough for the app to finish its last cycle.
Second, since this is really just a set of shell scripts, and you're only using AppleScript as a container to run them periodically, you could just write the whole thing as a single shell script:
/Applications/FavoriteFTP.app/Contents/MacOS/FavoriteFTPPro -profile 'Download from FTP'
SRC='/Users/frederik/cctv/motion'
DEST='/Users/frederik/cctv/motion'
DEST_EXT=mp4
HANDBRAKE_CLI=HandBrakeCLI
for FILE in \"$SRC\"/.avi
do
filename=$(basename \"$FILE\")
extension=${filename##.}
filename=${filename%.}
/usr/local/bin/HandBrakeCLI -i \"$FILE\" -o \"$DEST\"/\"$filename\".$DEST_EXT
done
find /Users/frederik/cctv/motion -name '.avi' -exec rm -r {} ;
/Applications/FavoriteFTP.app/Contents/MacOS/FavoriteFTPPro -profile 'Upload CH1 to HASS'
/Applications/FavoriteFTP.app/Contents/MacOS/FavoriteFTPPro -profile 'Upload CH2 to HASS'
save it to disk, and run it on schedule as a launchd job. Launchd is designed to do that. You'll write a plist file that looks something like this:
<?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>Label</key>
<string>user.ftp.transfer.mdvideos</string>
<key>Program</key>
<string>/path/to/shellscript</string>
<key>StartInterval</key>
<integer>30</integer>
</dict>
</plist>
save it at ~/Library/LaunchAgents/user.ftp.transfer.mdvideos.plist
, then load it as a job using the unix command
launchctl load ~/Library/LaunchAgents/user.ftp.transfer.mdvideos.plist`
See man launchctl
and man launchd.plist
for details
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.