Executing an Applescript from iCal

I’ve cobbled together a script to run my backup program, send me an email of the log from the backup program, and do some general housekeeping. It works fine when I run it from within the Applescript Editor. However, I have now set it up as an event in iCal, naming the application version of the script as the file to be executed and it doens’t work. Since I don’t know how to look at any diagnostics, I’m not sure exactly where it is failing. I can tell that the backup is working fine but I never get the email of the log file. Below is the script, grateful for any pointers. The file references all point to a network mounted drive on another Mac, and yes, that drive is available at the time the script runs.

(* Run the backup )
set scriptText to “/Applications/SyncupX.app/Contents/MacOS/syncupx /Volumes/MS2”
do shell script scriptText
(
Send email with backup log in body )
tell application “TextEdit”
set theLog to file “MS2:syncUpLog.log” as alias
open theLog
set theContent to text of document 1
close document 1
quit
end tell
tell application “Mail”
set theMessage to make new outgoing message with properties {content:theContent, subject:“Lynda’s iBook Backup Result”}
tell theMessage to make new to recipient with properties {name:“Pete”, address:"pete@haworths.org"}
send theMessage
end tell
(
Delete the backup log file )
tell application “Finder”
delete file “MS2:syncUpLog.log”
end tell
(
Delete any files in the __removed backup folder that are > 10 days old *)
set scriptText to “find /Volumes/MS2/__removed -maxdepth 1 -type d -mtime +11 -exec /bin/rm -rf {} \;”
do shell script scriptText

Assuming it works as a script, I recommend that you put it in your Scripts folder as a compiled script and use the run script selection of the alarm, then select other and navigate to the script, then select when. I have iCal email me notices from a Eudora script all the time and they never fail me.

Thanks for the suggestion. What you describe is what I’ve done already so I’m kinda lost as to where to go from here. Is there any sort of log file produced by compiled Applescripts where I can at least find out what error is being encountered?

Been experimenting a bit with this problem. It seems to have nothing to do with the script being executed as an application from iCal as I originally thought

First, let me repeat what I’m trying to do. The backup writes data to a network mounted disk, MS2. AFter the backup runs, I am trying to send an email with the contents of the log file in it, syncUpLog.log, which also resides on the MS2 disk.

The error is in the following block of code

tell application “TextEdit”
set theLog to file “MS2:syncUpLog.log” as alias
open theLog
set theContent to text of document 1
close document 1
quit
end tell

When I run this code from the SCript Editor, I get an error “NSCannotCreateSCriptCommand” on the line that starts with “set theLog to file” and the word “alias” is highlighted. On the advice of a friend, I changed this line to:

set theLog to posix file “/Volumes/MS2/syncupLog.log” as alias

The script works fine with that syntax. However the ScriptEditor changes that line back to it’s original syntax when it runs the script. If I then try to execute the script again, it fails with the original error message.

So, somehow, the use of the posix file syntax neables everything to work OK, but I the ScriptEditor will not retain that syntax.

I would really appreciate any insight into this.

Thanks,
Pete

Hi,

too complicated to coerce around a few corners :wink:
a simple string is sufficient

tell application "TextEdit"
    open "MS2:syncUpLog.log"
...

but I would avoid TextEdit whenever I can, so

set theContent to read file "MS2:syncUpLog.log"

should do the same job, but much smarter

Many thanks for pointing me in the right direction, your code worked perfectly.

There is just one more thing I would like to do. The code I’m working on runs on a laptop, so when I’m at home and on my home network, it all works fine, but when I’m away from home, it obvioulsy fails because the network disk can’t be found. Is there some code I could put in there to check if the disk (or a file on the disk) is available and only execute the rest of the script if the disk is available?

Thanks,
Pete

Hi Pete,

yes, it’s possible with something like this

set theDisks to paragraphs of (do shell script "ls /Volumes")
if "myDisk" is in theDisks then
	-- do something
end if

Thanks again Stefan, that worked great too.
Pete