Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to detect a cd has been inserted and then start a script

Hi,

We are trying to automate the backup process on a cd-rw for our clients. Some will use a Mac mini, others a X-serve depending on their need. We created a shell script that is run every 8 hours to backup a postgres database automatically (and it is encryted at the same time). Everything workd perfectly but the cd burning. What we would like is to have a script auto detect when a cd-rw is inserted. With disk utilities, we can check if a cd is inserted and if it is rw (no problem there), but I would prefer not having to check with the script but instead the script that does the burning would start when a cd is inserted. How to do it?

Thanks

Patrice Drolet
Logiciels INFO-DATA inc.

Posted on Oct 17, 2005 7:08 AM

Reply
9 replies

Oct 17, 2005 6:00 PM in response to Patrice Drolet

I don't know how well it would work, but it might be possible to do something with AppleScript's "folder actions" to trigger when a disk is mounted (ie. an item is added) to the "/Volumes" folder. You would have to come up with test routines to run at that point (either in AppleScript or in your backup script) to see if the newly mounted disk is in fact a writable CD (and perhaps if it is the correct writable CD) before proceeding with the backup. There might also be conflicts with the "When you insert a blank CD" routines that you would have to watch out for. The "folder action" script might look something like this:<pre>on adding folder items to volfolder after receiving thedisks
-- << optional AppleScript commands here>>
do shell script "/path/to/backupscript.sh"
end adding folder items to</pre>The script should be saved in the "/Library/Scripts/Folder Action Scripts" folder. Then the script can be associated with the "/Volumes" folder by control-clicking the "/Volumes" folder and selecting "Attach a Folder Action…" (it might be necessary to "Enable Folder Actions" from the contextual menu as well).

Oct 17, 2005 6:12 PM in response to biovizier

Thanks a lot!

This is exactly what I was looking for. I was looking into this direction but not being an expert with AppleScript, it was tough (I am more of a java guy and now a somewhat not bad script programmer).

Because my company will deliver all the Mac's set up, there will be no problem to configure them.

I will try it ASAP.

Patrice

Oct 18, 2005 5:42 AM in response to Gary Kerbaugh

My guess would be that Patrice used, 'drutil status', which for a blank cdr returns.

Vendor Product Rev
MATSHITA CD-RW CW-8121 AA12

Type: CD-R Name: /dev/disk1
Cur Write: 8x CD Sessions: 0
Max Write: 8x CD Tracks: 0
Overwritable: 79:57:68 blocks: 359843 / 736.96MB / 702.82MiB
Space Free: 79:57:68 blocks: 359843 / 736.96MB / 702.82MiB
Space Used: 00:00:00 blocks: 0 / 0.00MB / 0.00MiB
Writability: appendable, blank, overwritable

Oct 19, 2005 4:56 PM in response to Nils C. Anderson

Hi,

Nils is right.

Here is the code:

...

reponse=`drutil discinfo | grep -c "erasable: 1"`
if [ $reponse = 1 ]; then
echo "Le cd est bien present et de type -RW!"
else
echo "ERREUR Pas de CD -RW dans le lecteur"
# house log...
insert_log "$FUNCTION" 0 "ERREUR Pas de CD -RW dans le lecteur"
exit 1
fi
reperr=`echo $?`
if [ $reperr = 0 ]; then
# erase disk and burn stuff - $BACKUPDIR is the folder to backup
reponse_texte=`drutil burn -erase -noverify $BACKUPDIR`
err_path=$?
if [ $err_path = 1 ]; then
echo "ERREUR: $reponse_texte"
insert_log "$FUNCTION" 0 "Probleme: $reponse_texte"
# there was an error - I attempt to simple erase it sot next time it might be ok
reponse_texte=`drutil erase full`
insert_log "$FUNCTION" 0 "Disque efface: $reponse_texte"
exit 1
fi

err_cmd=`echo ${reponse_texte} | grep -c "Usage"`
#echo $err_cmd
if [ $err_cmd = 1 ]; then
echo "ERREUR La commande pour graver le cd a echoue"
insert_log "$FUNCTION" 0 "La commande pour graver le cd a echoue"
exit 1
else
echo "Gravure SUCCES"
fi
insert_log "$FUNCTION" 1 "SUCCES de la gravure de la copie de securite"
new_echo "---------- `date` FIN Graver le dossier: ${BACKUPDIR}
----------"
.......

There is some code that is not relevant for others, but you can see how I did it.

Patrice

How to detect a cd has been inserted and then start a script

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.