need help with repeat / usrname script

Hello everyone, I started using Applescript a couple of days ago because we need a script that allows students who login locally to be prompted to mount their home dir. The script is not posted in it’s entirety, because it all worked… until I attempted to add a repeat in the script.

Basis of script before adding the repeat…

  1. Student is forced to use a number for their studentID
  2. then the student enters pw
  3. script mounts directory and opens it on desktop

all of that works in the script… the problem is that when the student inputs the wrong credentials, the script stops, and it brings up the standard apple connect to server dialog box… which isn’t really a big deal, but it would be nice if I could just restart the script until they logged in successfully.

this is the error i’m getting when trying to compile the script after adding the repeat… Syntax Error Expected “end” but found “to”. It highlights the “to” on “to getusername()” and stops there. I just don’t know why I can’t use this script I found online with a repeat… any help would be appreciated. I ended up sticking the entire script on here…

set diskname to “storage”
repeat until diskname is in (do shell script “/bin/ls /Volumes”)
to getusername()
set displayString to “Please enter your StudentID Number:”
set defaultAnswer to 0
try
repeat
set response to display dialog displayString default answer defaultAnswer
try
set username to (text returned of response) as number
exit repeat
on error errstr
set displayString to errstr & return & “Please try again.”
set defaultAnswer to text returned of response

		end try
	end repeat
	
on error number errorNumber
	if (errorNumber = -128) then
		tell application "Finder"
			say "no student eye dee"
		end tell
	end if
end try

return {username}

end getusername

to getuserpwd()
try
repeat

		display dialog "Enter your password:" default answer "" with hidden answer
		
		set userpwd to text returned of result
		
		if the userpwd is not equal to "" then exit repeat
		
	end repeat
	
on error number errorNumber
	if (errorNumber = -128) then
		tell application "Finder"
			say "no password"
		end tell
	end if
end try

return {userpwd}

end getuserpwd

set username to getusername()

set userpass to getuserpwd()
(*tell application “Finder”

say username

end tell *)
end repeat

set diskname to "storage"
repeat until diskname is in (do shell script "/bin/ls /Volumes")
to getusername()
    set displayString to "Please enter your StudentID Number:"
    set defaultAnswer to 0
    try
        repeat
            set response to display dialog displayString default answer defaultAnswer
            try
                set username to (text returned of response) as number
                exit repeat
            on error errstr
                set displayString to errstr & return & "Please try again."
                set defaultAnswer to text returned of response
                
                
            end try
        end repeat
        
    on error number errorNumber
        if (errorNumber = -128) then
            tell application "Finder"
                say "no student eye dee"
            end tell
        end if
    end try
    
    return {username}
    
end getusername

to getuserpwd()
    try
        repeat
            
            display dialog "Enter your password:" default answer "" with hidden answer
            
            set userpwd to text returned of result
            
            if the userpwd is not equal to "" then exit repeat
            
        end repeat
        
    on error number errorNumber
        if (errorNumber = -128) then
            tell application "Finder"
                say "no password"
            end tell
        end if
    end try
    
    return {userpwd}
    
end getuserpwd

set username to getusername()

set userpass to getuserpwd()
(*tell application "Finder"
    
    say username
end tell *)
end repeat
repeat until diskname is in (do shell script "/bin/ls /Volumes")
to getusername()

You cannot define a handler (subroutine) inside a repeat loop. Or anyting else, it has to defined at the top level of a script.

Good luck, you are going to need it.

set diskname to "storage"
repeat until diskname is in (do shell script "/bin/ls /Volumes")
	set username to getusername()

      set userpass to getuserpwd()

end repeat


to getusername()
	set displayString to "Please enter your StudentID Number:"
	set defaultAnswer to 0
	try
		repeat
			set response to display dialog displayString default answer defaultAnswer
			try
				set username to (text returned of response) as number
				exit repeat
			on error errstr
				set displayString to errstr & return & "Please try again."
				set defaultAnswer to text returned of response
				
				
			end try
		end repeat
		
	on error number errorNumber
		if (errorNumber = -128) then
			tell application "Finder"
				say "no student eye dee"
			end tell
		end if
	end try
	
	return {username}
	
end getusername

to getuserpwd()
	try
		repeat
			
			display dialog "Enter your password:" default answer "" with hidden answer
			
			set userpwd to text returned of result
			
			if the userpwd is not equal to "" then exit repeat
			
		end repeat
		
	on error number errorNumber
		if (errorNumber = -128) then
			tell application "Finder"
				say "no password"
			end tell
		end if
	end try
	
	return {userpwd}
	
end getuserpwd


(*tell application "Finder"
    
    say username
end tell *)

any suggestions on how to make this repeat… or can I call the subroutine to run again after the student’s creds don’t allow the login? Or is it possible to check to see if the disk is mounted, and if not, start the entire script from line 1 again?