ICTP Workshop 2007/EyaAppleScript

From Bjoern Hassler's website
Jump to: navigation, search

Version 3.12e of the original ICTP Producer AppleScript, translated into English[edit]

Keeshu 01:16, 15 December 2007 (CET) This version compiles, but there is still some Italian in the comments which I couldn't translate; any volunteers?

-- ----------------------------------------------------- -- scripts for Digital Lectures by M. Zennaro, E. Canessa and C. Fonda, ICTP-SDU -- licence: Creative Commons Attribution-Noncommercial-Share Alike 2.5 -- http://creativecommons.org/licenses/by-nc-sa/2.5/ -- ----------------------------------------------------- -- this version does some checking that the pictures are *really* taken and saved -- new in version 3.5: the image files are moved one by one when shooting, not all together at the end of the hour, to avoid overloading of ImageBrowser -- version 3.6 : now QT Player is quitted and restarted at the end of every hour... to avoid problems with file size growth with QT version 7.1.5 -- version 3.7 : now it removes the original folder after making and copying the TARfile, it also send a confirmation file "NAME_copied.ok" during the scp tranfer -- version 3.7b 13apr2007 : more info written on logfile, about the process of moving out photos during shooting loop. -- version 3.7c : now it keeps a minimum of 3 files in the Pictures/EyA folder, instead of just 2 -- version 3.8: now the number of the "actual photo" is incremented correctly in case of error -- version 3.9: corrected a major mistake in the test for the correct saving of previous picture (less than -> greater than) -- version 3.10: now it checks for Capture_00001.JPG images created after the first one, and delete them immediately (temporary solution) -- version 3.11: corrected an error in offset calculation (time of first shooting was wrong and always equal to 25 sec), added a FatalError flag and a global error control before sending the tarfile. -- version 3.12: changed folder name for both Application and Documents (now Applications/EyA-Producer and Documents/EyA-Producer) -- version 3.12e: translated comments and variables from Italian to English [Kees Huyser/Nikhef] -- -----------------------------------------------------

set interval to 15 (* Fixed delay (in seconds) between subsequent photos*) set GotError to false set FatalError to false

-- Initialize the log

do shell script "echo -n '*** Start task (15 sec interval, Vers. 3.11) at: '>> /var/log/recording.log ; date >> /var/log/recording.log"

-- Check the lock file

set LockFileUnix to "/tmp/DLrecording.lock" set reclocked to true tell application "Finder" to if not (exists LockFileUnix as POSIX file) then set reclocked to false set counter to 0 repeat until not reclocked tell application "Finder" to if not (exists LockFileUnix as POSIX file) then set reclocked to false delay 1 set counter to (counter + 1) if ((counter mod 5) = 0) then

-- Here you should do something if the lockfiles are removed too late (for now it sends a warning to the logfile every 5 sec)... do shell script "echo -n '>WARNING> Lockfile still present at: '>> /var/log/recording.log ; date >> /var/log/recording.log"

end if end repeat

-- There is no lock, we are free to create our lock files and continue

do shell script "echo -n 'Ok, no lockfile, starting at: '>> /var/log/recording.log ; date >> /var/log/recording.log"

try do shell script "touch /tmp/DLrecording.lock 2>> /var/log/recording.log" on error do shell script "echo -n '>WARNING> Cannot create lockfile at: '>> /var/log/recording.log ; date >> /var/log/recording.log" end try

-- Clean folders and files that may come into conflict with those to be generated

do shell script "rm -rf ~/Desktop/Movie.mov" do shell script "rm -rf ~/Pictures/EyA" try do shell script "mkdir ~/Pictures/EyA 2>> /var/log/recording.log" on error do shell script "echo -n '>>>ERROR> Cannot create folder Pictures:EyA at: '>> /var/log/recording.log ; date >> /var/log/recording.log" set FatalError to true end try

-- Create spreadsheet, as generated by date and time

set today to do shell script "date +%Y-%m-%d__%H%M%S" set PosixFolder to "~/Documents/EyA-Producer/" & today try do shell script "mkdir " & PosixFolder & " 2>> /var/log/recording.log" do shell script "mkdir " & PosixFolder & "/originals 2>> /var/log/recording.log" on error do shell script "echo -n '>>>ERROR> Cannot create output folder at: '>> /var/log/recording.log ; date >> /var/log/recording.log" set GotError to true set FatalError to true end try

-- Register in the log if not GotError then do shell script "echo 'Created output folder with name: " & today & "'>> /var/log/recording.log" end if set GotError to false

(* Activate QT and start recording

  • )


try tell application "QuickTime Player" activate set myMovie to (new movie recording) set myMovie to start recording true

(* set statoregistrazione to recording state of Movie - NON FUNZIONA *) (* do shell script "echo 'movie state is: " & statoregistrazione & "'>> /var/log/recording.log" - NON FUNZIONA *)

end tell on error do shell script "echo -n '>>>ERROR> Cannot start new recording at: '>> /var/log/recording.log ; date >> /var/log/recording.log" set GotError to true set FatalError to true end try

if not GotError then set theDate to current date set TotalSeconds to time of theDate set mySeconds to TotalSeconds mod 60 set Minutes to (TotalSeconds div 60) mod 60

-- Memorize the start time of the Movie set InitMovie to TotalSeconds

-- Memorize in StartDate the date and time for the beginning of registration set StartDate to do shell script "/bin/date '+%Y:%m:%d %H:%M'"

-- Register in the log do shell script "echo -n 'QT Movie recording started at: '>> /var/log/recording.log ; date >> /var/log/recording.log" end if set GotError to false

-- Waiting for the first picture taken

set TotalErrors to 0 -- Initialize the error counter set MissingPhoto to 0 -- Inizialize the counter of the number of missed photos (because of delays)

set NumberPrecedingPhoto to 0 -- setto i contatori delle foto per effettuare poi il controllo set NumberCurrentPhoto to 1

set theDate to current date set TotalSeconds to time of theDate set mySeconds to TotalSeconds mod 60 set Minutes to (TotalSeconds div 60) mod 60

repeat until (Seconds = 25) set theDate to current date set TotalSeconds to time of theDate

set mySeconds to TotalSeconds mod 60 set Minutes to (TotalSeconds div 60) mod 60

end repeat

-- qui provo a scattare la prima foto (e riprovo fino a che non ci riesco) -- Here I try to take the first shot (and riprovo extent to which there can)

set FirstDone to false repeat until (FirstDone = true)

set FirstMissed to true set CounterFirstPhoto to 0 repeat until (FirstMissed = false) set FirstMissed to false try tell application "RemoteCapture Task.app" activate end tell tell application "System Events" tell process "RemoteCapture Task" tell menu bar 1 tell menu bar item "Edit" tell menu "Edit" click menu item "Release" end tell end tell end tell end tell end tell on error do shell script "echo -n '>WARNING> First photo cannot be taken at: '>> /var/log/recording.log ; date >> /var/log/recording.log" set FirstMissed to true set MissingPhoto to MissingPhoto + 1 do shell script "echo '>INFO> Total number of errors until now is: " & MissingPhoto & ".'>> /var/log/recording.log" set CounterFirstPhoto to CounterFirstPhoto + 1 delay 1 end try

-- Try at most 5 times then write an error message and die if CounterFirstPhoto is greater than 5 then set FirstMissed to false set GotError to true do shell script "echo -n '>>>ERROR> Unable to shoot the first photo (after 5 trials) at: '>> /var/log/recording.log ; date >> /var/log/recording.log" set FatalError to true end if

end repeat

if GotError is false then do shell script "echo -n 'Shooting first photo at: '>> /var/log/recording.log ; date >> /var/log/recording.log" -- Register time taken (if successful) set theDate to current date set TotalSeconds to time of theDate

set mySeconds to TotalSeconds mod 60 set Minutes to (TotalSeconds div 60) mod 60 end if

-- Hours waiting until they see a file in the folder containing the saved photos

set WaitForFirstPhoto to 0 set CheckFirstPhoto to false

repeat until (CheckFirstPhoto = true) set NumberOfPhotos to do shell script "ls -1 ~/Pictures/EyA | wc -l | sed 's/^[ ]*//' " -- conto i files if NumberOfPhotos is greater than 0 then set FirstDone to true set CheckFirstPhoto to true do shell script "echo -n '> Ok, first photo saved on disk at: '>> /var/log/recording.log ; date >> /var/log/recording.log" end if delay 1

-- if no photo is saved within 15 seconds, try again to shoot... set WaitForFirstPhoto to WaitForFirstPhoto + 1 if WaitForFirstPhoto is greater than 15 then do shell script "echo -n '>WARNING> Unable to find the file of the first photo (after 15 seconds) at: '>> /var/log/recording.log ; date >> /var/log/recording.log"

set CheckFirstPhoto to true end if

end repeat

end repeat

-- Register the click in the log

set FirstPhoto to TotalSeconds -- Record time of the first photo shot delay 1

-- Register in file "offset.txt" the value of offset between the beginning of the movie and the first photos in seconds set MyOffset to (FirstPhoto - InitMovie) set ShellCommand to "echo '" & MyOffset & "' >" & PosixFolder & "/offset.txt 2>> /var/log/recording.log" try do shell script ShellCommand on error do shell script "echo -n '>>>ERROR> Cannot create file offset.txt: '>> /var/log/recording.log ; date >> /var/log/recording.log" set FatalError to true end try

-- Register value in the log do shell script "echo -n 'Offset of " & MyOffset & " sec written in offset.txt at: '>> /var/log/recording.log ; date >> /var/log/recording.log"

-- setto il counter delle foto per effettuare poi il controllo

set NumberCurrentPhoto to 2

-- Set a flag to eliminate the Capture_00001.JPG duplicate...

set OKFirstPhoto to true

-- The cycle begins when shooting periodic photos (from the second onwards)

repeat until ((Minutes = 59) and (Seconds > 10)) set theDate to current date set TotalSeconds to time of theDate

set mySeconds to TotalSeconds mod 60 set Minutes to (TotalSeconds div 60) mod 60

repeat until ((Seconds - 10) mod interval) = 0 set theDate to current date set TotalSeconds to time of theDate

set mySeconds to TotalSeconds mod 60 set Minutes to (TotalSeconds div 60) mod 60

end repeat


-- Click the picture here (and if there are errors write a log WARNING ...) -- But first record the attempt in the log

do shell script "echo -n 'Shooting photo #" & NumberCurrentPhoto & " at : '>> /var/log/recording.log ; date >> /var/log/recording.log"

try tell application "RemoteCapture Task.app" activate end tell tell application "System Events" tell process "RemoteCapture Task" tell menu bar 1 tell menu bar item "Edit" tell menu "Edit" click menu item "Release" end tell end tell end tell end tell end tell on error set MissingPhoto to MissingPhoto + 1 do shell script "echo -n '>WARNING> Photo #" & NumberCurrentPhoto & " failed to be taken [" & MissingPhoto & " photo(s) missed until now], at: '>> /var/log/recording.log ; date >> /var/log/recording.log"

set GotError to true end try

-- if there is no error for the current photo, I check that the PREVIOUS photo has been written to disk

if not GotError then

-- Meanwhile count how many files there are in the folder that contains pictures and saved in the final set NumberOfLocalPhotos to do shell script "ls -1 ~/Pictures/EyA | wc -l | sed 's/^[ ]*//' " set NumberOfFinalPhotos to do shell script "ls -1 " & PosixFolder & "/originals | wc -l | sed 's/^[ ]*//' " set NumberOfSavedPhotos to NumberOfLocalPhotos + NumberOfFinalPhotos

-- And then verify if the count increases...

if NumberCurrentPhoto is not equal to 2 then if NumberCurrentPhoto is greater than (NumberOfSavedPhotos + 1) then -- Here I know that there has been an error on the previous photo set TotalErrors to TotalErrors + 1 do shell script "echo ; echo -n '>WARNING> Previous photo (#" & (NumberCurrentPhoto - 1) & ") has not been taken [" & TotalErrors & " error(s) until now], checked at:'>> /var/log/recording.log ; date >> /var/log/recording.log" set NumberCurrentPhoto to (NumberCurrentPhoto - 1) -- devo ripetere la foto end if end if

do shell script "echo -n ' Image files: " & NumberOfLocalPhotos & " (saved) + " & NumberOfFinalPhotos & " (archived) = " & NumberOfSavedPhotos & ". '>> /var/log/recording.log"

if NumberOfLocalPhotos is greater than 4 then -- If I have saved at least 5 photos, move the first of these photos to the destination folder set NameOldPhoto to do shell script "ls -1 ~/Pictures/EyA | head -1"

-- Check that there isn't a duplicate photo named Capture_00001.JPG

if NameOldPhoto is equal to "Capture_00001.JPG" then if OKFirstPhoto is false then -- It is a duplicate do shell script "rm -f ~/Pictures/EyA/Capture_00001.JPG 2>> /var/log/recording.log" do shell script "echo; echo '>WARNING> Deleting duplicated " & NameOldPhoto & ".'>> /var/log/recording.log" else

-- This is really the first photo, move to archive

do shell script "mv -f ~/Pictures/EyA/" & NameOldPhoto & " " & PosixFolder & "/originals/ 2>> /var/log/recording.log" do shell script "echo 'Archiving " & NameOldPhoto & ".'>> /var/log/recording.log" set OKFirstPhoto to false -- The next but no... end if else do shell script "mv -f ~/Pictures/EyA/" & NameOldPhoto & " " & PosixFolder & "/originals/ 2>> /var/log/recording.log" do shell script "echo 'Archiving " & NameOldPhoto & ".'>> /var/log/recording.log" end if else do shell script "echo 'Nothing is moved.'>> /var/log/recording.log" end if

set NumberCurrentPhoto to (NumberCurrentPhoto + 1) -- Increase the counter for the next picture


end if

set GotError to false

delay 1

end repeat

-- Now I have come to minute 59 and expect one to arrive at 45 seconds (-15 sec at the end of the hour)

repeat until (Seconds > 40) set theDate to current date set TotalSeconds to time of theDate

set mySeconds to TotalSeconds mod 60 set Minutes to (TotalSeconds div 60) mod 60 delay 1 end repeat

-- Move all remaining photos to the final folder do shell script "echo -n 'Moving all the remaining image files: '>> /var/log/recording.log ; ls ~/Pictures/EyA | tr ' ' ' ' >> /var/log/recording.log ; echo ', at: '>> /var/log/recording.log ; date >> /var/log/recording.log" do shell script "mv -f ~/Pictures/EyA/* " & PosixFolder & "/originals/ 2>> /var/log/recording.log"

-- Finally log the missing photos

do shell script "echo '>INFO> A total of " & MissingPhoto & " + " & TotalErrors & " photo(s) are missing, at: '>> /var/log/recording.log"


(* Stop QT recording and Quit Quicktime

  • )

try tell application "QuickTime Player" activate set myMovie to stop recording true end tell on error do shell script "echo -n '>>>ERROR> Cannot stop movie recording at: '>> /var/log/recording.log ; date >> /var/log/recording.log" set GotError to true set FatalError to true end try

-- Set StopDate to the time of the last recording set StopDate to do shell script "/bin/date '+%H:%M'"

if not GotError then do shell script "echo -n 'QT Movie recording stopped at: '>> /var/log/recording.log ; date >> /var/log/recording.log" -- registro sul log

delay 1

-- Close the Movie window try tell application "QuickTime Player" activate close myMovie (* set Movie to close window true *) end tell on error do shell script "echo -n '>>>ERROR> Cannot close movie window at: '>> /var/log/recording.log ; date >> /var/log/recording.log" set GotError to true set FatalError to true end try

if not GotError then do shell script "echo -n 'QT Movie window closed at: '>> /var/log/recording.log ; date >> /var/log/recording.log" -- registro sul log

-- Check if the movie file exists on the DeskTop set NoMovie to do shell script "ls -1 ~/Desktop | grep Movie" if NoMovie is not equal to "Movie.mov" then set GotError to true do shell script "echo -n '>>>ERROR> Movie file has not been written, checked at: '>> /var/log/recording.log ; date >> /var/log/recording.log" set FatalError to true end if

-- If there are errors write them to the log and write the value of beginning and end of registration the file "date.txt"

if not GotError then do shell script "echo -n 'Ok, QT Movie file existence checked at: '>> /var/log/recording.log ; date >> /var/log/recording.log" set ShellCommand2 to "echo '" & StartDate & "-" & StopDate & "' >" & PosixFolder & "/date.txt 2>> /var/log/recording.log" try do shell script ShellCommand2 on error do shell script "echo -n '>>>ERROR> Cannot create file date.txt, at: '>> /var/log/recording.log ; date >> /var/log/recording.log" set FatalError to true end try

delay 1

-- Finally move the movie to the destination folder

try do shell script "mv ~/Desktop/Movie.mov " & PosixFolder & "/ 2>> /var/log/recording.log" on error do shell script "echo -n '>>>ERROR> Cannot move Movie.mov from Desktop to output folder, at: '>> /var/log/recording.log ; date >> /var/log/recording.log" set FatalError to true end try


end if end if end if

set GotError to false

-- Better stop and restart immediately after QuickTime Player, -- otherwise there are problems with the size of the .mov file growing -- continuously between a recording and later (QT version 7.1.5) do shell script "echo -n '+ Quitting QuickTime Player at: '>> /var/log/recording.log ; date >> /var/log/recording.log"

try tell application "QuickTime Player" quit saving no delay 1 end tell on error do shell script "echo -n '>WARNING> Cannot quit QuickTime Player at: '>> /var/log/recording.log ; date >> /var/log/recording.log" end try

do shell script "echo -n '+ Restarting QuickTime Player at: '>> /var/log/recording.log ; date >> /var/log/recording.log"

delay 2

try tell application "QuickTime Player" activate end tell on error do shell script "echo -n '>WARNING> Cannot restart QuickTime Player.app, at: '>> /var/log/recording.log ; date >> /var/log/recording.log" end try

(* Ready to take pictures

  • )

-- I can now remove the lock file do shell script "rm -rf /tmp/DLrecording.lock 2>> /var/log/recording.log" do shell script "echo -n '*** Removing LOCKfile at: '>> /var/log/recording.log ; date >> /var/log/recording.log"

-- Create a copy of logfile in the destination folder try do shell script "cp /var/log/recording.log " & PosixFolder & "/logfile.txt 2>> /var/log/recording.log" on error do shell script "echo -n '>WARNING> Cannot copy this logfile into the output folder, at: '>> /var/log/recording.log ; date >> /var/log/recording.log" end try

-- Create a TAR-file containing the output in the destination folder do shell script "echo -n '*** Start to create the TARfile " & today & ".tar, at: '>> /var/log/recording.log ; date >> /var/log/recording.log"

try do shell script "cd ~/Documents/EyA-Producer/ ; tar -cf " & today & ".tar " & today & " 2>> /var/log/recording.log" on error do shell script "echo -n '>>>ERROR> Cannot make the TARfile " & today & ".tar at: '>> /var/log/recording.log ; date >> /var/log/recording.log" set GotError to true set FatalError to true end try

-- Secure copy (SCP) the TAR-file to the machine MASTER - BUT ONLY IF THERE WERE NO ERRORS

if not FatalError then

-- read user@host.domain from file hostmaster.txt set DiplomaMaster to do shell script "cat /Applications/EyA-Producer/hostmaster.txt"

do shell script "echo -n '*** Start to copy the TARfile " & today & ".tar to " & DiplomaMaster & " with scp, at: '>> /var/log/recording.log ; date >> /var/log/recording.log"

-- read room name from file room-name.txt set RoomName to do shell script "cat /Applications/EyA-Producer/room-name.txt"

-- copy the TARfile and then a second (empty) file "SAMENAME_copied.ok" to confirm the end of the transfer try do shell script "cd ~/Documents/EyA-Producer/ ; touch " & today & "_copied.ok ; scp -q " & today & ".tar " & today & "_copied.ok " & DiplomaMaster & ":~/Documents/EyA-Master/" & RoomName & "/input/ 2>> /var/log/recording.log"

on error do shell script "echo -n '>WARNING> Cannot copy the TARfile " & today & ".tar to MASTER, at: '>> /var/log/recording.log ; date >> /var/log/recording.log" set GotError to true end try

-- Update copy of the logfile in the destination folder do shell script "cp -f /var/log/recording.log " & PosixFolder & "/logfile.txt 2>> /var/log/recording.log"

-- If the TAR-file transfer is successful, then delete the original folder (but keep the local copy of the TAR-file) if not GotError then try do shell script "cd ~/Documents/EyA-Producer/ ; rm -rf " & today & " " & today & "_copied.ok 2>> /var/log/recording.log" on error do shell script "echo -n '>WARNING> Cannot delete the folder " & today & ", at: '>> /var/log/recording.log ; date >> /var/log/recording.log" end try end if set GotError to false

-- Add the name of this TARfile to the list of work done in "tasks.txt". try do shell script "echo '" & today & "' >> ~/Documents/EyA-Producer/tasks.txt 2>> /var/log/recording.log" on error do shell script "echo -n '>WARNING> Cannot add " & today & " to tasks.txt at: '>> /var/log/recording.log ; date >> /var/log/recording.log" end try

else -- If there were fatal errors...

do shell script "echo '>>>ERROR> There have been one or more FATAL ERRORS during this 1h task.'>> /var/log/recording.log ; echo '>>>ERROR> Nothing has been transferred to Master (" & DiplomaMaster & ").'>> /var/log/recording.log ; echo '>>>ERROR> Check into folder ~/Documents/EyA-Producer/" & today & " to see the saved files, if any.'>> /var/log/recording.log"

end if

-- I greet the logfile do shell script "echo -n '*** End of this 1h task (EyA-producer with 15sec interval, version 3.12e) at: '>> /var/log/recording.log ; date >> /var/log/recording.log"