Matching An ID to a Name application script

This should probably be an easy script, yet not for me.
I just want to receive a name based on a long list of 4 digit number ids that represent a name. (which could be enterred inside the script - no need to access another file)
For example:
4598 = John
4576 = Jeff
4562 = Joe
4500 = Bill,

etc, etc.

I wanted to create an application script that would throw up a dialog box that would ask the user to enter the 4 digit id. The script would then return the name associated with the id.

Any ideas?

This is crude and quick, but it works for me - it needs some serious fine tuning, but the concept works.

set IDKey to {4598, "John", 4576, " Jeff", 4562, "Joe", 4500, "Bill"} -- just a list of alternating numbers and names. I tried to use a property record here, but could not figure out how to construct the name of a variable.

set Q to display dialog "Please enter 4-digit ID number" default answer "xxxx" buttons {"Cancel", "Enter"} default button "Enter"
if button returned of Q is "Enter" then
	set IDN to text returned of Q
	try
		set T to IDN as number
	on error
		display dialog "Got an error for that entry - was it all numbers?"
		return
	end try
else
	return -- catches "Cancel".
end if
-- check for 4 digits
if (count of characters of IDN) is not 4 then
	display dialog "Please start again - the entry must be 4 consecutive digits, no spaces"
	return
end if
repeat with K from 1 to ((count of IDKey) - 1) by 2
	if item K of IDKey is IDN as number then
		set theName to item (K + 1) of IDKey
		display dialog "The name for ID " & IDN & " is " & theName
		return
	end if
end repeat
display dialog "No name found for that number"

Adam,
I do not know what you mean by crude and quick? This seems to be right on target. And it appears to work perfectly.

I owe you a huge thanks!!! This script turned out to be more involved than I thought it would. I wouldn’t have accounted for as many errors as you did.

Once again, thank you this is GREAT!!!

You’re welcome. By crude and quick I just meant that it doesn’t have any way to enter codes and names into the list (which must therefore be done in the script itself), and if the list contains a few thousand names, this approach won’t be very fast. Glad it works. Taught me something about records which are not appropriate for this job.

Would it be possible for someone to help this script contain one or two more parameters?
Rather than having to relaunch the script upon error, could it be scripted to have one of the buttons yield: “Try Again”, in which the script would do just that? Ideally even if a number entered equalled a correct match, it would be preferred to give the user the option to “End” or “Try Again” which would repeat the process?

This is far from being a critical request. So if this involves a lot of time and effort, please disregard this post. FWIW, this should be good practice for me to try and make work. But obviously an answer is equally beneficial :slight_smile:

Thanks again,
jeff

You can play with the conditions of when it does what, but here’s the general setup - make the data a property, and make the machinery a handler, then do all your tests in the main part of the program. I tested this for about 3 minutes, so you may have to track down some conditions you don’t like.

property IDKey : {4598, "John", 4576, " Jeff", 4562, "Joe", 4500, "Bill"} -- just a list of alternating numbers and names.
-- The decisions are here:
set Reply to GetNumber()
if Reply = "NotNum" then
	set Reply to GetNumber()
else if Reply = "Cancel" then
	return
else if Reply = "Not4" then
	set Reply to GetNumber()
else if Reply = "GotName" then
	display dialog "Do you want to enter another name?" buttons {"No", "Yes"} default button "Yes"
	if button returned of result is "Yes" then
		set Reply to GetNumber()
	else
		return
	end if
else if Reply = "NoName" then
	display dialog "Try Again?" buttons {"No", "Yes"} default button "Yes"
	if button returned of result is "Yes" then
		set Reply to GetNumber()
	else
		return
	end if
else
	return
end if
-- The machinery is here, unaltered from before except to make sure there's a return for each case.
to GetNumber()
	set Q to display dialog "Please enter 4-digit ID number" default answer "xxxx" buttons {"Cancel", "Enter"} default button "Enter"
	if button returned of Q is "Enter" then
		set IDN to text returned of Q
		try
			set T to IDN as number
		on error
			display dialog "Got an error for that entry - was it all numbers?"
			return "NotNum"
		end try
	else
		return "Cancel"
	end if
	-- check for 4 digits
	if (count of characters of IDN) is not 4 then
		display dialog "Please start again - the entry must be 4 consecutive digits, no spaces"
		return "Not4"
	end if
	repeat with k from 1 to ((count of IDKey) - 1) by 2
		if item k of IDKey is IDN as number then
			set theName to item (k + 1) of IDKey
			display dialog "The name for ID " & IDN & " is " & theName
			return "GotName"
		end if
	end repeat
	display dialog "No name found for that number"
	return "NoName"
end GetNumber

Adam,
You outdid yourself here, this is great!
No way could I have set that up.
Thanks again Adam

jeff