Wednesday, February 6, 2008

My Mozyhome Automation Script:

As I mentioned in a previous post, I really like Mozyhome but I don't like having it constantly running in the background; it really only needs to run when it performs a backup, and any other time it shouldn't be wasting my RAM and CPU. So here's a handy Autohotkey script I've written that'll automate starting, running, and killing Mozyhome automatically.

Here's the full script, which I've called mozybackup.ahk:

#SingleInstance force
DetectHiddenText, On
SetTitleMatchMode 2
SetTitleMatchMode, Slow

run C:\Program Files\MozyHome\mozystat.exe

Sleep, 5000

run C:\Program Files\MozyHome\mozystat.exe

Sleep, 10000

WinWait, ahk_class wxWindowClassNR
IfWinNotActive, ahk_class wxWindowClassNR, , WinActivate, ahk_class wxWindowClassNR,
WinWaitActive, ahk_class wxWindowClassNR,
MouseClick, left, 347, 41

WinWait, ahk_class wxWindowClassNR, 1 minute ago
Run, C:\Program Files\PSTools\pskill.exe "mozybackup.exe"
Run, C:\Program Files\PSTools\pskill.exe "mozystat.exe"
Run, C:\Documents and Settings\Arvin\My Documents\scripts\finishedbackup.txt

Alright, now let's go through each part of the script to explain what it does, in case you need to alter parts of it later on (which I'm sure you'll have to).

#SingleInstance force
DetectHiddenText, On
SetTitleMatchMode 2
SetTitleMatchMode, Slow

This part kind of sets the tone for the rest of the script. The first line makes sure only one instance of the script is running. The next three lines sets Autohotkey to actively and thoroughly read the text inside currently open windows. You'll see why this is important later.
run C:\Program Files\MozyHome\mozystat.exe

Sleep, 5000

run C:\Program Files\MozyHome\mozystat.exe

Sleep, 10000
This part runs Mozyhome. If you installed Mozyhome into a different directory, change the file path as appropriate. The reason I call the program twice is because mozystat.exe usually starts minimized in the system tray, where I can't get at it. The second run command brings up the Mozyhome window to the top of the desktop. The long sleep times allow time for Mozyhome to properly load in plenty of time.

WinWait, ahk_class wxWindowClassNR
IfWinNotActive, ahk_class wxWindowClassNR, , WinActivate, ahk_class wxWindowClassNR,
WinWaitActive, ahk_class wxWindowClassNR,
MouseClick, left, 347, 41

This part waits for activates the Mozyhome window if it hadn't already (wxWindowClassNR is the ahk_class or window class of the Mozyhome window). The last line clicks the "Start Backup" button on the Mozyhome window, which starts the backup.
WinWait, ahk_class wxWindowClassNR, 1 minute ago
Run, C:\Program Files\PSTools\pskill.exe "mozybackup.exe"
Run, C:\Program Files\PSTools\pskill.exe "mozystat.exe"
Run, C:\Documents and Settings\Arvin\My Documents\scripts\finishedbackup.txt

The first line of this next section waits for the Mozyhome window to display the term "1 minute ago," which would show up as soon as the backup finishes. The script is paused at this point until that phrase in that window pops up.

Once it finishes, it runs PSKill to kill the mozybackup.exe and mozystat.exe processes. I'd featured PSKill before, but basically it force closes the specified processes. I've put pskill.exe into a folder called PSTools in my Program Files folder, but again, if you've put it elsewhere, make sure you put the right path in there.

Finally, the last line opens a text file called finishedbackup.txt that I'd prewritten and saved in my scripts folder that just simply says "Finished backup!" I just put it in there to let me know the backup finished successfully. You don't need to do this part, or customize it yourself with whatever text file you want. I decided against using a MsgBox because the script won't close unless the OK button on the MsgBox is clicked. With the text file, the script can open the file, and then close itself.

And there you go! To run this script, either just run it by double clicking whenever you want to, or incorporate it into another script and have it run from a specific keystroke. I have it run as part of my computer's shutdown routine, which I invoke with a simple ALT-H at the end of the night, or in the morning before I head out to work.

Related Posts by Categories



Widget by Hoctro | Jack Book

1 comment:

Tommy said...

Hi, thanks for the post. "MouseClick, left, 347, 41" probably won't work for everyone, so I suggest sending an Alt-S combo to start the backup instead (once the Mozy Status window is in focus).