Sync now (AppleScript)

Monday, May 24, 2004

property lasterrror : ""
property currentlog : ""
property logout : ""

on quitAllApps() --(appName)
   
   
set logout to true
   
   
tell application "System Events"
      
set appList to the name of every process
   
end tell
   
   
set importantApps to {"SEC Helper", "iCalAlarmScheduler", "Sync Now", "loginwindow", "MouseWorks Background", "MicrosoftKeyboardHelper", "TabletDriver", "Timbuktu Host", "Timbuktu Host Menu", "SystemUIServer", "StatusItem", "SecurityAgent", "System Events", "MirrorAgent", "Finder", "Dock", "qtimageserver", "AEServer", "iChatAgent", "LittleSnitchDaemon"}
   
set stallApps to {"BitTorrent"}
   
   
set importantApps to importantApps & stallApps as list
   
   
repeat with a in appList
      
if importantApps does not contain a then
         
feedback("Quitting " & a as string)
         
quitApp(appList, a)
      
end if
      
if stallApps contains a then
         
set logout to false
      
end if
   
end repeat
   
end quitAllApps


on quitApp(theapps, appName)
   
   
if theapps contains appName then
      
set appObj to application appName
      
tell application "Finder"
         
set pathstring to the path to appObj
      
end tell
      
try
         
tell application appName
            
quit
         
end tell
      
end try
      
      
delay 2
   
end if
end quitApp


on sync(remotemachineadr)
   
feedback("Sync to " & remotemachineadr as string)
   
set r to display dialog "Sync Applications?" buttons {"No", "Okay"} default button "No" with icon note giving up after 30
   
set flapps to (button returned of r) as string
   
   
   
set now to the current date
   
set logname to the year of now & "_" & the month of now & "_" & the day of now & ".log" as string
   
set logPath to "\"/Users/shared/RsyncLogs/" & logname & "\"" as string
   
   
set currentlog to logPath
   
   
set commandline to "/usr/bin/rsync -rltzu --stats --eahfs --delete --log-format='%o %f [%b]' "
   
set commandline to commandline & "--include-from=/Users/james/Library/SyncControl/excludelist.txt " as string
   
set commandline to commandline & "-e /usr/bin/ssh /Users/james james@" & remotemachineadr & ":/Users/" as string
   
   
do shell script "echo \"" & commandline & "\" > " & logPath
   
   
ignoring application responses
      
tell application "Monitor Sync"
         
activate
      
end tell
   
end ignoring
   
   
feedback("Starting...")
   
   
set commandline to commandline & " >> " & logPath as string
   
   
tell application "Finder"
      
set sd to the name of the startup disk
      
set maclogPath to sd & ":Users:shared:RsyncLogs:" & logname as string
   
end tell
   
   
   
set commandline2 to ""
   
   
if flapps is "Okay" then
      
set commandline2 to commandline2 & "/usr/bin/rsync -rltz --stats --eahfs --delete --log-format=\"%o %f [%b]\" "
      
set commandline2 to commandline2 & "--include-from=\"/Users/james/Library/SyncControl/excludelist.txt\" " as string
      
set commandline2 to commandline2 & "-e /usr/bin/ssh /Applications james@" & remotemachineadr & ":/" as string
      
      
do shell script "echo \"" & commandline2 & "\" >> " & logPath
      
      
set commandline2 to commandline2 & " >> " & logPath as string
      
      
set commandline to commandline2 & ";" & commandline
      
   
end if
   
   
   
   
set lasterrror to ""
   
   
try
      
do shell script commandline
   
on error errst
      
set lasterrror to errst
   
end try
   
   
return logname
   
end sync


on orientate_self()
   
   
   
   
tell application "IP Monitor for OS X"
      
set myIP to Get My IP
      
quit
   
end tell
   
   
if (MP Computer Name) is "wegman" then
      
set thisMachine to "wegman"
      
set remoteMachine to "Preston"
      
set target_ipadr to "james.spahr.org"
      --
check to see if my office machine is at home
      
if myIP is "192.168.3.3" then
         
set target_ipadr to "192.168.3.5"
      
end if
   
else
      
set thisMachine to "Preston"
      
set remoteMachine to "wegman"
      
set target_ipadr to "james.designframe.com"
      
   
end if
   
   
set is_orienated to true
   
   
set r to display dialog "Will Sync with:" default answer target_ipadr buttons {"Cancel", "Okay"} default button "Okay" with icon note giving up after 30
   
set target_ipadr to (text returned of r) as string
   
if button returned of r is "Cancel" then quit
   
   
return target_ipadr
   
end orientate_self

on wait_till_fin()
   
   
delay 30
   
   
repeat
      
      
set s to do shell script "ps -ax | grep /usr/bin/rsync"
      
set r to count of paragraphs of s
      
      
      
if r < 3 then
         
exit repeat
      
end if
      
      
      
set s to do shell script ("tail -n 3 " & currrentlog)
      
feedback(s)
      
      
delay 5
      
   
end repeat
   
end wait_till_fin

on send_email(logname)
   
   
feedback("Sending email")
   
   
tell application "Finder"
      
set sd to the name of the startup disk
      
set logPath to sd & ":Users:shared:RsyncLogs:" & logname as string
   
end tell
   
   
set s to lasterrror & return & return & "--" & return & (do shell script ("tail -n 12 \"/Users/shared/RsyncLogs/" & logname & "\"" as string)) as string
   
set subline to "Rsync Results " & the (current date) as string
   
   
send mail to {"james@spahr.org"} from "james@spahr.org" subject subline body s SMTP server ¬
      "mail.designframe.com"
attachments {alias logPath}
end send_email

on feedback(s)
   
tell application "SEC Helper"
      
show screen duration 6 message s
   
end tell
end feedback

on run
   
set oself to orientate_self()
   
   
quitAllApps()
   
feedback("Ready to sync")
   
set logname to sync(oself)
   
wait_till_fin()
   
send_email(logname)
   
   
delay 5
   
   
if logout then
      
tell application "System Events"
         
log out
      
end tell
   
end if
   
   
end run


Top | Made with Script Debugger 3.0