Dropdown list created out of a text file

Hi everybody,
Could someone help me get started…

I have 2 text files: Location1.txt and location2.txt

Location1.txt contains:

Loc1Printer1, Printer1 IP, Printer1 PPD, Printer1 Info
Loc1Printer2, Printer2 IP, Printer2 PPD, Printer2 Info
Loc1Printer3, Printer3 IP, Printer3 PPD, Printer3 Info

Location2.txt contains:

Loc2Printer1, Printer1 IP, Printer1 PPD, Printer1 Info
Loc2Printer2, Printer2 IP, Printer2 PPD, Printer2 Info
Loc2Printer3, Printer3 IP, Printer3 PPD, Printer3 Info

And this is what I would like to do:

I would like to create a applescript that launces a window where the user can click on to bottums:

Location1 Location2

If the user clicks on Location1 a dropdown menu is filled with the info from Location1.txt
like this:

Loc1Printer1, Printer1 Info
Loc1Printer2, Printer2 Info

If the user clicks on the other buttom Location2 the dropdown is filled with

Loc2Printer1, Printer1 Info
Loc2Printer2, Printer2 Info

When then the user clicks on one if the items in the dropdown list - example:

Loc2Printer1, Printer1 Info

the full line in the right text file is read and given as parameters to a nother script:

Like this:

User clicks on:
Loc2Printer1, Printer1 Info

Following info is is given as paramenters to a nother script:
Loc1Printer1, Printer1 IP, Printer1 PPD, Printer1 Info

Does this make sense to anyone…?
I need it to make it more simple for the users to create there printers.

Any imput would be appriciated…

Thanks
Thomas

It isn’t pretty and it contains practically no error checking but this should get you started. It takes the script to the point where it can pass the parameters (held by the variable printer_info_out) to the other script. The easiest way to export the info might be to generate another text file for the other script to read.

Note: You need to modify the paths of the two text files to reflect local conditions.

set dd to (display dialog "Choose a location" buttons {"Location 1", "Location 2", "Cancel"} default button 3)

if button returned of dd is "Location 1" then
	set loc to paragraphs of (read file "path:to:location1.txt")
else
	if button returned of dd is "Location 2" then
		set loc to paragraphs of (read file "path:to:location2.txt")
	end if
end if

set tids to text item delimiters
set display_info to {}
try
	set text item delimiters to ","
	repeat with item_ in loc
		set end of display_info to (text item 1 of item_ & ", " & text item -1 of item_)
	end repeat
	set text item delimiters to tids
on error
	set text item delimiters to tids
end try

set printer_info to (choose from list display_info) as text

if printer_info is "false" then -- user chose cancel button
	error number -128 -- terminate script, or
	-- do something else
end if

set count_ to 0
repeat with item_ in display_info
	set count_ to count_ + 1
	if contents of printer_info is equal to contents of item_ then
		set printer_info_out to item count_ of loc
		exit repeat
	end if
end repeat

-- for your benefit, this dialog shows what was chosen and what will be sent to the other script.
-- it can be eliminated in the oroduction version of the script.
display dialog "User chose: " & return & item_ & return & return & "Send this to other script: " & return & printer_info_out

Take a look at the dictionary of StandardAdditions.osax to see the what you can do with the ‘choose from list’ command. It provides options which aren’t incorporated in the code above.

– Rob

Thanks Rob,
That was really great…

I have 2 more quistions:

  1. How can I get the first window to contain 6 locations - and not listed in one row ?
  2. How can I get the output into 4 seperate parameters instead of one string.

Again…thanks for your answer.

For thoose of you why might be interested - im trying to make it easy for our users to setup there printers. What we need to do is distribute 1 text file with all the printers.
And ofcause update it if something changes…

The Parameters are then given to a shell script:

do shell script "lpadmin -p Parameter1 -E -v lpd://Parameter2/Parameter3 -P /Parameter4 "

Where

Parameter1 = Internal name of the printer
Parameter2 = Printserver
Parameter3 = Print Queue
Parameter4 = Path to PPD or gz file

Regards
Thomas

I don’t understand. Can you elaborate?

This might be close to what you need but I suspect that I don’t have the variables (p1 - p4) in the correct locations in the shell command.

set dd to (display dialog "Choose a location" buttons {"Location 1", "Location 2", "Cancel"} default button 3)

if button returned of dd is "Location 1" then
	set loc to paragraphs of (read file "Powerbook G4:Users:rjj:Desktop:location1.txt")
else
	if button returned of dd is "Location 2" then
		set loc to paragraphs of (read file "Powerbook G4:Users:rjj:Desktop:location2.txt")
	end if
end if

set tids to text item delimiters
set display_info to {}
try
	set text item delimiters to ","
	repeat with item_ in loc
		set end of display_info to (text item 1 of item_ & ", " & text item -1 of item_)
	end repeat
	set text item delimiters to tids
on error
	set text item delimiters to tids
end try

set printer_info to (choose from list display_info) as text

if printer_info is "false" then -- user chose cancel button
	error number -128 -- terminate script, or
	-- do something else
end if

set count_ to 0
repeat with item_ in display_info
	set count_ to count_ + 1
	if contents of printer_info is equal to contents of item_ then
		set printer_info_out to item count_ of loc
		exit repeat
	end if
end repeat

try
	set text item delimiters to ","
	set {p1, p2, p3, p4} to ({text item 1, text 2 thru end of text item 2, ¬
		text 2 thru end of text item 3, text 2 thru end of text item 4} of printer_info_out)
	set text item delimiters to tids
on error
	set text item delimiters to tids
end try

set shell_command to "lpadmin -p " & p1 & " -E -v lpd://" & p2 & "/" & p3 & " - P /" & p4
--do shell script shell_command

This can likely be improved/optimized but it’s late and I need to get some sleep. Sorry 'bout that!

– Rob