Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Using Applescript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Using Applescript
Authored by: theDan on Mar 16, '07 10:38:37AM

I had a similar dillema, although in my situation it was a network file server who had a dedicated Backup drive.

I wrote a simple enough applescript that launches at login, following this logic:
(I use variables for the backup program's name and the server and the backup drive, and you can modify those on your own)

if backupapp is not running and backupdisk is not mounted
quit

if backupapp is running and backupdisk is not mounted
mount backupdisk

if backupapp is not running and backupdisk is mounted
unmount backupdisk

if bakupapp is running and backupdisk is mounted
check every 10 seconds if appname is running

With this script, I was able to set my backup to start at 7:00AM, and anytime i logged in after then, it would launch itself. This script, upon seeing backupapp running, but no disk mounted, will look in the variables for the server name and disk name, and then extract the username/password stored in your keychain and use that. I used this method so i didn't have to hardcode usernames and passwords into the script.

The first time it is run, it will ask you for permission to access your keychain, to use an item in your keychain, and once more on top of that. For the script to work, you must select allow once or allow always, otherwise the script is unable to access the login info for your server.

Here it is:
(I used Growl for notification, because dialog boxes are terrible, but if you do not have Growl, i recommend getting it.)
<code>
-- VERSION
set ScriptVersion to "v0.9"
set startDelay to 1
set sampleDelay to 5
-- SERVER VARIABLES
set AppName to "Application you use to backup"
set ServerName to "Name of Server, as it would appear in your keychain. Basically the Bonjour address without the .local"
set ServerURL to "afp://" & ServerName & ".local"
set VolumeName to "The name of the shared volume you wish to mount"
set VolumeURL to ServerURL & "/" & VolumeName
set UnmountCMD to "umount " & "/Volumes/" & VolumeName

-- ERROR MESSAGES
set UnmountError to "Volume could not be unmounted, Most likely, this is because files are still open.
Please quit any applications that may be accessing the volume
and click OK.

If you do not wish to unmount the volume right now,
click Cancel and unmount the volume manually later."

set KeychainError to "There was an error accessing your keychain for the login information. You may not have a keychain for this server.
Please mount the volume using the Finder and check the 'Remember Password' box in the login dialog to create a keychain."

set MountError to "The volume could not be mounted. Please verify that:" & "
The URL for the volume is " & VolumeURL & ".
The server and this computer are on the same local network, and that the server is on with AFP enabled."



-- FUNCTION
-- Function to check if App is running, returns count of every app named AppName, 0 if none
on getAppStatus(AppName)
tell application "System Events"
set AppRunning to count (every process whose name is equal to AppName)
return AppRunning
end tell
end getAppStatus

-- Function to see if volume VolumeName is mounted. Returns 0 for not mounted, 1 for is mounted.
on volumeMounted(VolumeName)
tell application "Finder"
if (exists disk VolumeName) then
return 1
else
return 0
end if
end tell
end volumeMounted

-- GROWL NOTIFY
tell application "GrowlHelperApp"
-- All notifications
set the allNotificationsList to ¬
{"Update", "Error"}
-- Enabled notifications
set the enabledNotificationsList to ¬
{"Update", "Error"}
-- Registration
register as application ¬
"Backup Network Mount" all notifications allNotificationsList ¬
default notifications enabledNotificationsList ¬
icon of application AppName
-- Startup Notification
notify with name "Update" title "Backup Script Loaded..." description "Backup Network Mount Script loaded." application name "Backup Network Mount"
end tell
on gnotify(gTitle, gMessage, gType)
tell application "GrowlHelperApp"
notify with name gType title gTitle description gMessage application name "Backup Network Mount" icon of application "Backup"
end tell
end gnotify

--SCRIPT
-- Wait xx seconds to allow time for Backup to launch
delay (delay)
-- If App is not running and the volume not mounted after this, send notifiction and exit
if volumeMounted(VolumeName) + getAppStatus(AppName) = 0 then
gnotify("Nothing to do", AppName & " is not running, and " & VolumeName & " is not mounted. Ending Script.", "Error")
else
-- If the app is running
if getAppStatus(AppName) > 0 then
-- If the volume is not mounted
if volumeMounted(VolumeName) = 0 then
gnotify(AppName & " Running...", "Mounting " & VolumeName, "Update")
try
gnotify("Getting Keychain Info", "Acessing the login information for " & ServerName, "Update")
tell application "Keychain Scripting"
set serverKey to first key of current keychain whose name is ServerName
set UserName to account of serverKey
set PWD to password of serverKey
end tell
on error
gnotify("Keychain Error...", "There was an error accessing the login information in your keychain.", "Error")
display dialog KeychainError
end try
-- log mounting info
gnotify("Attempting to Mount", "Connecting as " & UserName, "Update")
-- Mount the volume
try
mount volume VolumeURL as user name UserName with password PWD
gnotify("Connected Successfully...", "Volume " & VolumeName & " on " & ServerName & " is now mounted.", "Update")
on error
gnotify("Error Mounting " & VolumeName & "...", "There was an error mounting the network volume.", "Error.")
display dialog MountError buttons ["Quit"]
-- quit
end try
end if
end if
-- Check periodically if the app is running
gnotify("Waiting for Backup to Complete...", "Volume " & VolumeName & " will be unmounted once " & AppName & " quits.", "Update")
repeat while getAppStatus(AppName) > 0
tell application "Finder"
delay sampleDelay
end tell
end repeat
gnotify(AppName & " quit.", "Unmounting " & VolumeName & "...", "Update")
-- Once the app has quit, unmount the Volume if it is mounted
if volumeMounted(VolumeName) = 1 then
-- Warn User
gnotify("Unmounting...", "Unmounting " & VolumeName & " because " & AppName & " is no longer running.", "Update")
-- While Disk is mounted, try to unmount and display a error message.
repeat while volumeMounted(VolumeName) is 1
tell application "Finder"
try
do shell script UnmountCMD
on error
tell application "Finder"
activate
display dialog UnmountError
gnotify("Unmounting Failed...", "There was an error Unmounting " & VolumeName, "Error")
end tell
end try
end tell
end repeat
end if
end if
</code>

Here is a version for people who don't care about being notified, even if the script fails. This script is totally silent, not even beeps.

<code>
-- SERVER VARIABLES
set AppName to "Application you use to backup"
set ServerName to "Name of Server, as it would appear in your keychain. Basically the Bonjour address without the .local"
set ServerURL to "afp://" & ServerName & ".local"
set VolumeName to "The name of the shared volume you wish to mount"
set VolumeURL to ServerURL & "/" & VolumeName
set UnmountCMD to "umount " & "/Volumes/" & VolumeName

-- FUNCTION
-- Function to check if App is running, returns count of every app named AppName, 0 if none
on getAppStatus(AppName)
tell application "System Events"
set AppRunning to count (every process whose name is equal to AppName)
return AppRunning
end tell
end getAppStatus

-- Function to see if volume VolumeName is mounted. Returns 0 for not mounted, 1 for is mounted.
on volumeMounted(VolumeName)
tell application "Finder"
if (exists disk VolumeName) then
return 1
else
return 0
end if
end tell
end volumeMounted

-- SCRIPT
-- If the app is running
if getAppStatus(AppName) > 0 then
-- If the volume is not mounted, display a Dialog Box informing the user that the volume is being mounted
if volumeMounted(VolumeName) = 0 then
tell application "Finder"
try
tell application "Keychain Scripting"
set serverKey to first key of current keychain whose name is ServerName
set UserName to account of serverKey
set PWD to password of serverKey
end tell
end try
-- Mount the volume
try
mount volume VolumeURL as user name UserName with password PWD
on error
quit
end try
end tell
end if
end if
-- Check periodically if the app is running
repeat while getAppStatus(AppName) > 0
tell application "Finder"
delay 1
end tell
end repeat

-- Once the app has quit, unmount the Volume if it is mounted
if volumeMounted(VolumeName) = 1 then
-- While Disk is mounted, try to unmount and display a error message.
repeat while volumeMounted(VolumeName) is 1
tell application "Finder"
try
do shell script UnmountCMD
end try
end tell
end repeat
end if
</code>

Copy and paste the script you would like to use into Script Editor, insert your netwoork server information, and then save as an application bundle, without a startup screen.

Then, add it as a login item and you are ready to go!

May you be forever backed up.

-Dan



[ Reply to This | # ]