Multiple selection - table views

I have a table view set up where files are chosen and placed into the table for reordering. Single file reordering t works fine but I am not able to do multiple selections and drags within the table to do the reordering - only one data row moves…code:


...
else if name of theObject = "choose" then
		set theFolder to choose folder with prompt "Choose the folder containing the files" default location (path to desktop) without invisibles
		tell application "Finder"
			set theFiles to every file of theFolder
			set theTableViewContents to {}
			repeat with aFile in theFiles
				tell aFile to set end of theTableViewContents to {its name, it as text}
			end repeat
		end tell
		tell window "merger"
			set content of table view 1 of scroll view 1 to theTableViewContents
		end tell
	end if
	set allows reordering of table view 1 of scroll view 1 of window "merger" to true
	set allows multiple selection of table view 1 of scroll view 1 of window "merger" to true
...

My second question - now that these files are reordered in the table, how do I reference these files to perform other tasks? Prior to the setting up the table, files were selected from the path as outlined in the first part of the script and the task was initiated immediately thereafter. For instance, this is one of the tasks that I need to do:


	...
tell application "Finder" to set files_ to files of folder_ as alias list  -- this now needs to reference the contents of table
	--set files_ to items 1 thru 2 of files_
	try
		set file_ref to open for access output_ with write permission
		
		repeat with file_ in files_
			set text_ to read file_
			write (text_ & return) to file_ref starting at eof
		end repeat
		try
			close access file_ref
		end try
	on error e
		display dialog e
		try
			close access file_ref
		end try
	end try
...

The issue with reordering multiple items seems to be solved. Now, I need to figure out how to make reference to the updated data rows in the table. I tried this but, to no avail:


on merge()
	set d to (make new data source at end of data sources)

	tell application "Finder" to set files_ to files of d as alias list
...

Somehow, this is not grabbing the data in the table.

Hi, marlon4417.

‘Table views’, ‘data rows’, and ‘data sources’ don’t occur in vanilla AppleScript. If you need help scripting an application that has them, you need to say what that application is and what you’re trying to do, then people might understand your questions. :confused:

I’m quite sure the OP is talking about AppleScript Studio & Xcode

Sorry Nigel,

Being a novice in applescript, I am probably using these terms loosely and incorrectly. My apologies. Let me summarize what I need to do.

I have a table that contains several rows of data, which are files. These files were reordered to my liking and now I need to merge data from each file sequentially. Prior to setting up a table, I would run this script (which asked for the files, ask for the output file name and then ran):



on Merge()
	
	set folder_ to choose folder with prompt "Select the folder containing files to be merged." default location (path to desktop)
	set output_ to (choose file name with prompt "Choose a name and location for the merged file.")
	showpanel()
	tell application "Finder" to set files_ to files of folder_ as alias list
	set files_ to items 1 thru 2 of files_
	try
		set file_ref to open for access output_ with write permission
		
		repeat with file_ in files_
			set text_ to read file_
			write (text_ & return) to file_ref starting at eof
		end repeat
		try
			close access file_ref
		end try
	on error e
		display dialog e
		try
			close access file_ref
		end try
	end try
	hidepanel()
	beep
	success()
	
end Merge


The merge() was triggered with an onclicked theObject. Now, when merge() is called, the files are already stored in the table and reordered. I just don’t know how to reference the files in the table. I don’t know if this any clearer. My apologies.

Thanks.

Moved to correct forum.

I’d appreciate some help with this issue…thanks.

Hi,

I react on your direct off-list mail, but I answer it here in the forum.

Question: How to select the row inside the table and get the path from the file you have in there.

My example is directly copied from one of my own scripts dealing with images, but the principle is the same. I did remove a lot of other stuff that is doing all kind of other things.

First you have some function that fills your table.
I have a table view inside a scroll view inside a window. A thumbnail is placed in the first column cell and the path is placed in the second column cell. If necessary you could hide this column, but my users like to see it (and so do I myself).
Look at how they are named and called.

	if (count of theFiles) > 0 then
		set the_table to table view "files" of scroll view "files" of window "ImgFuserWindow"
		set theDataSource to data source of the_table
		-- Delete all of the data rows in the data source
		delete every data row of theDataSource
		tell theDataSource
			if (count data columns) = 0 then
				make new data column at end of data columns of theDataSource with properties {name:"image"}
				make new data column at end of data columns of theDataSource with properties {name:"path"}
			end if
		end tell
		
		-- Turn off the updating of the views
		set update views of theDataSource to false
		
		-- Delete all of the data rows in the data source
		delete every data row of theDataSource

		-- For every item in the list, make a new data row and set it's contents
		repeat with theItem in theFiles
			set theItem to POSIX path of theItem
			set OneImage to quoted form of POSIX path of theItem
			set theDataRow to make new data row at end of data rows of theDataSource
			set contents of data cell "image" of theDataRow to (load image table_thumb)
			set contents of data cell "path" of theDataRow to theItem
			set update views of theDataSource to true
		end repeat
	end if -- End "(count of theFiles) > 0" check

In the Interface Builder you select your table. Have your inspector tool open on the Applescript part.
Double-click the table. This will bring you to the Scrollview Applescript properties.
Double-click the table again. Now the inspector will show you the NSTableView properties. In the properties part of the Applescript options set the Action->Clicked option to true (check the check box).

Now in your script you define the:
on clicked theObject
end clicked


if name of theObject is "files" then
	if (get clicked row of theObject) is 0 then
		display dialog "no row selected"
	else
		if (count data rows of data source of table view "files" of scroll view "files" of window "ImgFuserWindow") is greater than 0 then
			set theSelectedRow to selected data row of table view "files" of scroll view "files" of window "ImgFuserWindow"
			set thePath to contents of data cell "path" of theSelectedRow as Unicode text
			set image of image view 1 of window "ShowSourceImage" to load image thePath
			if visible of window "ShowSourceImage" = false then show window "ShowSourceImage"
			set title of window "ShowSourceImage" to thePath
		end if
	end if
end if

Now the statement “set thePath to contents of data cell “path” of theSelectedRow as Unicode text” tells you how I accessed the path to the file (image here) in the table. You can also see how I load the image. You can use some other option to load your text files, but I assume you have already figured that out.

I put the silly “if (get clicked row of theObject) is 0 then” in this example in there on purpose. If you do not handle this situation correctly your applescript will crash, instead of doing nothing. My actual script deals with this on a completely different place that would make the example more fuzzy.

I managed to get this working - thanks in a big way to ief2. With the creation of a handler that controlled the aliases, this worked great:



property mainTableView : null
global mainDataSource
-------
set myFolder to choose folder with prompt "Choose the folder containing the files" default location (path to desktop) without invisibles
		tell application "Finder" to set allFiles to every file of myFolder
		delete every data row of mainDataSource
		repeat with i in allFiles
			set i to i as alias
			set newRow to make new data row at end of data rows of mainDataSource
			set content of data cell "Name" of newRow to name of (info for i)
			set content of data cell "POSIX" of newRow to POSIX path of i
		end repeat
		set allows reordering of mainTableView to (count allFiles) > 1
		set allows multiple selection of mainTableView to (count allFiles) > 1
		set enabled of button "merge" of window "merger" to (count allFiles) > 1
	--------------------------------

on aliasesOfMainDataSource()
	set allDataRows to every data row of mainDataSource
	
	set allAliases to {}
	repeat with i in allDataRows
		set myPath to (content of data cell "POSIX" of i)
		set myPath to (POSIX file myPath) as alias
		set end of allAliases to myPath
	end repeat
	return allAliases
end aliasesOfMainDataSource



Tables are not for the beginner. Again, a big thank you to ief2 and Harry for the guidance.