I’m hoping someone could help me finish this script. It’s part of an automated dvd backup routine I’m working on.
OVERVIEW
This script executes whenever a dvd is inserted via the system prefs. I’ve got two SuperDrives, using FairMount to mount and read the DVDs, applescript to separate them from the rest of the volumes and do shell ditto -rsrc to make the backups.
PLEASE HELP
This process works in the background (which is great), but I can’t figure out how to get it to notify me when a copy is completed. I’d really love to be able to have a SPEAK TEXT command, but a simple repeat beep/alarm would work great too.
Thanks in advance for helping me out! I’ve learned so much on this forum! Much appreciated!
delay 20
tell application "FairMount"
launch
delay 20
end tell
set the_disks to list disks
set diskCount to count of the_disks
if the diskCount is greater than 8 then
tell application "Finder" to set cd_name to item 1 of (get name of (disks whose capacity is less than 9.0E+9))
set the_cd to (quoted form of POSIX path of ("Volumes:" & cd_name))
set target_file to (quoted form of POSIX path of (("Volumes:SpeedDisk:Encode:") & cd_name))
tell application "Finder" to set cd_name2 to item 2 of (get name of (disks whose capacity is less than 9.0E+9))
set the_cd2 to (quoted form of POSIX path of ("Volumes:" & cd_name2))
set target_file2 to (quoted form of POSIX path of (("Volumes:SpeedDisk:Encode:") & cd_name2))
do shell script "ditto -rsrc " & the_cd & " " & target_file & " &> /dev/null &" & " ditto -rsrc " & the_cd2 & " " & target_file2 & " &> /dev/null &"
else
tell application "Finder" to set cd_name to item 1 of (get name of (disks whose capacity is less than 9.0E+9))
set the_cd to (quoted form of POSIX path of ("Volumes:" & cd_name))
set target_file to (quoted form of POSIX path of (("Volumes:SpeedDisk:Encode:") & cd_name))
do shell script "ditto -rsrc " & the_cd & " " & target_file & " &> /dev/null &"
end if
It beeps precisely when it means to, which is after the do shell script command finishes. The do shell script returns almost immediately because you have ditto running in the background. If it suits your needs, you could try removing the ampersands that are making ditto run in the background.
Also, I thought I would offer this slightly changed (and untested) script:
try
delay 20
tell application "FairMount"
launch
delay 20
end tell
tell application "System Events" to count disks
if result is greater than 8 then
tell application "System Events" to items 1 thru 2 of ({nameList:name, pathList:POSIX path} of disks whose capacity is less than 9.0E+9)
tell result
set firstCD to quoted form of first item of pathList
set firstTarget to quoted form of POSIX path of ("/Volumes/SpeedDisk/Encode/" & first item of nameList)
set secondCD to quoted form of second item of pathList
set secondTarget to quoted form of POSIX path of ("/Volumes/SpeedDisk/Encode/" & second item of nameList)
end tell
do shell script "/usr/bin/ditto -rsrc " & firstCD & " " & firstTarget & " &> /dev/null & " & ¬
"/usr/bin/Ëšditto -rsrc " & secondCD & " " & secondTarget & " &> /dev/null &"
else
tell application "System Events" to {firstName:name, firstCD:POSIX path} of first disk whose capacity is less than 9.0E+9
set firstTarget to quoted form of POSIX path of ("/Volumes/SpeedDisk/Encode/" & firstName)
do shell script "/usr/bin/ditto -rsrc " & quoted form of firstCD & " " & firstTarget & " &> /dev/null &"
end if
on error errMsg number errNum
display alert "Error " & errNum message errMsg buttons {"Cancel"} default button 1
end try
Bruce Phillips is right, you’re telling the ditto command to return immediately to the script without waiting for ditto to complete with the & at the end of the command. You can still run ditto in the background and still find out when the ditto commands completes. For example if at the end of your ditto command, even after the &, add “echo $!”. This will return the process id of the ditto process. Then you can monitor for that PID.
Here’s an example of getting the PID…
-- to get the pid of the process use "echo $1"
do shell script "/bin/sleep 5 > /dev/null 2>&1 & echo $!"
set the_pid to the result
Here’s an example how to monitor for the PID…
-- to check if the process is still running. If "" is returned then the process is finished. If the pid number is returned then the process is still running.
set the_pid_answer to do shell script "ps ax | grep " & the_pid & " | grep -v grep | awk '{ print $1 }'"