I have (by hook and by crook - stolen from the internet) created a script that copies files to a network folder when dropped into a local folder:
property Folder1 : "ENG_HDD_02:JonE:FolderOut"
on adding folder items to this_folder after receiving these_items
tell application "Finder"
if exists disk "ENG_HDD_02" then
duplicate these_items to folder Folder1 without replacing
else
try
mount volume "smb://172.16.30.74/ENG_HDD_02" as user name "admin" with password "yeahright"
end try
repeat until (list disks) contains "ENG_HDD_02"
end repeat
end if
duplicate these_items to folder Folder1 without replacing
end tell
end adding folder items to
As you can see, when a file is dropped, it makes sure the network share is mounted before copy. If not it will mount it and then copy the file across.
I’d like to be able to create a log file that logs each stage to a file i.e.:
Volume mount check
Copy check
but I have no idea on how to achieve this. Any help would be really appreciated.
I’m using an universal log handler which writes the log file UTF8 encoded into ~/Library/Logs
I haven’t tested it but try this
property logName : "mountENG_HDD_02.log"
property Folder1 : "ENG_HDD_02:JonE:FolderOut"
on adding folder items to this_folder after receiving these_items
if (do shell script "/bin/ls /Volumes" does not contain "ENG_HDD_02") then
try
mount volume "smb://172.16.30.74/ENG_HDD_02" as user name "admin" with password "yeahright"
writeToLog from "Volume ENG_HDD_02 mounted"
on error e
writeToLog from "mount error: " & e
return
end try
end if
repeat with anItem in these_items
tell application "Finder"
set fileName to name of anItem
if exists file fileName of folder Folder1 then
writeToLog of me from "file " & fileName & " not copied"
else
duplicate anItem to folder Folder1
writeToLog of me from "file " & fileName & " copied"
end if
end tell
end repeat
end adding folder items to
on writeToLog from theMessage
tell (current date) to set timestamp to short date string & space & time string & ": "
set logFile to ((path to library folder from user domain as text) & "Logs:" & logName)
try
set the logStream to open for access file logFile with write permission
set logFileEof to get eof of the logStream
write timestamp & theMessage & return to logStream starting at eof as «class utf8»
close access logStream
on error
try
close access file logFile
end try
end try
end writeToLog
Really appreciate your help and time on this. I’ve just tried your script, and unfortunately it doesn’t work. The Copy to the SMB mount doesn’t work if the mount is there, and the script doesn’t mount the volume if it isn’t there.
I wasn’t sure how the log file gets created initially, but noticed it haddn’t. So i created a file at the location, but nothing got written. I’ve only had a quick go as i’ve run out of time… so I intend to spend more time on it tomorrow.
if (do shell script "/bin/ls /Volumes") does not contain "ENG_HDD_02" then
I tested the script successfully with an AFP volume by removing the on adding handler and specifying
the parameters this_folder and these_items manually
So it looks like I didn’t do my preparatory work properly and analyse the files that I need to copy. Schoolboy error…
The files that need to be copied are log files that are created and named using the current days date (eg ‘22/09/11.txt’). These files are then written to and appended to during the day at irregular intervals. At around 21:30 (+/- 10 minutes) the next day’s file is created (eg 23/09/11.txt) and then appended to, as described above.
The script Stefan has kindly helped me with, because of my mistake picks up an empty file when its created. What needs to happen, I think(!)… is that when a new file is created, the script should read the new file name, and then copy the file from the previous day. It will be interesting to know how this is achieved at a change in month also! Unless anyone has a better idea!