How do I grab the first letter of a word that is input?

…that will probably take you guys half a second to solve (but I’m stumped):

How do I grab the first letter of a word that is input?

Here’s the deal: I’m trying to make a nice server login script for our students, so that when they run the script, it asks them for their network username, then their network password, then it logs them onto the (Win NT, blah) server and opens their personal folder.

The folders are on the server sorted by the first letter of their last name: all the As are in the ‘a’ folder, Bs in ‘b’, etc.

The login command is simple to type into the Connect to Server dialong (afp://xxx.xxx.xxx.xxx/Users/s/smith).

So I want this to, after they input their username, grab the first letter of their last name (which is the first letter of their network username), then add that to the login command. If I can just grab that first letter of their username, I can plug it into that command.

Here’s what I have so far:


tell application "Finder"
activate
display dialog "Input your network username" default answer "" buttons {"Cancel", "OK"} default button 2
set theName to result
set theLetter to first letter of result
end tell

That’s just to test out what comes back as the result, but…it won’t even compile. I get the oh-so-helpful error of “Expected class name but found identifier.”

I tried changing the “set theLetter to first letter of result” to “set theLetter to first character of result”; it compiles fine, but when I run it with the Result pane open, I get “Can’t get character 1 of {text returned:“smith”, button returned:“OK”}.”

Any advice?

not sure never tried it but this might work…

tell application "Finder" 
activate 
set theName to text returned of (display dialog "Input your network username" default answer "" buttons {"Cancel", "OK"} default button 2) 
set theLetter to first letter of theName
end tell 

maybe perhaps???..

This might work. Note that Finder isn’t required but it will work either way. It depends on what you want the script to do.

set txt_returned to text returned of ¬
	(display dialog "Input your network username" default answer "" buttons ¬
		{"Cancel", "OK"} default button 2)

if txt_returned is not "" then set char1 to first character of txt_returned

– Rob

Thanks to everyone for their suggestions so far.

Based on your helpful posts, and some other info I found searching these forums, here’s what I have and it seems to work quite well:


tell application "Finder"
	activate --bring the Finder to the front so dialogs aren't hidden
	try -- check to see if volume is already mounted
		set VolumeName to "Users" as string
		set DiskList to list disks --get all disks currently mounted --unimportant if this is strictly a script that is run on startup 
		
		if DiskList contains VolumeName then
			select VolumeName
			eject VolumeName
			
		else
			login() of me --invoke the login subroutine 
		end if
	end try
end tell

login()

on login()
	tell application "Finder"
		activate
		set txt_returned to text returned of ¬
			(display dialog "Input your network username" default answer "" buttons ¬
				{"Cancel", "OK"} default button 2)
		
		if txt_returned is not "" then set char1 to first character of txt_returned
		set user_name to txt_returned
		
		mount volume "afp://xxx.xxx.xxx.xxx/Users/"
		
		select folder user_name of folder char1 of disk "Users"
		
		open selection
	end tell
end login

This first checks to see if a prior user has left themselves logged in. If so, it unmounts the server, then prompts them for their network username (this is to gather the info needed to open their personal folder after logon). Then they get the regular NT Login screen, which asks for their username and password (if anyone can help me automate around that authentication screen, that would be great – I couldn’t find a way), then logs them into the server. Once the Users volume is mounted, it finds their personal folder, and opens it, so they can access their files quickly, without digging around the server.

If anyone can see a way to streamline this or speed it up, I’d be happy to hear. As it is, it works quite well, so I’m going to start putting it on the desktop of all of our lab machines.

Here’s a slightly modified version that leaves the Finder out of things that don’t require the Finder. It also eliminates working with selected items. I prefer to avoid working on selections, when possible, because a user can change a selection during a script’s execution. This can cause unexpected and undesired results.

tell application "Finder" to activate --bring the Finder to the front so dialogs aren't hidden

try -- check to see if volume is already mounted 
	set VolumeName to "Users" as string
	set DiskList to list disks --get all disks currently mounted --unimportant if this is strictly a script that is run on startup 
	
	if DiskList contains VolumeName then
		tell application "Finder" to eject disk VolumeName
	else
		login() of me --invoke the login subroutine 
	end if
end try

login()

on login()
	tell application "Finder" to activate
	set txt_returned to text returned of ¬
		(display dialog "Input your network username" default answer "" buttons ¬
			{"Cancel", "OK"} default button 2)
	
	if txt_returned is not "" then set char1 to first character of txt_returned
	set user_name to txt_returned
	
	mount volume "afp://xxx.xxx.xxx.xxx/Users/"
	
	tell application "Finder" to open folder user_name of folder char1 of disk "Users"
end login

– Rob

Rob,

Thanks for the quick reply!

I tried your mods, but when I double-click the script in the Finder, the “Input your network username” doesn’t appear, instead, the script icon bounces in the Dock, and I have to click on it to have the “Input your…” box to appear. The login works fine, but here’s something odd: if the server was not mounted on the Desktop, after the successful login, it asks me for my network username again.

If I cancel out of it, it asks me again for my network username after hitting Cancel.

This script is meant to sit on the Desktop of all of our lab Macs (which stay logged into a “Guest” account), so that students can get right to their private folders. Then when the next student runs it, it logs off the previous user (if they forgot to do it themselves) and repeats the login process.

Yeah, using the Finder for selecting stuff can be dicey.

I don’t know why the script continues when you cancel the dialog unless the login handler is being called twice. Maybe this will solve the problems (bouncing icon, won’t quit when canceled). You might also want to consider that further action might be needed if the user fails to enter a username in the dialog.

tell application "Finder" to activate --bring the Finder to the front so dialogs aren't hidden 

try -- check to see if volume is already mounted 
	set VolumeName to "Users" as string
	set DiskList to list disks --get all disks currently mounted --unimportant if this is strictly a script that is run on startup 
	
	if DiskList contains VolumeName then
		tell application "Finder" to eject disk VolumeName
	else
		login() of me --invoke the login subroutine 
	end if
end try

login() -- is this needed?

on login()
	tell application "Finder"
		activate
		set dd to (display dialog "Input your network username" default answer "" buttons ¬
			{"Cancel", "OK"} default button 2)
	end tell
	
	if button returned of dd is not "OK" then error number -128 -- quit script
	set txt_returned to text returned of dd
	
	if txt_returned is "" then
		-- no text entered
		-- exit script?
		-- ask for username until one is entered?
	end if
	
	set char1 to first character of txt_returned
	set user_name to txt_returned
	mount volume "afp://xxx.xxx.xxx.xxx/Users/"
	
	tell application "Finder" to open folder user_name of folder char1 of disk "Users"
end login

– Rob