I have a table view with an image cell and a text cell. This table view supports drag&drop from image files I select and drop onto the table. The images load fine, but I get an error for every text cell. Due to the try statement it fills my table with images but leaves the text cell empty.
the error is: error: Can’t get <> 2 of <> id 16 of <> id 5. Invalid index.
If I make it a one cell/column table with either an image or a text cell it works fine. With multi-column tables it doesn’t.
on drop theObject drag info dragInfo
-- Get the list of data types on the pasteboard
set dataTypes to types of pasteboard of dragInfo
-- We are only interested in "file names" data types
if "file names" is in dataTypes then
-- Initialize the list of files to an empty list
set theFiles to {}
set ImageList to ""
-- We want the data as a list of file names, so set the preferred type to "file names"
set preferred type of pasteboard of dragInfo to "file names"
-- Get the list of files from the pasteboard
set theFiles to contents of pasteboard of dragInfo
set CountOfFiles to (count of theFiles)
tell window "main"
set the contents of text field "CountOfFiles" to CountOfFiles
end tell
-- Make sure we have at least one item
if (count of theFiles) > 0 then
--- Get the data source from the table view
set theDataSource to data source of theObject
-- 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
try
set theDataRow to make new data row at end of data rows of theDataSource
set contents of data cell 1 of theDataRow to (load image theItem)
set contents of data cell 2 of theDataRow to theItem
on error the_error
display dialog "error: " & the_error
end try
end repeat
-- Turn back on the updating of the views
set update views of theDataSource to true
end if
end if
-- Set the preferred type back to the default
set preferred type of pasteboard of dragInfo to ""
end drop
I am taking a guess. Reading the error message it says “can’t get data column 2 of data row id 16 of datasource id 5”. Which sounds to me like the script can’t access the second data column.
So my question to you is, have you set up your data source to have 2 columns in the “will finish launching” or “awake from nib”?
I do something like this:
on will finish launching theObject --when app fully appeared
set myDoneFilesList to data source of table view "listInner" of scroll view "listOuter" of window "main"
tell myDoneFilesList
make new data column at the end of the data columns with properties {name:"name"}
make new data column at the end of the data columns with properties {name:"files"}
make new data column at the end of the data columns with properties {name:"errors"}
make new data column at the end of the data columns with properties {name:"message"}
end tell
So your data source, even though empty, knows it will have, in my case 4 columns.
Maybe you have done this and the problem is not this.
I had in “on will open” the following declaration.
-- Set up the theDataSource so that the rest will be simpler
set theDataSource to data source of table view "files" of scroll view "files" of window "main"
-- Here we will add the data columns to the data source of the images table view
tell theDataSource
make new data column at the end of the data columns with properties {name:"files"}
make new data column at the end of the data columns with properties {name:"paths"}
end tell
I moved that to “will finish launching” and than to “awake from nib”, but that didn’t help.
This (sub)script allows you to drag and drop a set of images onto the table view which then displays the image (a thumbnail) in the first column and the pathname and file in the second column.
In interface builder create a 2 column table view, set the row height somewhere to 50-75 (or whatever height related to the thumbsize you want). Open the inspector panel and for this specific script set the name of the scroll view to “files” in the Applescript sub pane and also set the name of the table view to “files”. Name the first column “image” and the second column “path” (the headers). Drag an image cell onto the first column.
Set checkbox values to true for “drop” in “drag&drop” and for “awake from nib” in the Inspector panel sub-pane Applescript settings for the table view (not the scroll view and not the columns)
on awake from nib theObject
-- Create the data source for the table view
set theDataSource to make new data source at end
set data source of table view "files" of scroll view "files" of window "main" to theDataSource
-- Register the "file names" drag types
tell theObject to register drag types {"file names"}
end awake from nib
on drop theObject drag info dragInfo
-- Get the list of data types on the pasteboard
set dataTypes to types of pasteboard of dragInfo
-- We are only interested in "file names" data types
if "file names" is in dataTypes then
-- Initialize the list of files to an empty list
set theFiles to {}
-- We want the data as a list of file names, so set the preferred type to "file names"
set preferred type of pasteboard of dragInfo to "file names"
-- Get the list of files from the pasteboard
set theFiles to contents of pasteboard of dragInfo
set CountOfFiles to (count of theFiles)
tell window "main"
set the contents of text field "CountOfFiles" to CountOfFiles -- This is a hidden text field I also need in the other scripts
end tell
-- Make sure we have at least one item
if (count of theFiles) > 0 then
--- Set the data source for the table view
set theDataSource to data source of table view "files" of scroll view "files" of window "main"
-- 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 theDataRow to make new data row at end of data rows of theDataSource
set contents of data cell "image" of theDataRow to (load image theItem)
set contents of data cell "path" of theDataRow to theItem
end repeat
--set update views of theDataSource to true
end if
end if
-- Set the preferred type back to the default
set preferred type of pasteboard of dragInfo to ""
return true
end drop
Note: my original script is much bigger and more complicated and only part of a set of applescripts. It also contains error checking to see whether they are really image files. Next to that a lot of other actions are performed on the images via “Image Events”.
Model: MacBook 2 GHz Intel Core 2 Duo, 2GB RAM
AppleScript: 1.10 (Tiger running from external USB disk) and 2.x (Leopard)
Browser: Firefox 3.0.1
Operating System: Mac OS X (10.5)