Hey all, long time lurker and now I’ve come to the point to where I need your help. I’ve tried reading around and looking at the example apple provides in the developer folder but I cannot figure this out.
What I am trying to do:
I am trying to create a blank NSTableView window (called batch in IB) and add different file paths to it with an add button that opens up a file dialog. Then once the user is done selecting files I want to proform a function on them one by one and have them cleared off the list in NSTableView once one of them is done and go to the next. I am just trying to get them to add to the list then I will try to tackle the rest.
if name of theObject is "addtobatch" then
set app_filepath to (path to me) as string
--set app_filepath to extract_parent_folder_path_from(app_filepath)
set pospath to POSIX path of app_filepath
set theWindow to window of theObject
set commandstart to pospath & "Contents/Resources/"
set inpath to (choose file with prompt "Choose movie file:") as string
set inpath to POSIX path of inpath
tell theWindow
--display dialog inpath
set batch to batch & {batch:inpath}
tell table view "batch" of scroll view "batch" to update
end tell
end if
I get the generic NSReceiverEvaluationScriptError: 4 (1) error and that’s why I’ve posted the code.
Thanks for all your help. This one just has me puzzled!
I am just trying to write a script to take a file path and add it to the tableview and then be able to pull them off one at a time. I just cannot figure it out. Can anyone lend any help?
It’s easier to understand using a data source. The ‘display open panel’ might be better also. It has better options than the standard additions ‘choose file’. Here’s like a basic template I use for the data source. The ‘awake from nib’ handler is connected to the table view. The button’s name is ‘add’ and is connected to the ‘clicked’ handler. The script property tableData is not used here.
property tableData : {}
on awake from nib theObject
set n to name of theObject
if n is “table” then
set theDataSource to make new data source at end of data sources with properties {name:“files”}
make new data column at end of data columns of theDataSource with properties {name:“path”}
set data source of theObject to theDataSource
– init the data source
– append theDataSource with tableData
end if
end awake from nib
on clicked theObject
set n to name of theObject
if n is “add” then
set allows multiple selection of open panel to true
set r to (display open panel)
if r is 1 then
set f_list to path names of open panel
repeat with this_file in f_list
append data source “files” with {{this_file}}
end repeat
end if
end if
end clicked
that appears to be an issue, the log n is not adding anything to the log. so it looks like they are not connected… oy vei.
Any assistance with that? Then I swear I am done asking questions for a while. It seems this NSTableView stuff is not the easiest to grasp.
Yeah, a common mistake is to connect the handler to the scroll view instead of the table view. You have to double click on the table to get to the table view. The table view is automatically placed in a scroll view. Later on, if you need to use a reference to the table, then it might look like this:
table view “table” of scroll view “scroll” of window “main”
It’s good to give everything a name in the AppleScript pane of the info window. Even name the table column.
Use the ‘delete’ command. In the previous example, I added a button named “delete” and connected it to the same script’s clicked handler. Here’s the clicked handler part that will delete the selected data row when you hit the button.
on clicked theObject
set n to name of theObject
if n is “add” then
set allows multiple selection of open panel to true
set r to (display open panel)
if r is 1 then
set f_list to path names of open panel
repeat with this_file in f_list
append data source “files” with {{this_file}}
end repeat
end if
else if n is “delete” then
set s to selected data row of table view “table” of scroll view “scroll” of window “main”
delete s
end if
end clicked
Note that you should check if a row is selected or not. This is just raw stuff to get you started.
Thanks! I tell you they should make this table stuff a bit more like realbasic or the arrays in C which I am used to.
So I’ve gotten what you’ve provided working but I need for the computer to automatically select the first entry in the list automatically so I’ve tried to code it and I get some strange log errors.
else if n is "encode" then
set theBatch to table view "batch" of scroll view "batch" of window "Window"
set selected row of theBatch to {1}
--set s to selected data row of table view "batch" of scroll view "batch" of window "Window"
--delete s
end if
[29293] *** -[NSCFArray intValue]: selector not recognized [self = 0x31fb10]
Or possibly easier I might just try to pull the paths off of the table one at a time top to bottom.
else if n is "encode" then
set theBatch to table view "batch" of scroll view "batch" of window "Window"
set allows multiple selection of theBatch to true
set theRow to 1
set nextbatch to contents of data cell "path" of theRow
The value of the property ‘selected row’ of the table view object is integer. This is from the dictionary:
selected row integer – the index of the selected row
Instead you used {1} which is a list with one item. So instead of:
set selected row of theBatch to {1}
you could have done this:
set selected row of theBatch to 1
or you can select multiple rows:
set selected rows of theBatch to {1,3}
selected rows list – the indices of the selected rows
In the next section you posted:
set nextbatch to contents of data cell “path” of theRow
Actually, I don’t know what you’re trying to get here, but the reference is incomplete or wrong. If you wanted to get the value of the cell of row 1
set nextbatch to content of data cell “path” of data row 1 of data source “files”
This is confusing huh? When in doubt, look at the dictionary for the data type of a property. ‘content’ gets the value of an object that has this property while contents gets a reference to the object most of the time. You might want to experiment a little and look at the examples in the reference guide and those provided in AppleScript Studio.