Go Back   The macosxhints Forums > Working with OS X > OS X Developer



Reply
 
Thread Tools Rating: Thread Rating: 3 votes, 5.00 average. Display Modes
Old 02-27-2003, 02:50 PM   #1
addertheblack
Prospect
 
Join Date: Sep 2002
Posts: 5
applescript "computer name" command problem OS X

I'm runnning OS 10.2.4 and I'm trying to get the applescript below to work


set moo to computer name
display dialog moo


it is supposed to pop up with a dialog that has the computer's name in it. It works fine in macOS 9 and classic but fails in macOS X... MacOS X script editor changes the program to


set moo to system attribute name
display dialog moo


Then fails giving some sort of UNIX environment variables in the error box. I am curious if this is a bug in MacOS X or some sort of bizzar intended behavior I can't follow...
addertheblack is offline   Reply With Quote
Old 02-27-2003, 03:59 PM   #2
at_sym
Triple-A Player
 
Join Date: Jan 2002
Posts: 179
Try using the do shell script functionality:

set moo to do shell script "grep APPLETALK_HOSTNAME /etc/hostconfig |
cut -d '=' -f 2"
display dialog moo
at_sym is offline   Reply With Quote
Old 03-03-2003, 01:40 PM   #3
addertheblack
Prospect
 
Join Date: Sep 2002
Posts: 5
Quote:
Originally posted by at_sym
Try using the do shell script functionality:

set moo to do shell script "grep APPLETALK_HOSTNAME /etc/hostconfig |
cut -d '=' -f 2"
display dialog moo

no, that doesn't work.. My hostconfig file doens't have my machine name in it...
addertheblack is offline   Reply With Quote
Old 03-04-2003, 11:24 AM   #4
xchanyazy
All Star
 
Join Date: Jan 2002
Location: Dexter, MI, USA
Posts: 704
If I do
Code:
set moo to do shell script "hostname"
display dialog moo
I get a dialog with my rendezvous name.

The other script works for me though, so this is possibly no help at all if your hostname is set to something different than what the UNIX part of your computer thinks it is (if that's possible)
__________________
- Greg

Happy user of OS X since the Public Beta.
Help Team Mac OS X cure cancer, Alzheimer's, ALS, Parkinson's, and more!
xchanyazy is offline   Reply With Quote
Old 03-04-2003, 01:23 PM   #5
at_sym
Triple-A Player
 
Join Date: Jan 2002
Posts: 179
Quote:
Originally posted by addertheblack
no, that doesn't work.. My hostconfig file doens't have my machine name in it...

Hmmm. That's worked on all the machines I've tried it on. (As does Greg's.) What result do you get when you run them?
at_sym is offline   Reply With Quote
Old 03-04-2003, 07:52 PM   #6
addertheblack
Prospect
 
Join Date: Sep 2002
Posts: 5
Lightbulb

Quote:
Originally posted by at_sym
Hmmm. That's worked on all the machines I've tried it on. (As does Greg's.) What result do you get when you run them?

I just get a bunch of numbers.. i think I may have worked out why, though.. If you put a none-standard char in it (like option g), the name gets set to a bunch of numbers...

There are other problems with greping the hostconfig file.. If you have a double quote, the double quote will be escaped (that is it will have a backslash in front)...

Actually, I'm quite impressed, the logic for figuring out what to put in the hostconfig file form the computer's name must be fairly complex.. !

Oddly enough, I think the default name generated when the user first uses their system has a smart single quote in it.. Which is asking for trouble....

Greping the hostconfig file is not a universal solution.

The hostname command trick works.... however it's not the computer's name, it's the rendezvous name...
addertheblack is offline   Reply With Quote
Old 03-04-2003, 08:23 PM   #7
xchanyazy
All Star
 
Join Date: Jan 2002
Location: Dexter, MI, USA
Posts: 704
This will work assuming you have no period in your computer name:

Code:
set moo to do shell script "hostname | awk -F. '{print $1}'"
display dialog moo
__________________
- Greg

Happy user of OS X since the Public Beta.
Help Team Mac OS X cure cancer, Alzheimer's, ALS, Parkinson's, and more!
xchanyazy is offline   Reply With Quote
Old 03-04-2003, 09:35 PM   #8
addertheblack
Prospect
 
Join Date: Sep 2002
Posts: 5
Quote:
Originally posted by xchanyazy
This will work assuming you have no period in your computer name:

Code:
set moo to do shell script "hostname | awk -F. '{print $1}'"
display dialog moo

Code:
set moo to do shell script "hostname -s"
display dialog moo
does something similar only without the the awk-ing...

of course it's still the rendezvous name, which can be varied independently fom the computer name, but anyways... At least it's not a large seemingly random number.

I wonder if I should bother with a bug report on how

Code:
display dialog computer name
is changed to

Code:
display dialog system attribute name
which does something totally different..
addertheblack is offline   Reply With Quote
Old 03-04-2003, 10:43 PM   #9
mervTormel
League Commissioner
 
Join Date: Jan 2002
Posts: 5,536
% man hostname

...

-s Trims off any domain information from the printed name.
mervTormel is offline   Reply With Quote
Old 03-07-2003, 04:46 PM   #10
greggo
Prospect
 
Join Date: Jul 2002
Posts: 14
The only way I know to return the computer name is using 'Extra Suites' from www.kanzu.com then you can run this script.
Code:
tell application "Extra Suites" to computer name of (system info)
Greg
MacScripter.net
greggo is offline   Reply With Quote
Old 12-20-2004, 03:01 PM   #11
tessera
Registered User
 
Join Date: Dec 2004
Posts: 1
shell command to pull computer name

The computer name is located in the file /Library/Preferences/SystemConfiguration/preferences.plist .

One shell command (can run in Terminal) to print the computer name is

grep -A1 'ComputerName<' /Library/Preferences/SystemConfiguration/preferences.plist | grep -v key | sed s/"[[:blank:]]*<\/*string>"//g

It has to be all on one line. This looks for the string "ComputerName<" and returns that line and the one after it, removes the line containing the word "key", and then strips off the leading whitespace and the <string></string> tags around the actual computer name.

I'm sure there are other prettier ways to parse the information from the preferences.plist file as well.
tessera is offline   Reply With Quote
Old 06-18-2009, 02:07 PM   #12
ephantom
Prospect
 
Join Date: Jun 2009
Posts: 1
I wanted to add to this thread that scutil can get you a clean Computer Name regardless of the state of the network.

scutil --get ComputerName

I have found that both scutil and hostname are useful when writing network scripts. Be aware that hostname can change if you are using DHCP depending on your Location setting and which network you are on. As an example here is a useful Perls scrip that will let you know of your desktop or laptop is connected to the network if you are using DHCP.

Code:
#!/usr/bin/perl

# Checks to see of network location is active by verifying DHCP has
# given the Macintosh a useable host name.

# Loop network check
while (1) {

	# Get dhcp hostname given to the system
	$hostname = `hostname`;
	
	# Get the Machintosh Computer Name
	$macname = `scutil --get ComputerName`;
	
	# Remove line returns
	chomp $hostname;
	chomp $macname;
	
	# Check if the host name is <Computer Name>.local
	# If it is, the Macintosh has not been assigned a DHCP hostname
	if ($hostname eq "${macname}.local") {
	
		print "$hostname  $macname NETWORK IS DISABLED\n";
	}
	else {
		print "$hostname  $macname network is active\n";
	}
	
	# Sleep for two seconds before repeting loop
	sleep(2);
}
ephantom is offline   Reply With Quote
Old 06-18-2009, 09:40 PM   #13
robot_guy
Triple-A Player
 
Join Date: Aug 2003
Posts: 132
What's wrong with

display dialog computer name of (system info)

?

No shell script, no "Extra Suites"...not sexy enough?

Just noticed the date of the thread...um, never mind.
__________________
*****************
** homo sapien **
*** emulation ***
**** wetware ****
*****************

Last edited by robot_guy; 06-18-2009 at 09:42 PM. Reason: Oh...I see...
robot_guy is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 05:42 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.