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

10.5: Set Time Machine disk via AppleScript System 10.5
Like many users of Time Machine, I have a need to back up to different disks at different times. For instance, when my laptop is at home I want to back it up to an external drive for data security, but when I am on the road, I'd like to have it back up to a partition on the internal drive so I can use it as a way to "undo" file changes and quickly access deleted files.

This normally requires manually setting the Time Machine preferences every time you want to switch drives. So I put together an AppleScript to do this, and launch it automatically when my external drive is mounted or unmounted (using the free utility Do Something When).

The script uses GUI Scripting to set Time Machine to backup to a "primary drive" (the primarydrivename variable in the script) if it is available, and if it isn't, it will attempt to have it backup to a "secondary drive" (secondarydrivename). Failing that, it leaves the Time Machine preferences unchanged.

[robg adds: I haven't tested this one.]
    •    
  • Currently 1.89 / 5
  You rated: 3 / 5 (9 votes cast)
 
[26,253 views]  

10.5: Set Time Machine disk via AppleScript | 15 comments | Create New Account
Click here to return to the '10.5: Set Time Machine disk via AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.5: Set Time Machine disk via AppleScript
Authored by: cebailey on Jan 09, '08 11:00:07AM

Awesome, I have been wanting a way to do this, but I never bothered to figure out the necessary GUI scripting behind it. Question, though: do the backups work correctly? For example, if I'm at work all day backing up to my drive at work, and then I come home and back up at home, will it still back up all 8 (or 10) hours' worth of files that have changed since I last backed up TO THAT SPECIFIC DRIVE? or does it only back up the files that have been changed in the past hour?



[ Reply to This | # ]
10.5: Set Time Machine disk via AppleScript
Authored by: pbeyersdorf on Jan 09, '08 01:59:03PM

Time machine stores the state of the file system the last time it backed up on the backup drive itself, so it will update all the files that were changed since it backed up to that drive, thus both drives will be complete backups, they will just have snap shots that were recorded at different times. I've tested this. Nevertheless, it's not a bad idea to check that it has the behavior you expect before relying on it.

I suspect there may be an easier way to script this by changing a field in a plist somewhere rather than using the GUI to change the settings, but I didn't want to try to muck around with something I didn't fully understand, so by using the GUI scripting, the effect it has on time machine is transparent - it does the same thing as if you clicked through the GUI to change the settings.



[ Reply to This | # ]
10.5: Set Time Machine disk via AppleScript. problem: language (German)
Authored by: gertw on Jan 22, '08 12:49:14AM

awesome script, works beautifully but it does not work, e.g., if your main language is German, as in my case.
Anyone out there who can assist to "translate" the script into German?

I have figured it out until the step where the actual drive is being automatically selected (thanks to Peter for his suggestion to use UI Browser), at which it gets stuck...
I think the problem is in the script from below here:
set tablecontents to (rows of table 1 of scroll area 1 of sheet 1 of window "Time Machine")

However, I have no experiences in scripting and can't figure out what's wrong.
Help appreciated.



[ Reply to This | # ]
10.5: Set Time Machine disk via AppleScript. problem: language (German)
Authored by: carsten_s on May 30, '08 03:51:22PM

This looks interesting but I wonder if there isn't an easier way of changing the disk drive to backup to via a command line in terminal instead of letting the script "klick" on special buttons on special windows. I guess this script would have to be rewritten each time the UI for Time Machine changes.

So any ideas which parameter to change for setting the backup drive for Time Machine?



[ Reply to This | # ]
10.5: Set Time Machine disk via AppleScript. problem: language (German)
Authored by: ronnyHH on Aug 15, '08 11:09:16PM

I have some experience doing this. Is this problem still unsolved? If so, give me me what you have so far. I got somehow the same thing to do with AS and don´t want to start from scratch.

Ronald



[ Reply to This | # ]
10.5: Set Time Machine disk via AppleScript. problem: language (German)
Authored by: tmaeder on Aug 16, '08 01:39:59PM
I have changed the script, which now should work in English, French, German, Italian and Spanish. It finds out the language from the localised name of the System Preferences application, and (sorry for the imperfect translations!) attempts to work in that language.
Also, it now allows for a preferred backup volume (1st in list) and an arbitrary list of allowed backup volumes (no defined priority), and exits gracefully from System Preferences in case of failure by "clicking" on the Cancel button.
Try it out - and have fun! T.M.






(* Original source : http://www.macosxhints.com/article.php?story=20080105135511764 *)


(* This is a script to automatically choose which volume to have time machine back up to. If the primary volume (= 1st in the list) is available, it will be chosen, otherwise it will choose any volume available in the list. If none is available, it will make no changes to the time machine preferences.

To use, set the names of the volumes here : *)

set backup_drives to {"preferred", "backup2", "backup3"}

(*
Then launch the script whenever you want to change the time machine backup. The excellent utility DSW (http://www.azarhi.com/) can be used to launch the script whenever a volume is mounted or unmounted.

The script attempts to use growl to notify the user of its results.

Note that GUI scripting must be enabled
*)


(* User interface elements required for UI scripting
Élments de l'interface utilisateur ncessaire à son pilotage
Für die Steuerung nötige Elementen der Benützeroberfläche
Elementi della interfaccia utente necessari per pilotare l'applicazione
Elementos de la interfacia necesarios para pilotar l'aplicacion *)

(* Lists / listes / Listen / liste / listas : 1) english, 2) français, 3) deutsch, 4) italiano 5) español *)

(* Application (EN)
Application (FR)
Applikation (DE)
Applicazione (IT)
Aplicacion (ES) *)

property names_prefs : {"System Preferences",
"Prfrences Système",
"Systemeinstellungen",
"Preferenze di Sistema",
"Preferencias del Sistema"}


(* Time machine *)

property names_tm : {"Time Machine",
"Time Machine",
"Time Machine",
"Time Machine",
"Time Machine"}


(* Preference pane list menu (EN)
Menu des panneaux de prfrences (FR)
Menu der Einstellungenfenster (DE)
Menu dei paneli di preferenze (IT)
Menu de las panelas da preferencias (ES) *)

property names_view : {"View",
"Prsentation",
"Einstellungen",
"Vista",
"Visualización"}


(* Disc selection button (EN)
Bouton de slection du disque (FR)
Speicherauswahltaste (DE)
Bottone di schelta del disco (IT)
Botón di seleccion del disco (ES) *)

property names_usebutton : {"Use for Backup",
"Slectionner",
"Für backup verwenden",
"Utilizza per backup",
"Usar para copia de seguridad"}


(* Cancel button (EN)
Bouton d'annulation (FR)
Abbrechtaste (DE)
Bottone di annulatione (IT)
Botón di cancelatión (ES) *)

property names_cancelbutton : {"Cancel",
"Annuler",
"Abbrechen",
"Annula",
"Cancelar"}


(* Quit command (EN)
Commande pour quitter (FR)
Beenden-Befehl (DE)
Istruzione per escire (IT)
Comando per salire (ES) *)

property names_quit : {"Quit System Preferences",
"Quitter Prfrences Système",
"Systemeinstellungen Beenden",
"Esci da Preferenze di Sistema",
"Salir de Preferencias del Sistema"}


(* General notification (EN)
Notification gnrale (FR)
Allgemeine Meldung (DE)
Notificazione generale (IT)
Notificación general (ES) *)

property msgs_general : {"General notification",
"Notification gnrale",
"Allgemeine Meldung",
"Notificazione generale",
"Notificación general"}


(* Error notification (EN)
Notification d'erreur (FR)
Fehlermeldung (DE)
Notificazione di errore (IT)
Notificación de error (ES) *)

property msgs_error : {"ERROR",
"ERREUR",
"FEHLER",
"ERRORE",
"ERROR"}


(* Success message (EN)
Message de succès (FR)
Erfolgsmeldung (DE)
Messagio di riuscita (IT)
Mensaje de xito (ES) *)

property msgs_newvolume : {"The Time Machine backup volume is now ",
"Le volume de sauvegarde de Time Machine est maintenant ",
"Der Zielspeicher für Time Machine ist nun ",
"Il disco di backup per Time Machine ora ",
"El disco de copia de seguridad de Time Machine es ahora "}


(* Error message (EN)
Message d'erreur (FR)
Fehlermeldung (DE)
Messagio di errore (IT)
Mensaje de error (ES) *)

property msgs_notchanged : {"Time Machine preferences were not changed",
"Les prfrences de Time Machine n'ont pas t changes",
"Die Einstellungen von Time Machine wurden nicht geändert",
"Le preferenze di Time Machine non sono venuti cambiate",
"Las preferencias de Time Machine no han sido cambiadas"}


(* Language not found message *)

property msg_language : "ERROR - language not found" & return &
"ERREUR - langue non trouve" & return &
"FEHLER - Sprache nicht gefunden" & return &
"ERRORE - lingua non trovata" & return &
"ERROR - lingua no trovada"


property growl_general : "General Notification" -- Growl notification types
property growl_debug : "Debug Notification"
property growl_error : "Error Notification"


register_growl() -- register with Growl notification system



activate application "System Preferences" -- activate or launch preferences


tell application "System Events" -- get the localized_name of the System Preferences application
set loc_name to displayed name of process named "System Preferences"
end tell

global lg -- allow language index to be accessed from subroutines

set lg to 0 -- find out the language used by system preferences
repeat with i from 1 to count names_prefs
if item i of names_prefs = loc_name then
set lg to i
exit repeat
end if
end repeat

if lg = 0 then -- language not found - exit with error
growlnote(growl_error, item 1 of msgs_error, msg_language)
else

set name_tm to item lg of names_tm -- get name of Time Machine because it is used quite often

tell application "System Events"
tell process "System Preferences"
click menu item name_tm of menu 1 of menu bar item (item lg of names_view) of menu bar 1 -- directly activate Time Machine panel from menu

repeat until exists window name_tm
delay 0.2
end repeat
click button 1 of group 1 of window name_tm -- change disk button

(* Choose 1st available disk *)
repeat until exists sheet 1 of window name_tm -- wait till disk change dialog comes up
delay 0.2
end repeat

set tablecontents to (rows of table 1 of scroll area 1 of sheet 1 of window name_tm) -- traverse disk list
set rownumber to 0
set desiredrownumber to 0 -- reset row number (in volume list) of desired volume
set desired_name to "" -- reset desired volume name
set preferred_name to item 1 of backup_drives -- get name of the preferred / primary backup volume = 1st in the list
repeat with tablerow in tablecontents
set rownumber to rownumber + 1
set volumename to (value of static text of row rownumber of table 1 of scroll area 1 of sheet 1 of window name_tm) as string
log volumename
if (volumename as string) is in backup_drives then -- found a volume in the list
set desiredrownumber to rownumber -- set the desired row number in volume list
set desired_name to volumename -- set the desired volume name
if desired_name = preferred_name then exit repeat -- keep this one if this is the primary volume
end if
end repeat
if desiredrownumber > 0 then
try
select row desiredrownumber of table 1 of scroll area 1 of sheet 1 of window name_tm -- select the desired volume in list
click button (item lg of names_usebutton) of sheet 1 of window name_tm -- click the localised "use" button
set message_title to (item lg of msgs_general) -- tell user that a backup volume has been successfully selected
set message to (item lg of msgs_newvolume) & desired_name
my growlnote(growl_general, message_title, message)

on error
set desiredrownumber to 0 -- if failure for some reason, reset row number to 0 = nothing
end try
end if

if desiredrownumber = 0 then -- error message if unsuccessful
try -- try to cancel
click button (item lg of names_cancelbutton) of sheet 1 of window name_tm -- click the localised "Cancel" button
end try
set message_title to (item lg of msgs_error) -- not found : tell user that the preferences weren't changed
set message to item lg of msgs_notchanged
my growlnote(growl_error, message_title, message)
end if

delay 1 -- wait for a confirmation dialog that appears if you select a partition on the same physical disk as that being backed up
if (exists button (item lg of names_usebutton) of window 1) then
click button (item lg of names_usebutton) of window 1
end if


click menu item (item lg of names_quit) of menu 1 of menu bar item (item lg of names_prefs) of menu bar 1 -- quit preferences
end tell
end tell
end if




(* Try to register script with Growl *)

on register_growl()
try
tell application "GrowlHelperApp"
set the allNotificationsList to {growl_general, growl_debug, growl_error} -- Growl notifications
set the enabledNotificationsList to {growl_general, growl, debug, growl_error} -- Those enabled for this app
register as application "Configure TimeMachine" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "TimeMachine"
end tell
end try
end register_growl

on growlnote(growltype, msg_title, msg_text)
try
tell application "GrowlHelperApp"
notify with name growltype title msg_title description msg_text application name "Configure TimeMachine"
end tell
end try
end growlnote

[ Reply to This | # ]
Run with administrative privileges
Authored by: denydias on Sep 11, '08 07:56:11PM

I sucessfully managed to include Brazillian portuguese translation to the script sent by tmaeder (tks!). Now I facing another issue.

I log on to my OS X system using a non privileged user, a normal, non administrator one. So, to change any system level preference I must authenticate with a system administrator account when I click on the locker icon in any protected preference panel (Security, Network, Sharing, Time Machine and others).

The AppleScript from the tip work like a charm if I previously unlcok the locker icon with my admin account. But to do that, I have to open System Preferences, open Time Machine panel and unlock. This makes the script useless as I want it to be automatic. So I ask:

1 - There is way so I can make the AppleScript to show a dialog where I can enter just enter my admin user/password so it keep doing its job?
-- OR --
2 - There is a way to make the AppleScript to set the privilege prior it calls System Events application?

For debug purposes, when I run the script prior to run and unlock Time Machine panel first, it enter on a loop when Time Machine ask for admin/password in background, but don't show up the dialog itself. It says forever, until I stop it mannually:

<code>
exists sheet 1 of window "Time Machine" of process "System Preferences"
false
exists sheet 1 of window "Time Machine" of process "System Preferences"
false
exists sheet 1 of window "Time Machine" of process "System Preferences"
false
exists sheet 1 of window "Time Machine" of process "System Preferences"
false
...
</code>

Best for all,



[ Reply to This | # ]
[SOLVED] Run with administrative privileges
Authored by: denydias on Sep 25, '08 01:20:35PM
Hi,

Asking to myself. :) Here's how I did it.

[code]
(* Original source : [http://www.macosxhints.com/comment.php?mode=display&format=threaded&order=ASC&pid=101884] *)


(* This is a script to automatically choose which volume to have time machine back up to. If the primary volume (= 1st in the list) is available, it will be chosen, otherwise it will choose any volume available in the list. If none is available, it will make no changes to the time machine preferences.
To use, set the names of the volumes here : *)

set backup_drives to {"preferred", "backup2", "backup3"}

(* Then launch the script whenever you want to change the time machine backup. The excellent utility DSW (http://www.azarhi.com/) can be used to launch the script whenever a volume is mounted or unmounted.
The script attempts to use growl to notify the user of its results.
Note that GUI scripting must be enabled *)

(* User interface elements required for UI scripting *)

(* Lists / listes / Listen / liste / listas / listas : 1) english, 2) français, 3) deutsch, 4) italiano, 5) español, 6) português do Brasil *)

(* Application (EN)
Application (FR)
Applikation (DE)
Applicazione (IT)
Aplicacion (ES)
Aplicação (PT_BR) *)

property names_prefs : {"System Preferences",
"Prfrences Système",
"Systemeinstellungen",
"Preferenze di Sistema",
"Preferencias del Sistema",
"Preferências do Sistema"}

(* Time machine *)

property names_tm : {"Time Machine",
"Time Machine",
"Time Machine",
"Time Machine",
"Time Machine",
"Time Machine"}

(* Preference pane list menu (EN)
Menu des panneaux de prfrences (FR)
Menu der Einstellungenfenster (DE)
Menu dei paneli di preferenze (IT)
Menu de las panelas da preferencias (ES)
Menu das Preferências do Sistema (PT_BR) *)

property names_view : {"View",
"Prsentation",
"Einstellungen",
"Vista",
"Visualización",
"Visualizar"}

(* Click the lock to make changes. (EN)
Pour modifier, cliquez sur le cadenas. (FR)
Klicken Sie auf das Schloss, um Änderungen vorzunehmen. (DE)
Modifiche disabilitate. (IT)
Haga clic aquí para realizar cambios. (ES)
Clique no cadeado para efetuar alterações. (PT_BR) *)

property names_lock : {"Click the lock to make changes.",
"Pour modifier, cliquez sur le cadenas.",
"Klicken Sie auf das Schloss, um Änderungen vorzunehmen.",
"Modifiche disabilitate.",
"Haga clic aquí para realizar cambios.",
"Clique no cadeado para efetuar alterações."}

(* Disc selection button (EN)
Bouton de slection du disque (FR)
Speicherauswahltaste (DE)
Bottone di schelta del disco (IT)
Botón di seleccion del disco (ES)
Botão de seleçõ do disco (PT_BR) *)

property names_usebutton : {"Use for Backup",
"Slectionner",
"Für backup verwenden",
"Utilizza per backup",
"Usar para copia de seguridad",
"Usar para Backup"}

(* Cancel button (EN)
Bouton d'annulation (FR)
Abbrechtaste (DE)
Bottone di annulatione (IT)
Botón di cancelatión (ES)
Botão cancelar (PT_BR) *)

property names_cancelbutton : {"Cancel",
"Annuler",
"Abbrechen",
"Annula",
"Cancelar",
"Cancelar"}

(* Quit command (EN)
Commande pour quitter (FR)
Beenden-Befehl (DE)
Istruzione per escire (IT)
Comando per salire (ES)
Comando Encerrar (PT_BR) *)

property names_quit : {"Quit System Preferences",
"Quitter Prfrences Système",
"Systemeinstellungen Beenden",
"Esci da Preferenze di Sistema",
"Salir de Preferencias del Sistema",
"Encerrar Preferências do Sistema"}

(* General notification (EN)
Notification gnrale (FR)
Allgemeine Meldung (DE)
Notificazione generale (IT)
Notificación general (ES)
Notificação geral (PT_BR) *)

property msgs_general : {"General notification",
"Notification gnrale",
"Allgemeine Meldung",
"Notificazione generale",
"Notificación general",
"Notificação geral"}

(* Error notification (EN)
Notification d'erreur (FR)
Fehlermeldung (DE)
Notificazione di errore (IT)
Notificación de error (ES)
Notificação de Erro *)

property msgs_error : {"ERROR",
"ERREUR",
"FEHLER",
"ERRORE",
"ERROR",
"ERRO"}

(* Success message (EN)




Message de succès (FR)
Erfolgsmeldung (DE)
Messagio di riuscita (IT)
Mensaje de xito (ES)
Mensagem de sucesso (PT_BR) *)

property msgs_newvolume : {"The Time Machine backup volume is now ",
"Le volume de sauvegarde de Time Machine est maintenant ",
"Der Zielspeicher für Time Machine ist nun ",
"Il disco di backup per Time Machine ora ",
"El disco de copia de seguridad de Time Machine es ahora ",
"O volume de backup ativo "}

(* Error message (EN)
Message d'erreur (FR)
Fehlermeldung (DE)
Messagio di errore (IT)
Mensaje de error (ES)
Mensagem de erro (PT_BR) *)

property msgs_notchanged : {"Time Machine preferences were not changed",
"Les prfrences de Time Machine n'ont pas t changes",
"Die Einstellungen von Time Machine wurden nicht geändert",
"Le preferenze di Time Machine non sono venuti cambiate",
"Las preferencias de Time Machine no han sido cambiadas",
"As preferências do Time Machine não foram alteradas"}

(* Language not found message *)

property msg_language : "ERROR - language not found" & return &
"ERREUR - langue non trouve" & return &
"FEHLER - Sprache nicht gefunden" & return &
"ERRORE - lingua non trovata" & return &
"ERROR - lingua no trovada" & return &
"ERRO - Idioma não encontrado"

property growl_general : "General Notification" -- Growl notification types
property growl_debug : "Debug Notification"
property growl_error : "Error Notification"

register_growl() -- register with Growl notification system

activate application "System Preferences" -- activate or launch preferences

tell application "System Events" -- get the localized_name of the System Preferences application
set loc_name to displayed name of process named "System Preferences"
end tell

global lg -- allow language index to be accessed from subroutines

set lg to 0 -- find out the language used by system preferences
repeat with i from 1 to count names_prefs
if item i of names_prefs = loc_name then
set lg to i
exit repeat
end if
end repeat

if lg = 0 then -- language not found - exit with error
growlnote(growl_error, item 1 of msgs_error, msg_language)
else

set name_tm to item lg of names_tm -- get name of Time Machine because it is used quite often

tell application "System Events"
tell process "System Preferences"

delay 1 -- give it a time to load and get ready (avoid errors)

click menu item name_tm of menu 1 of menu bar item (item lg of names_view) of menu bar 1 -- directly activate Time Machine panel from menu

repeat until exists window name_tm -- wait for Time Machine Preferences to load
delay 0.2
end repeat

(* Get credentials and authenticate to go *)
if button 4 of window name_tm exists then -- authenticate if needed
if title of button 4 of window name_tm is item lg of names_lock then -- authenticate if unauthorized

activate "System Events"
set credentials to {1, 2}
set item 1 of credentials to display dialog "Digite um nome de administrador." with icon (path to resource "FileVaultIcon.icns" in bundle ((path to library folder from system domain as string) & "CoreServices:CoreTypes.bundle") as alias) with title "Time To Go" default answer "" buttons {"Cancel", "OK"} default button 2 giving up after 60 -- !!!remember to translate "Digite um nome de administrador." (Type an administrator username.) to your language.
set item 2 of credentials to display dialog "Digite a senha do administrador." with icon (path to resource "FileVaultIcon.icns" in bundle ((path to library folder from system domain as string) & "CoreServices:CoreTypes.bundle") as alias) with title "Time To Go" default answer "" buttons {"Cancel", "OK"} default button 2 giving up after 60 with hidden answer -- !!!remember to translate "Digite a senha do administrador." (Type an administrator password.) to your language.

click button 4 of window name_tm -- open the locker

tell application "System Events" -- authentication
tell window 1 of process "SecurityAgent" -- call authentication agent
set value of text field 1 of scroll area 1 of group 1 to text returned of item 1 of credentials
set value of text field 2 of scroll area 1 of group 1 to text returned of item 2 of credentials
click button 2 of group 2
end tell
end tell

if sheet 1 of window 1 of process "SecurityAgent" of application "System Events" exists then -- check if authentication failed
click button 1 of sheet 1 of window 1 of process "SecurityAgent" of application "System Events"
click button 1 of group 2 of window 1 of process "SecurityAgent" of application "System Events"
tell application "System Preferences" to quit -- quit preferences
activate "System Events" -- focus dialog
display dialog "Autenticação falhou. Tente novamente." with icon stop buttons {"Cancel"} with title "Time To Go" default button 1 -- let the user know it failed !!!Remember to translate "Autenticação falhou. Tente novamente." (Authentication Failed. Try Again.) to your language.
end if
end if
end if

click button 1 of group 1 of window name_tm -- change disk button

(* Choose 1st available disk *)
repeat until exists sheet 1 of window name_tm -- wait till disk change dialog comes up
delay 0.2
end repeat

set tablecontents to (rows of table 1 of scroll area 1 of sheet 1 of window name_tm) -- traverse disk list
set rownumber to 0
set desiredrownumber to 0 -- reset row number (in volume list) of desired volume
set desired_name to "" -- reset desired volume name
set preferred_name to item 1 of backup_drives -- get name of the preferred / primary backup volume = 1st in the list
repeat with tablerow in tablecontents
set rownumber to rownumber + 1
set volumename to (value of static text of row rownumber of table 1 of scroll area 1 of sheet 1 of window name_tm) as string
log volumename
if (volumename as string) is in backup_drives then -- found a volume in the list
set desiredrownumber to rownumber -- set the desired row number in volume list
set desired_name to volumename -- set the desired volume name
if desired_name = preferred_name then exit repeat -- keep this one if this is the primary volume
end if
end repeat
if desiredrownumber > 0 then
try
select row desiredrownumber of table 1 of scroll area 1 of sheet 1 of window name_tm -- select the desired volume in list
click button (item lg of names_usebutton) of sheet 1 of window name_tm -- click the localised "use" button
set message_title to (item lg of msgs_general) -- tell user that a backup volume has been successfully selected
set message to (item lg of msgs_newvolume) & desired_name
my growlnote(growl_general, message_title, message)

on error
set desiredrownumber to 0 -- if failure for some reason, reset row number to 0 = nothing
end try
end if

if desiredrownumber = 0 then -- error message if unsuccessful
try -- try to cancel
click button (item lg of names_cancelbutton) of sheet 1 of window name_tm -- click the localised "Cancel" button
end try
set message_title to (item lg of msgs_error) -- not found : tell user that the preferences weren't changed
set message to item lg of msgs_notchanged
my growlnote(growl_error, message_title, message)
end if

delay 1 -- wait for a confirmation dialog that appears if you select a partition on the same physical disk as that being backed up
if (exists button (item lg of names_usebutton) of window 1) then
click button (item lg of names_usebutton) of window 1
end if

if title of button 4 of window name_tm is not item lg of names_lock then
click button 4 of window name_tm -- Close the locker
end if

delay 1 -- wait for the closed locker
end tell
end tell
tell application "System Preferences" to quit -- quit preferences
end if

(* Try to register script with Growl *)

on register_growl()
try
tell application "GrowlHelperApp"
set the allNotificationsList to {growl_general, growl_debug, growl_error} -- Growl notifications
set the enabledNotificationsList to {growl_general, growl, debug, growl_error} -- Those enabled for this app
register as application "Configure Time Machine" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "Time Machine"
end tell
end try
end register_growl

on growlnote(growltype, msg_title, msg_text)
try
tell application "GrowlHelperApp"
notify with name growltype title msg_title description msg_text application name "Configure Time Machine"
end tell
end try
end growlnote
[/code]

Some considerations:
1 - It will ask for admin user/pass credentials and store it in memory during runtime. This method is safer than simple set it in a script variable but less secure than call SecurityAgent directly to athenticate (which I don't know how to do using AppleScript).
2 - The click in the locker and authentication process is localized just as the original script by tmaeder (tks again, bro!).
3 - The dialogs I created to ask for user, password and to let the user know if authentication failed is not localized. You must edit the script to make it read right in your language. I provided english translation in the line comment as a base.
4 - Sometime the script fails to click in the System Preferences menu (it's not my fault, this happens since the original script). In this case, I recommend you to run it again. Set a shortcut to run it or set a QuickSilver's trigger (I'm using this one).
5 - If the authentication fail (wrong user/pass typed), the script will close System Preferences and exits gently. In this case, rerun it using the shortcut or trigger.

That's all folks. It's being really helpfull to me. I hope it could be the same for others out there.

Best,


[ Reply to This | # ]
10.6 Breaks this script
Authored by: see3d on Sep 05, '09 12:12:40PM

I have used a modified version of this script to allow me to do automatic nightly backups to a local and a network drive. However, with 10.6 it no longer works. The problem is that the volumename used to be set from the following line:

set volumename to (value of static text of row rownumber of table 1 of scroll area 1 of sheet 1 of window name_tm) as string

Now that line returns the size of the volume instead of the name of the volume. Not being even a little proficient in Applescript, I don't know what to change to make it work again.



[ Reply to This | # ]
10.5: Set Time Machine disk via AppleScript
Authored by: JLG on Sep 10, '09 08:53:29AM
Some of the UI parts have been renamed/reworked in 10.6, so this script requires a couple of minor tweaks to work in Snow Leopard.

First, on the two lines that read:

set volumename to value of static text of row rownumber of table 1 of scroll area 1 of sheet 1 of window "Time Machine"

and

set volumename to value of static text of row desiredrownumber of table 1 of scroll area 1 of sheet 1 of window "Time Machine"

You must change "static text" to "text field".

Second, on the lines that read

if volumename as string is primarydrivename then

and

else if volumename as string is secondarydrivename then

You must change "is" to "starts with".

These two changes should fix the script for 10.6.

[ Reply to This | # ]
10.5: Set Time Machine disk via AppleScript
Authored by: sbossinger on Dec 22, '09 12:18:42PM

I had a devil of a time getting this script working in my environment. Of course, part of that, I'm sure, is that this is the first AppleScript I've ever worked with.

What was happening to me was that the script was ending by canceling the screen where it chooses the buttons to "Use for Backup" or "Cancel". I traced this back to the script using the value "0" for the value of desiredrownumber.

So, I changed it to this:


(*  Altered the test to ensure the property desiredrownumber would trigger the script to run, even if the desiredrownumber was 0 (as it is in cases where the script encounters only one drive attached, and that drive is in the first position - position 0).
*)
			if desiredrownumber ≥ 0 then
				try
					select row desiredrownumber of table 1 of scroll area 1 of sheet 1 of window name_tm -- select the desired volume in list
					click button (item lg of names_usebutton) of sheet 1 of window name_tm -- click the localised "use" button
					set message_title to (item lg of msgs_general) -- tell user that a backup volume has been successfully selected
					set message to (item lg of msgs_newvolume) & desired_name
					my growlnote(growl_general, message_title, message)

(*   Changed the error setting to -1.
*)					
				on error
					set desiredrownumber to -1 -- if failure for some reason, reset row number to -1 = nothing
				end try
			end if



[ Reply to This | # ]
10.5: Set Time Machine disk via AppleScript
Authored by: Totoleheros on Mar 12, '10 12:50:52PM

Hi,

I'am trying to use this script to switch between my Time Capsule (Home) and a Time Machine hard drive attached by a firewire cable (Work). The problem is with my Time Capsule because it is password protected and when the script select the 'Time Machine', I am asked to enter the password and to Connect to the Time Capsule (and the script process is broken).

Now, with my very poor knowledge of Applescript, I have tried unsuccessfully to have the click done on the connect button (the password is automatically filled in because it is stored in my keychain). Can someone help me please? I'm sure that is must be a line of code only...

Many thanks in advance!!



[ Reply to This | # ]
10.5: Set Time Machine disk via AppleScript
Authored by: travis.lawrie on Jul 28, '10 01:23:45AM

After the line:
click button "Use for Backup" of sheet 1 of window "Time Machine"

I inserted the line:
keystroke return

Whilst this doesn't appear to actually do anything at first, it seems the computer waits about 10 seconds or so (I'm assuming for you to change the password or something) during which time you can click the "Connect" button manually, and then clicks it for you (as it virtually 'clicks' the return/enter key on your keyboard).

This then lets the script get on with what it was doing.

Note: I have written the above in Snow Leopard, and I have no idea if it works on Leopard.



[ Reply to This | # ]
10.5: Set Time Machine disk via AppleScript
Authored by: rogerC on Mar 23, '10 08:09:43AM

Very close to that which I wish. I feel I can not rely on one hard drive for my 'history' so looking to switch between 2 HD's say every day or maybe morning and afternoon. Now to see if that can be done.



[ Reply to This | # ]
10.5: Set Time Machine disk via AppleScript
Authored by: Totoleheros on Apr 11, '10 02:47:53AM

No one to help there?



[ Reply to This | # ]