Search This Blog

Saturday, February 9, 2013

Disabling Time Machine via AppleScript (Update)

While my Carbonite AppleScript in the last post worked nicely there was an error with the Time Machine script.

The problem is that executing

defaults write /Library/Preferences/com.apple.TimeMachine AutoBackup -bool true

from or as part of a shell script requires you to be root.  Otherwise you get an error like this:

... defaults[6633:707] Could not write domain com.apple.TimeMachine; exiting

The fix is ugly.  Of course, even though you own your Mac or MacBook you have to have explicitly set things up to access the user 'root'.

Pretty much no matter what you do (upgrade a machine, buy a machine, etc.) root is disabled by default.  To enable root use these instructions here (make sure you remember and/or write down the password you choose).

Once root is enabled you have to make sure you can run a shell script as root.

Typically this is done via a command call sudo.

You start a terminal window and enter the command:

 sudo defaults write /Library/Preferences/com.apple.TimeMachine AutoBackup -bool true
Password: 

You then have to manually enter the root password.

Putting this into an AppleScript won't work because there won't be anyone or anything to type in the password.

However, there is a not-so-simple work around.

First, create two shell scripts like this:

TM_Off.csh:

defaults write /Library/Preferences/com.apple.TimeMachine AutoBackup -bool false

TM_On.csh:

defaults write /Library/Preferences/com.apple.TimeMachine AutoBackup -bool true

I put them into my Desktop folder: /Users/fred/Desktop as /Users/fred/Desktop/TM_Off.csh and
/Users/fred/Desktop/TM_On.csh.

I also performed the following on these files as 'root' though I am not sure it is necessary:

chmod 4555 TM_O*.csh
chown root TM_O*.csh

Next you have to (most likely) create a file in /etc call sudoers, i.e., /etc/sudoers.

So if you normally log in into your machine as "fred" then you have to create an entry in the /etc/sudoers file like this:

fred ALL = NOPASSWD: /Users/fred/Desktop/*

What this does is allows user 'fred' to run scripts via sudo from the folder /Users/fred/Desktop.

If you've set this all up right then as user fred you should be able to manually enter the TM_On.csh and TM_Off.csh commands into a shell windows, e.g., created by Terminal, and you should be able to check and see its working via the "System Preferences."

Then you can use the following AppleScripts to control Time Machine:

do shell script "sudo /Users/fred/Desktop/TM_Off.csh"

do shell script "sudo /Users/fred/Desktop/TM_Off.csh"


So there you have it - an AppleScript solution for controlling Time Machine.

No comments:

Post a Comment