How to get contents of selected rows of Studio table view

It took me awhile to find out how to get the text content of selected rows in an AppleScript Studio table view. Thanks to another post I was able to get what I needed. Note that I use a ‘on selection changed’ handler. This lends itself well to selecting text and getting the result. So here’s the the essential lines you need:

on selection changed theObject
	set mySelection to selected data rows of table view 1 of scroll view 1 of window 1
end selection changed

To extract the text from ‘mySlection’ you would use a ‘repeat with thisRow in theDataSource’ with ‘set thisContent to (the contents of data cell yourNumber of thisRow) as text’ chunk of code. For example to get the content of each 7th cell of each row do the following:

on selection changed theObject
	set mySelection to selected data rows of table view 1 of scroll view 1 of window 1
        repeat with thisRow in theDataSource
		set thisContent to (the contents of data cell 7 of thisRow) as text
		display dialog thisContent
		end if
	end repeat
end selection changed

Hope this saves someone some time. Now if I can just figure out how to hook up the copy and paste menu items so they work…

I think you have a few typos or bugs in your script, and an unnecessary reference:

  1. The “end if” shouldn’t be there.

  2. theDataSource should be mySelection, to match your earlier variable assignment.

  3. The “on selection changed” handler automatically puts a reference to the responding table into the theObject variable for you, so you can refer to it instead of re-specifying the table.

So a simpler and working script would be:

on selection changed theObject
	set mySelection to selected data rows of theObject
	repeat with thisRow in mySelection
		set thisContent to (the contents of data cell 7 of thisRow) as text
		display dialog thisContent
	end repeat
end selection changed

I hope this helps,
Tom
BareFeet

Thanks for cleaning up my messy post. I can’t think and write at the same time :slight_smile: