Remove items in list from Table View

I have this list of items that I need to delete from a table view. Let’s say, for instance, that table view has Item 1, Item 2, Item 3, etc…to Item 20 in it. I have a list taken from a text file that has Item 3, Item 8 and Item 18 in it. I would like to delete the items in the list from the table. Here is the code that I have so far, which does not work:


set theTableView to table view "ItemList" of scroll view "ItemList" of theWindow
set theDataSource to data source of theTableView
set linecount to count every paragraph of myDataList
repeat with i from 1 to linecount
	display dialog (paragraph i of myDataList) --shows item correctly
	set selected data row of theTableView to (paragraph i of myDataList)
	set theData to selected data row of theTableView
	delete theData --generates error message "The variable theData is not defined. (-2753)"
end repeat

It appears that the set selected data row of theTableView to (paragraph i of myDataList) is not doing what it is supposed to do. Am I doing this right? Is there another way to programmatically tell the application to select a specific data row and delete it?

Thanks.

Hi,

selected data row expects a data row, not a paragraph


set theTableView to table view "ItemList" of scroll view "ItemList" of theWindow
set theDataSource to data source of theTableView
set linecount to count every data row of myDataList
repeat with i from 1 to linecount
	display dialog (paragraph i of myDataList) --shows item correctly
	set selected data row of theTableView to data row i of myDataList
	set theData to selected data row of theTableView
	delete theData --generates error message "The variable theData is not defined. (-2753)"
end repeat

StefanK…Thanks for the quick reply. I tried your method, but it still is not working. I changed it just a little bit (Display dialog (data row i of myDataList))to see what the data row gets set to. Here is the piece of code:


set theTableView to table view "ItemList" of scroll view "ItemList" of theWindow
set theDataSource to data source of theTableView
set linecount to count every data row of myDataList
repeat with i from 1 to linecount
	display dialog (data row i of myDataList) 
	set selected data row of theTableView to data row i of myDataList
	set theData to selected data row of theTableView
	delete theData --generates error message "The variable theData is not defined. (-2753)"
end repeat

This didn’t work at all. I put a display dialog (linecount) after the set linecount line and the display was 0. I then changed the set linecount back to count every paragraph of myDatalist and it set the count to 5 (which is right. I have 5 items in the list).

So I changed that, but when the display dialog (data row i of myDataList) gets called, it is breaking the myDataList variable up by letters. So for instance, if the first line item is Item 1; it displays “I”, then “t”, then “e”, then “m”, " ", “1”. Then the app errors out again with the same theData variable not defined.

For whatever reason, the only way I can get the line item to stay as a complete line item is to use the paragraph of myDataList.

Please help…

Thanks again for the quick reply!

Sorry, I mixed up the data source and the text file.
What is the exact content of the text file? Is it just the numbers of the lines to delete like

3 8 18
?
If yes, maybe this is sufficient


set theTableView to table view "ItemList" of scroll view "ItemList" of theWindow
set theDataSource to data source of theTableView

repeat with i from (count paragraphs of myDataList) to 1 by -1 -- deleting in reverse order avoids problems to reference the proper list item
    display dialog (paragraph i of myDataList)
    delete data row (paragraph i of myDataList as integer) of theDataSource
end repeat

No. The text file contains the names of the items in the list. The table has these values in it:

Item 1
Item 2
Item 3
Item 4
Item 5
Item 6

The text file has this:

Item 2
Item 4
Item 5

I need the table to look like this after:

Item 1
Item 3
Item 6

and they may not be exactly that or in that order. Table item names given just for example. The table might grow into many items not necessarily named neatly as item X. I basically need a way to search the table for an item name and select that row and delete it.

Is “item 2” literal text or a placeholder for a name?
If it’s a placeholder you could check for the contents of the data row cell

The problem is, if you delete an item by index for example item 4,
the former item 5 becomes item 4 after the deletion

StefanK, sorry for the delayed reply. “Item 2” is literal text. The items will have all different names. The list that I gave (Item 1, Item 2, Item 3) was for demonstration purposes only. The actual list has items in it (like inventory items with all different names). So, I need to be able to grab a list of items, search for them in the table, then delete the row for that item.

Sorry if my explanations are a little bit confusing.

Thanks again for you help!

Alright, I got it! What I had to do was do a nested repeat loop, one scrolling through the datasource and one scrolling through the list. So it starts by grabbing the first item in the text file and comparing it to every item in the table, if it finds the item, it deletes it. Then loops through and does the same for the next item in the list…etc…etc…etc…

Here is the piece of code that I used:


			--read data file into variable to delete from available titles table
			set myDataList to (read file dataFile)
			
			--delete software in catalog from available titles table
			set linecount to count every paragraph of myDataList
			
			repeat with j from 1 to linecount
				set theTableView to table view "itemList" of scroll view "itemList" of theWindow
				set theDataSource to data source of theTableView
				set rowCount to count of data rows of theDataSource
				try --needed because if you delete an item from the table, the number of rows is less and generates an error on the last run of the loop
					repeat with i from 1 to rowCount
						set thisRow to data row i of theDataSource
						set theContents to contents of data cell "items" of thisRow
						if theContents = (paragraph j of myDataList) then
							delete thisRow
						end if
					end repeat
				end try
			end repeat

Thanks again for all your help.