Check if item exists in table view

Hi!

When adding an entry to a table view (unlimited rows, one column) I want the program to check if an entry with the same name already exists.
See the comment in the script.

Tried everything that came to my mind but XCode was always complaining about the things I tried.


on clicked theObject
	
	set validcharacters to "abcdefghijklmnopqrstuvwxyz1234567890 "
	
	display dialog "Enter name of new setup:" default answer "" buttons {"OK", "Cancel"} default button 1 with icon note
	set SetupName to text returned of result
	
	set SetupNameOkay to true
	
	if SetupName is "" then
		set SetupNameOkay to false
		display dialog "You forgot to enter a name for the setup."
	else
		repeat with tempchar in characters of SetupName
			if validcharacters does not contain tempchar then
				set SetupNameOkay to false
				display dialog "Only letters, numbers and spaces allowed"
				exit repeat
			end if
		end repeat
	end if
	
	if SetupNameOkay then
		set theTableView to table view "applications" of scroll view "scroll" of window "main"
		set dataSource to data source of theTableView
		
		
		
		
		-- I think here should be the best place where the checking should take place
		--
		-- repeat with (contents of every data cell of every data row of dataSource)
		-- 
		--
		--
		-- end repeat
		
		
		set dataRow to make new data row at end of data rows of dataSource
		set contents of data cell "application" of dataRow to SetupName
		
		-- ... do some other stuff ...		
		
		set contents of default entry "apps" of user defaults to (contents of every data cell of every data row of dataSource)
	end if
	
end clicked

Hi,

If you use:

contents of every data cell of every data row of dataSource

then you get a list of lists

It’s easier to get a list of the names

set name_list to (contents of data cell 1 of every data row of dataSource)

Then you can find out if a name is in the list with:

if “SomeName” is in name_list then
beep 2
– do something or not
end if

or

if “SomeName” is not in name_list then
beep 2
– do something or not
end if

Also note that you can use the ‘append’ command to append and record or list to the data source. See ‘append’ in the docs.

gl,

Kel,

Thanks for this. It works perfectly. My only question is, how would you make this script “select” or hightlight the row that matches the word your searching for?

Thanks in advance,
Twitch

Jacques, I can’t thank you enough. I knew it was something simple, but I just couldn’t quite get it working. You ended my frustration…haha

Thanks again, you rock!
Twitch

I think this is an elegant way of finding the entry. (I used to loop thru all rows searching for a match)
So I adapted your suggestion:


set DB to make new data source at end with properties {name:"DB"}
tell DB
		make new data column at end of data columns with properties {name:"num", sort order:ascending, sort type:alphabetical, sort case sensitivity:case sensitive}
---etc
end tell
append DB with clientList  -- add the entries 
set clientTable's data source to DB
set numList to contents of data cell "num" of data rows of DB
if thisClient's num is in numList then
	-- change value
	(*                          Here is my adaption                *)
	set a to (first data row of DB of it whose contents of data cell "num" is thisClient's num)
	log("10787 a's num: " & (content of a's data cell "num") & " thisClient's num: " & thisClient's num)
-->output: "10787 a's num: 00586 thisClient's num: 03568"
	(*        which isn't actually what I expected              *) 
	-- do whatever
else
	-- make new row
	set newRow to {{nw:true, num:thisClient's num, nom:"--", mailit:false, address:"@.", printit:false, mostrecentfilename:thisFileName}}
	append DB with newRow
end if

Edit: any ideas why the comparision returns true when it isn’t?
Thanks in advance.

Wow, Jaques :smiley: That’s the ticket!
Now my code and log looks real slick
Thanks allot :cool:

How about checking conditions of multiple columns?
this works for me right now, but I was wondering if there was a way to do it more elegantly → without loop

	set emailTheseList to {}
	repeat with a in (DB's data rows)
		if (content of a's data cell "nw") and (content of a's data cell "mailit") and ("--" ≠ content of a's data cell "mostrecentfilename") then set emailTheseList's end to a
	end repeat

Edit: this doesn’t work:

set emailTheseList to data rows of DB of it (whose content of data cell 2)and (whose content of data cell 1)

Thanks Jacques that looks much better.

Unfortunately it didn’t work as expected with 3 evaluations.
You were a bit quick and dropped an “of
The working syntax is:

set emailTheseList to data rows of DB of it whose content of data cell 1 is true and content of data cell 2 is true and content of data cell 3 is not "--"