Delete Items From Outline View

Hello,

I am currently using this bit of code to delete items from an outline view. I have three data items at the top level of the outline view that I never want deleted so they are excluded from the delete. My problem though is that this process of excluding the data items the way I have done here can really take a long time to process if I have say 7000 thousand items to delete. It takes about 5-6 seconds for every 500 hundred data items. Does anyone know a faster way that I could use to do the deletions while still excluding the top level data items?

Thanks
Dallas


	if name of theObject is "Remove" then
		set theSelectedRow to selected data items of outline view "outlineview1" of scroll view "scroll1" of window "main"
		tell data_source
			set search_folder_exemption to data item "Search Folders"
			set good_movie_exemption to data item "Good Movies"
			set bad_movie_exemption to data item "Bad Movies"
		end tell
		repeat with a from 1 to length of theSelectedRow
			set therow to item a of theSelectedRow
			if therow is not search_folder_exemption and therow is not good_movie_exemption and therow is not bad_movie_exemption then
				delete therow
			end if
		end repeat
		tell data_source
			set count_errors_bm to (count data items of data item "Bad Movies")
			set count_errors_for_data_cell to " - (" & count_errors_bm & ")" as string
			set contents of data cell "Status" of data item "Bad Movies" to x_mark & count_errors_for_data_cell
			set count_errors_gm to (count data items of data item "Good Movies")
			set count_errors_for_data_cell to " - (" & count_errors_gm & ")" as string
			set contents of data cell "Status" of data item "Good Movies" to check_mark & count_errors_for_data_cell
		end tell
		if count_errors_gm is 0 and count_errors_bm is 0 then
			set enabled of button "Open file" of window "main" to false
		end if
	end if

My bet is that the repeat loop is whats hurting you. Its fine for smaller numbers of items, but on this scale, it’ll be very inefficient.

I would try something like a whose clause. Note the pseudo code - UNTESTED. You might need to tweak it a bit for proper language.


   if name of theObject is "Remove" then
       set theSelectedRow to selected data items of outline view "outlineview1" of scroll view "scroll1" of window "main"
       tell data_source
           set search_folder_exemption to data item "Search Folders"
           set good_movie_exemption to data item "Good Movies"
           set bad_movie_exemption to data item "Bad Movies"
           -- PSEUDO CODE 
           delete every item of theSelectedRow whose name is not "Search Folders" and name is not "Good Movies" and name is not "Bad Movies"
           -- END PSEUDO CODE
       end tell
       
       tell data_source
           set count_errors_bm to (count data items of data item "Bad Movies")
           set count_errors_for_data_cell to " - (" & count_errors_bm & ")" as string
           set contents of data cell "Status" of data item "Bad Movies" to x_mark & count_errors_for_data_cell
           set count_errors_gm to (count data items of data item "Good Movies")
           set count_errors_for_data_cell to " - (" & count_errors_gm & ")" as string
           set contents of data cell "Status" of data item "Good Movies" to check_mark & count_errors_for_data_cell
       end tell
       if count_errors_gm is 0 and count_errors_bm is 0 then
           set enabled of button "Open file" of window "main" to false
       end if
   end if


Another option would be to employ the “has parent” property of outline view items. Your top-level data items won’t have a parent. The rest do.

Hi TJ,

Thanks for the reply. The problem is that variable “theSelectedRow” is a list. This is how the outline view returns the selected data items. So the repeat is required to cycle through the list. Maybe you know another way?

Thanks
Dallas

You might try something like…



every data item of (every item in theSelectedRow)


Hi TJ,

I can’t seem to get away from the repeat. How does the ‘Has Parent’ property work. I’ve been looking around a bit and haven;t found muc information on that subject.

Thanks again
Dallas

Look up that property in the AppleScript Studio documentation. That will tell you what you need to know.