Getting the first item from a text line

Hi
Im after some help creating a list.
I have a .txt file which has 20 lines in it. and on each line the amount of items varies from 2 to 15. What Im trying to get is the first item from each line and display them all together. Can any one help?

Thanks in advance
MD

Hi MD,

can you post an example of a few lines,
it’s very difficult to guess the structure :wink:

Try this one:

set theLines to paragraphs of (read (choose file))
set AppleScript's text item delimiters to " " --This assumes your items are delimited by a space - change it if needed
set firstItems to {}
repeat with aLine in theLines
	set end of firstItems to text item 1 of aLine
end repeat
set AppleScript's text item delimiters to ""
firstItems

Good luck,
Vincent

Hi Stefan

here are a couple of lines from the text file

Laptop No Display Battery problem Error Message Hardware Fail No Power
Memory Error Message Hardware Fail

What i would eventualy like to do is Dispay all of the first items in a popup button and then display the corresponding ones in a second pop up box.

I have worked out how to get item 2 onwards to display in the second popup box but im un able to get all of item 1’s to show i can only get 1

My script so far is below


on awake from nib theObject
	set textfile to read file {"Macintosh HD:Users:tobin:Desktop:HardwareFinal.txt"} as string
	set text item delimiters to {return}
	set database_records_list to every text item of textfile
	repeat with i from 1 to (count items of database_records_list)
		set the_database_record to item i of database_records_list
		set text item delimiters to tab
		set field_list to every text item of the_database_record
		set theCount to count every text item of field_list
		set CallSubject2 to item 1 of field_list
		set CallSubject3 to items 2 thru theCount of field_list
	end repeat
	set theList to CallSubject2
	set pickList to text items of theList
	delete every menu item of menu of popup button "CallSubject2" of window "Window"
	repeat with i in pickList
		make new menu item at the end of every menu item of menu of popup button "CallSubject2" of window "Window" with properties {title:i, enabled:true}
	end repeat
	set theList2 to CallSubject3
	set pickList to text items of theList2
	delete every menu item of menu of popup button "CallSubject3" of window "Window"
	repeat with i in pickList
		make new menu item at the end of every menu item of menu of popup button "CallSubject3" of window "Window" with properties {title:i, enabled:true}
	end repeat
end awake from nib


Cheers
MD

Use this:

set theLines to paragraphs of (read (choose file))--or any other source text
set AppleScript's text item delimiters to " " --This assumes your items are delimited by a space - change it if needed
set firstItems to {}
repeat with aLine in theLines
	set end of firstItems to word 1 of aLine
end repeat
set AppleScript's text item delimiters to ""
firstItems

Hi MD,

the first part is no problem,
this extracts the first text items of the .txt file

set database_records_list to paragraphs of (read file (((path to desktop) as string) & "HardwareFinal.txt"))
set {CallSubject2, CallSubject3} to {{}, {}}
set text item delimiters to tab
repeat with i in database_records_list
	set end of CallSubject2 to text item 1 of i
	set end of CallSubject3 to text items 2 thru -1 of i
end repeat
set text item delimiters to {""}
	delete every menu item of menu of popup button "CallSubject2" of window "Window"
repeat with i in CallSubject2
	make new menu item at the end of every menu item of menu of popup button "CallSubject2" of window "Window" with properties {title:i, enabled:true}
end repeat

the second part with “item 2 thu end-list” is more difficult.
In your script and also in mine above you get a list like
{item 1 {text item 2 of item 1, … , text item -1 of item 1}, item 2 {text item 2 of item 2, … , text item -1 of item 2}}
this is certainly not what you want.
Maybe it helps so far a bit

Hi Stefan

Thanks for all your help. The first part of the code works perfectly, but like you i am unable to get the second part to display. ive have been trying different ways of getting the info, and in AS editor it works but in XCode i cant seem to display it. Maybe this is a post i shoul put under Xcode rather than OS X.

Also Vincent thanks for your help, i tried using your script but unfortunately, it wa not quite what i was after. But thank you anyway.

The file im trying to import from was originaly a Excel document but i saved it down as a .txt file. Each cell in the original contained a topic that i wanted to enter it into a popup list. Every thing in popup 1 called "CallSubject2"is from the first column, then the second popup called “CallSubject3” would contain every thing in the row relating to the cell/item selected in “CallSubject2”.

It might be that im putting the script in the wrong order and not in the correct handlers.

So far the best i have is what Stefan came up with

Any ideas guys

Cheers
MD

Hi MD,

the question is, in which way do you want the values to be displayed?

in case of CallSubject2, you have a list of one value:
a
b
c
in case of CallSubject3 you get a list of tab separated values (\t is the tab character)
a \t b \t c
d \t e
f \t g \t h \t i

Hi Stefan,
Im being dumb i know but what do you mean by tab character?

the file is saved as Text (tab delimited)

CallSubject2 =
a1
b1
c1

if c is selected then display all cells after c1
c2/c2/c3etc…
if a is selected then display all cells after a1
a1/a2/a3 etc…

Cheers
MD

Hi MD,

now I got it! :slight_smile:

try this:

set database_records_list to paragraphs of (read file (((path to desktop) as string) & "HardwareFinal.txt"))
set {CallSubject2, CallSubject3} to {{}, {}}
set text item delimiters to tab
repeat with i in database_records_list
	set end of CallSubject2 to text item 1 of i
	set end of CallSubject3 to text items 2 thru -1 of i
end repeat
set text item delimiters to {""}
delete every menu item of menu of popup button "CallSubject2" of window "Window"
repeat with i in CallSubject2
	make new menu item at the end of every menu item of menu of popup button "CallSubject2" of window "Window" with properties {title:i, enabled:true}
end repeat

-- here goes your code to determine which item of the list CallSubject2 has been chosen
-- I assume the result as an index number "chosenItem" as integer

set pickList to items of item chosenItem of CallSubject3
delete every menu item of menu of popup button "CallSubject3" of window "Window"
repeat with i in pickList
	make new menu item at the end of every menu item of menu of popup button "CallSubject3" of window "Window" with properties {title:i, enabled:true}
end repeat

we mean the same: tab (character) = tab delimiter

Hi Stefan

The problem i have is that rather than hard coding the data into the script i would like all the data to come from the text file as the fil will change every now and again.

eg of txt file

CallSubject2 =
a1=CD Rom
b1=Hard Drive
c1=Keybard

if c is selected then display all cells after c1
c2=not working/c2=buttons sticking/c3=Missing/etc…
if a is selected then display all cells after a1
a1=Not Ejecting/a2=Not Reading/a3=making a noise/etc…

Thansk for all the help
If your ever in the London area let me know and ill buy you a drink, :o)

Cheers
MD

I think, we have a misunderstanding.
Lets go thru the code detailed:

The first part reads the text file and returns two lists:
CallSubject2 = every first item of the text file’s lines
CallSubject3 = the rest of the items as list

set database_records_list to paragraphs of (read file (((path to desktop) as string) & "HardwareFinal.txt"))
set {CallSubject2, CallSubject3} to {{}, {}}
set text item delimiters to tab
repeat with i in database_records_list
   set end of CallSubject2 to text item 1 of i
   set end of CallSubject3 to text items 2 thru -1 of i
end repeat
set text item delimiters to {""}

then you prepare your first pop up menu and set the entries:

delete every menu item of menu of popup button "CallSubject2" of window "Window"
repeat with i in CallSubject2
   make new menu item at the end of every menu item of menu of popup button "CallSubject2" of window "Window" with properties {title:i, enabled:true}
end repeat

So far so good
Here comes the crucial point:
The entries of the second pop up menu can only be created when we have the information which item of the first pop up menu is selected

set pickList to items of item chosenItem of CallSubject3
delete every menu item of menu of popup button "CallSubject3" of window "Window"
repeat with i in pickList
   make new menu item at the end of every menu item of menu of popup button "CallSubject3" of window "Window" with properties {title:i, enabled:true}
end repeat

I’ll give you a call :slight_smile:

Hi
Sorry about the missunderstanding. It all works fine with the exception of CallSubject3
I dont seem to see how to set the variable chosenItem to display the relevent fields.

at this rate im boing to have to buy you a brewery :wink:

MD

:lol::lol:

We’re talking about a Xcode project, don’t we?

enable “choose menu item” for popup menu CallSubject2 and use this handler

on choose menu item theObject
	if theObject is popup button "CallSubject2" of window "Window" then
		set ChosenItem to (tag of current menu item of theObject)
		set pickList to items of ChosenItem of CallSubject3
		delete every menu item of menu of popup button "CallSubject3" of window "Window"
		repeat with i from 1 to count pickList
			make new menu item at the end of every menu item of menu of popup button "CallSubject3" of window "Window" with properties {title:item i of pickList, enabled:true, tag:i}
		end repeat
	end if
end choose menu item

to specify the tags of the first list you should change the routine into

delete every menu item of menu of popup button "CallSubject2" of window "Window"
repeat with i from 1 to count CallSubject2
make new menu item at the end of every menu item of menu of popup button "CallSubject2" of window "Window" with properties {title:item i of CallSubject2, enabled:true, tag:i}
end repeat

Hi

Thanks for that, i just have to set the variable for CallSubject3 and i think we will be sorted. Thanks so much for your help

MD