Script to rename computer?

I think I’m missing something here…
I have written a script that open system preferences selects the sharing pane and sets the value of the computer name text field, but when i exit system prefs and go back in the computername has reverted to the original. I can change the hostname permanently but not the computername…
i’d rather not have to write a shell script to parse out and replace the value in the preference file…

Thanks


ryan loughlin
network asst.
portsmouth school dept.
rloughlin@portsmouth.k12.nh.us

I know you didn’t want to do plist parsing but I found the following via google. Maybe it will help you.

Hmm… changing the text field’s value should do the trick, but it doesn’t work here either, Ryan. You may therefore have to resort to a little more brute force to achieve what you’re attempting.

Try something like this:

set computerName to "required name"
set tabKey to tab
tell application "System Preferences" to set current pane to pane "com.apple.preferences.sharing"
tell application "System Events" to tell process "System Preferences"
	set frontmost to true
	tell window "Sharing"'s text field 1 to if value is not computerName then
		if focused then
			keystroke "a" using {command down}
		else
			repeat until focused
				keystroke tabKey
			end repeat
		end if
		keystroke computerName
	end if
end tell
tell application "System Preferences" to quit

Thanks, kai, for the useful lesson in GUI scripting. (I’m only just getting into it.) :slight_smile:

One minor issue I did find with your script is that not all characters are rendered correctly by the ‘keystroke’ command. For instance, when a computer’s first set up, the assistant usually gives it a default name based on the owner’s name - eg. “Fred Bloggs’s Computer” - with a “smart” apostrophe. When I try to reproduce this using your script in Tiger, the computer’s renamed “Fred Bloggsss Computer”. May I suggest the following variation?:

set computerName to "required name"
set tabKey to tab
tell application "System Preferences" to set current pane to pane "com.apple.preferences.sharing"
tell application "System Events" to tell process "System Preferences"
	set frontmost to true
	tell window "Sharing"'s text field 1 to if value is not computerName then
		repeat until focused
			keystroke tabKey
		end repeat
		-- Type anything into the field to flag a modification to the pane.
		keystroke "x"
		-- Then set the value actually required.
		set value to computerName
	end if
end tell
tell application "System Preferences" to quit

Very good point, Nigel. Your suggestion avoids a potentially messy sequence of keystrokes, only some of which might require keystroke modifiers (such as ‘option down’). :slight_smile:

To make the technique even more subtle, I’d probably go for a white space character to initially ‘register’ a change.

An added bonus is that your alternative is a few lines shorter. It could be abbreviated further by inserting a tab character, rather than attributing the AppleScript constant ‘tab’ to the variable ‘tabKey’. (And if anyone is wondering why we can’t just use the statement ‘keystroke tab’, see Gory Details, below.)

Anyway, to be specific:

set computerName to "Mac's iBook" (* or whatever *)
tell application "System Preferences" to set current pane to pane "com.apple.preferences.sharing"
tell application "System Events" to tell process "System Preferences"
	set frontmost to true
	tell window "Sharing"'s text field 1 to if value is not computerName then
		repeat until focused
			keystroke "	" (* when copying this script, make sure the quoted text here is a single tab character *)
		end repeat
		keystroke space
		set value to computerName
	end if
end tell
tell application "System Preferences" to quit

Interesting. :confused: ‘tab’ compiles and behaves perfectly well in a System Events ‘tell’ block on both my Jaguar and Tiger machines. I wonder if you’re experiencing something that’s unique to Panther. If it was a name-space clash with an OSAX - or perhaps with Smile - I wouldn’t expect System Events to make any difference.

There’s also “\t”, which compiles as a tab character in string form. :slight_smile:

Thanks for that clarification, Nigel.

It’s not down to third party OSAX or Smile, since I generally test without either. In that case, I suspect an early implementation of UI scripting in Panther - since there are one or two other minor syntactic and behavioural differences for which I also have to make allowances. (Only a couple more clients left to persuade and I should finally be able to upgrade!) :wink:

Of course (as does “\r” for the return character). Thanks for completing the options and compensating for my ailing memory. :wink:

kai and Nigel…
You guys rock!
that worked like a charm.
thanks so much. not only does that solve my problem but it’s a great lesson as well!

thanks

ryan

Thanks very much for this script. It runs like a charm on a 10.4 system. However when I tried using it on a system running 10.3.8 I receive the following error message:

System Eventg got an error: NSReceiverEvaluationScriptError: 4

When I click on OK it highlights computerName in the line:

tell window “Sharing”'s text field 1 to if value is not computerName then

I tried a variety of this such as changing variable name and putting in string value instead, nothing solves the issue.

Any help/suggestions are appreciated.

Model: G4
AppleScript: 1.9.3
Browser: Internet Explorer 5.23
Operating System: Mac OS X (10.3.8)