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

A hack to script system preferences System
Is there a way to script the system preferences? Specifically, I want to be able to have the monitor resolution change at login to suit each user's preference.
    •    
  • Currently 2.25 / 5
  You rated: 1 / 5 (4 votes cast)
 
[6,414 views]  

A hack to script system preferences | 2 comments | Create New Account
Click here to return to the 'A hack to script system preferences' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
RE: Scripting Preferences
Authored by: adamcoop on May 17, '01 04:25:04AM

I haven't tried this yet (I'm actually about try), so it's still in theory stage. Unix allows you to script things (like a batch file in DOS world), so you may be able to write one to edit your system preferences (.plist files), or at the very least, switch between a few existing ones. This shell should then be able to be run as a startup program...

...But it's just a theory. I'll let you know when I've got this up and running.



[ Reply to This | # ]
RE: Scripting Preferences
Authored by: adamcoop on May 17, '01 05:51:39AM

Okay... I've had a go add changing the system preferences via the Unix shell.

The one I'm using for an example is to change the time it takes for my computer to go to sleep. Because I use iTunes as an alarm clock (using cron), the computer needs to be awake during the night, so it can activate iTunes at the set time.
After I leave the house, however, i've set my mac to go into sleep mode.

This is a bit long and drawn out, but if you have experience with Terminal, it shouldn't be a problem. Although this example works with the Power preferences, it will work with any other preference - this is simply a matter of replacing the preference file with a pre-made one.

1. Firstly, go to your System Preferences, and make the settings you want.

2. Next, you have to find the preferences file. The one I'm dealing with, 'com.apple.PowerManagement.plist', is in /Library/Preferences. This is where you'll also find the monitor settings, 'com.apple.windowserver.plist'.

3. Make a copy of this file and give it a name you can recognize - my first one is called 'sleepmode.on'.

4. Repeat steps 2 to 4 as many times as needed (once for each configuration).

5. You'll need to set the permissions for the preferences file (com.apple.PowerManagement.plist, for example) to write for all, as follows.

chmod 666 com.apple.PowerManagement.plist

Okay... that's the first bit done.

6. Next, go to the directory where you want the script to be made. 'Applications' is a logical choice, as all users can access it.

7. Here we can create the new document. Type
pico newscript.sh
where 'newscript.sh' is the name of the script.

8. The following is what is in my script (thanks to robg for the ftp script - that's where i learnt.)


#! /bin/sh

echo Setting G4Forty to go to sleep in 5 minutes...
# Start of "here" document
rm /Library/Preferences/com.apple.PowerManagement.plist
cp /Library/Preferences/sleepmode.on /Library/Preferences/com.apple.PowerManagement.plist
# End of "here" document
echo Set up complete - G4Forty will sleep in 5 minutes..

The 'echo' line is a visual return to the user... lets them know what's happening. These can be omitted is you like.

The '# Start of "here"' line is what tells Terminal that is a script. After this line, you can insert the required Unix commands.
Firstly, the old preferences file is deleted, and is replaced with one of the presets we made earlier (in this case sleepmode.on).

#End of here denotes the end of the script.

Press ctrl + x to quit and save your script.

The final bit... sort of.

9. Once you've done that, you can quit out of terminal. In Finder, go to the Applications directory (or wherever you made the script) and get info on it.

10. Set the default application to Terminal.

11. You will have to set up a seperate script for each setting.... I have one for 'sleep on' and 'sleep off'.

And that's about it.

If you want, as in the case of the monitor preferences, to open it at startup, simply put the required script into your startup items (in the login preference). Each user has their own login preference, so you can put a different script in for each user.

You will also notice the terminal will be left open after completing the process, but you could write a simple applescript that goes something like:

Tell Application "Terminal"
quit
end Tell

which you would put in the startup items after your script. Or you could open the script within the applescript.... but that's another story.



[ Reply to This | # ]