Problems with Mail.app script and System Events
Problems with Mail.app script and System Events
- Subject: Problems with Mail.app script and System Events
- From: Robert Jacobson <email@hidden>
- Date: Wed, 9 Nov 2005 13:34:54 -0500
I wrote an Perl script which formats Retrospect backup reports in a
easier to read format. I then wrote an Applescript to pass the
emailed reports from Mail.app to my perl script, and back to Mail.app
in a new email message. (Both scripts are at the bottom of this
message)
I have multiple problems with the script:
1. It doesn't always work! Sometimes it seems to work great. But
sometimes I never see the new mail message.
2. I think (but am not certain) that it causes problems with the
"System Event" server. If I enable my Mail.app rule that runs the
Applescript, eventually the Finder becomes very slow when navigating
through folders. Killing the "System Event" process and restarting
it manually seems to fix the Finder problem. (Note that the script
has a "tell System Events" block)
3. I have noted that in addition to not always displaying a new
mail message, the "set visible" part of the script doesn't always
work either. I'm messing with the visibility because when you create
a new mail message, Mail un-hides itself, and I'd like it to hide
itself again after it's done.
So I guess my questions are:
- Am I doing something stupid and/or wrong in the script? If so, what?
- Any idea why my script would cause problems with System Events?
Is there perhaps a race condition for the visibility of the
application? Maybe I'm trying to set the visible state to false
before the app is truly visible...
Thanks for any help you can provide. Scripts below.
----------------- BEGIN Applescript -------------------------
-- temporary file path
property retro_file : (((path to desktop folder) as text) & "retro
temp file")
-- path to perl script
property perl_script : "/Users/teridon/bin/format_retro_report.pl "
--property this_font : "Monaco"
--property font_size : 10
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
-- store the hidden status, because for some reason mail comes to
the front
-- when creating new mail messages
tell application "System Events"
set this_app to application process "Mail"
set vis_status to visible of this_app
end tell
tell application "Mail"
repeat with eachMessage in theMessages
-- check for attachments
set theText to content of eachMessage as text
try
close access file retro_file
end try
open for access file retro_file with write permission
set eof file retro_file to 0
write theText to file retro_file as string
close access file retro_file
--on error
--display dialog "could not open temp file"
--end try
-- Run script
-- get the POSIX path of the temp file
set posixPath_retro to quoted form of (get POSIX path of file
retro_file)
-- Run the Perl script on the temp file
set outtext to do shell script perl_script & posixPath_retro
-- Handle error
if outtext contains "ERROR: not a Retrospect backup report" then
set the alert_message to "Sorry, that is not a Retrospect backup
report"
display dialog alert_message buttons {"Darn"} default button 1
with icon stop
--do shell script "rm " & posixPath_retro
return
end if
-- remove the temporary file
do shell script "rm " & posixPath_retro
-- Output results in a new Mail message
set theSubject to (subject of eachMessage)
set outtext to theSubject & return & outtext
set new_message to make new outgoing message at end of outgoing
messages with properties {content:outtext, visible:true} --,
subject:theSubject}
end repeat
end tell
-- Hide Mail.app if needed
tell application "System Events"
if vis_status is false then
if visible of this_app is true then
set visible of this_app to false
end if
end if
end tell
end perform mail action with messages
end using terms from
------------------------------ END Applescript, BEGIN
format_retro_report.pl ----------
#!/usr/bin/perl -w
$first_line = <>;
if (not $first_line =~ /Retrospect/) {
print "ERROR: not a Retrospect backup report\n";
exit;
}
printf("%-35s %8s s %5s\n","Volume Name","#
files","Size","Errors");
while (<>) {
if (/^Date:/ ) {print;next;}
if (/^Subject:/ ) {next;}
if (/^From:/ ) {next;}
if (/^To:/ ) {next;}
if (/^Volume/) {
$line = $_;
$start_index = index($line,"\"");
$end_index = index($line, "\"", $start_index+1);
#print "$start_index $end_index\n";
$volume = substr($line,$start_index+1, $end_index-$start_index -1);
$volume =~ s/Backup Clients://;
$volume =~ s/ Local Desktop://;
$volume =~ s/SOHO Database://;
$volume =~ s/\xd5//;
if (/completed successfully/) {
$num_files = $size = $errors = $line;
$num_files =~ s/(.*copying)(.*)(files.*)/$2/; chomp $num_files;
$size =~ s/(.*for)(.*)(K.*)/$2/; chomp $size;
$total_size += $size;
$errors =~ s/(.*K with )(.*)( errors)/$2/; chomp $errors;
printf("%-35s d K ]\n",$volume, $num_files, $size,
$errors);
} else {
$error = substr ( $line, index($line,"error") );
printf("%-35s %s", $volume, $error);
}
}
if (/destination/) {
$destination = $_;
$destination =~ s/^(.*".*"destination )(".*")\./$2/;
}
}
$megs = int($total_size / 1024);
print "\nTotal size of backup $total_size K, $megs M\n";
---------------------- END Perl script ---------------------------
Example report:
Report generated by Retrospect, Copyright 2003 Dantz Development
Corporation.
----------------------
From Backup Server:
Volume "Backup Clients:SOHO Gary:SOHO Gary" completed successfully,
copying 338 files for 41001 K with 0 errors.
Script "Backup Server Macs" finished copying volume "Backup
Clients:SOHO Gary:SOHO Gary" to destination "Office Computers [7341]".
Duration: 6 minutes 47 seconds.
----------------------
Volume "Backup Clients:OpsSuper:OPSSUPER (C:)" completed
successfully, copying 197 files for 6012 K with 2 errors.
Script "Backup Server PCs" finished copying volume "Backup
Clients:OpsSuper:OPSSUPER (C:)" to destination "Office Computers
[7341]".
Duration: 1 minute 26 seconds.
----------------------
Volume "Backup Clients:sohowin2k:SOHOWIN2K (C:)" completed
successfully, copying 15 files for 1477 K with 3 errors.
Script "Backup Server PCs" finished copying volume "Backup
Clients:sohowin2k:SOHOWIN2K (C:)" to destination "Office Computers
[7341]".
Duration: 1 minute 4 seconds.
----------------------
Volume "Backup Clients:SOHODPS:SOHODPS (C:)" completed successfully,
copying 232 files for 142357 K with 7 errors.
Script "Backup Server PCs" finished copying volume "Backup
Clients:SOHODPS:SOHODPS (C:)" to destination "Office Computers [7341]".
Duration: 11 minutes 23 seconds.
----------------------
Volume "Backup Clients:SH4-PC:SH4-PC (C:)" completed successfully,
copying 1050 files for 73569 K with 3 errors.
Script "Backup Server PCs" finished copying volume "Backup
Clients:SH4-PC:SH4-PC (C:)" to destination "Office Computers [7341]".
Duration: 6 minutes 29 seconds.
----------------------
Volume "Backup Clients:sh5-pc:SH5-PC (C:)" completed successfully,
copying 78 files for 10737 K with 0 errors.
Script "Backup Server PCs" finished copying volume "Backup
Clients:sh5-pc:SH5-PC (C:)" to destination "Office Computers [7341]".
Duration: 1 minute 38 seconds.
----------------------
Volume "Backup Clients:SOHOET2:sohoet2 (C:)" completed successfully,
copying 1563 files for 84188 K with 18 errors.
Script "Backup Server PCs" finished copying volume "Backup
Clients:SOHOET2:sohoet2 (C:)" to destination "Office Computers [7341]".
Duration: 8 minutes 24 seconds.
----------------------
Volume "Backup Clients:SOHOSCHED:SOHOSCHED (C:)" completed
successfully, copying 612 files for 285764 K with 3 errors.
Script "Backup Server PCs" finished copying volume "Backup
Clients:SOHOSCHED:SOHOSCHED (C:)" to destination "Office Computers
[7341]".
Duration: 17 minutes 53 seconds.
----------------------
Volume " Local Desktop:UserData:Team Meeting Audio" completed
successfully, copying 0 files for 0 K with 0 errors.
Script "Team Meeting Audio" finished copying volume " Local
Desktop:UserData:Team Meeting Audio" to destination "User Data [7341]".
Duration: zero seconds.
----------------------
Volume "Backup Clients:GSE-G4:GSE G4" completed successfully, copying
585 files for 886652 K with 0 errors.
Script "Backup Server Macs" finished copying volume "Backup
Clients:GSE-G4:GSE G4" to destination "Office Computers [7341]".
Duration: 1 hour 2 minutes 25 seconds.
----------------------
Volume "Backup Clients:GSE-G4:GSE G4" completed successfully, copying
678 files for 2573347 K with 0 errors.
Script "Backup GSE-G4" finished copying volume "Backup Clients:GSE-
G4:GSE G4" to destination "Office Computers [7341]".
Duration: 2 hours 42 minutes 23 seconds.
----------------------
Volume "Backup Clients:SOHO String4 iMAC:Sh4 iMac" completed
successfully, copying 213 files for 25654 K with 0 errors.
Script "Backup Server Macs" finished copying volume "Backup
Clients:SOHO String4 iMAC:Sh4 iMac" to destination "Office Computers
[7341]".
Duration: 7 minutes 12 seconds.
----------------------
Volume "Backup Clients:Kelly Miller's Computer:Kelly" completed
successfully, copying 161 files for 108947 K with 1 errors.
Script "Backup Server Macs" finished copying volume "Backup
Clients:Kelly Miller's Computer:Kelly" to destination "Office
Computers [7341]".
Duration: 12 minutes 49 seconds.
----------------------
Retrospect Backup Server backed up 13 volume(s) in the last 12 hours.
Backup Server reported 7 errors.
--- Example output of script:
Volume Name # files Size Errors
SOHO Gary:SOHO Gary 338 41001 K 0
OpsSuper:OPSSUPER (C:) 197 6012 K 2
sohowin2k:SOHOWIN2K (C:) 15 1477 K 3
SOHODPS:SOHODPS (C:) 232 142357 K 7
SH4-PC:SH4-PC (C:) 1050 73569 K 3
sh5-pc:SH5-PC (C:) 78 10737 K 0
SOHOET2:sohoet2 (C:) 1563 84188 K 18
SOHOSCHED:SOHOSCHED (C:) 612 285764 K 3
UserData:Team Meeting Audio 0 0 K 0
GSE-G4:GSE G4 585 886652 K 0
GSE-G4:GSE G4 678 2573347 K 0
SOHO String4 iMAC:Sh4 iMac 213 25654 K 0
Kelly Miller's Computer:Kelly 161 108947 K 1
Total size of backup 4239705 K, 4140 M
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Robert Jacobson email@hidden
BS, Aeronautical Engineering Univ. of Md., College Park
Flight Ops. Team - SOlar Heliospheric Observatory (SOHO)
(301) 286-1591
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden