A little help with a table view & a data source

Hi guys, I’m new here and found the site when looking for some help, and found an article that gave me what I needed perfectly!

Now I am at another part of developing my app and am trying to make part of my work like the screen at the link below.
http://www.webedition-cms.com/images/content/products/The-Mamp/hosts_general-1.jpg

I need it to read & edit the data from a datasource (not set up) and then write that data into a template like this:

text variable1
text variable2

And then repeat that group for every row of data and save it as a file. Then I want the interface to show the data inside input cells when you click on the title, like in the screenshot.

I am not a genius with applescript but I am getting used to it.

Any help would be greatly appreciated and thanks in advance!

Also, if anyone knows how to make the on/off slider in the Time Machine Preference Pane of System Preferences on Leopard, that would help too! Thank you!

Have you had a look at the scripts of the example AppleScript Studio applications on your hard disk:
/Developer/Examples/AppleScript Studio
such as “Simple Table”, “Table”
etc? They should give you enough information to extract the data you want. If you get stuck, please post your specific problem and code so far.

Tom
BareFeet

Thanks! Found that a few hours ago, and now have that more or less working. But how would I get the data to save & open on relaunch?

Ok. I’ve got some parts working. I need help with this code:


set numberrows to call method "numberOfRows" of hostsDataSource
repeat with i from 1 to numberrows
	set current row of hostsDataSource to i
	set theRow to the contents of the current row of hostsDataSource
	set name to contents of data cell "name" of theRow
	set address to contents of data cell "address" of theRow
	set zip to contents of data cell "zip" of theRow
end repeat

I am trying to get it to repeat an action for the number of rows in the datasource. I get an error that says numberrows is undefined. How do I get this to work? Thanks!

Ok. I tried this too, but no luck:


set rowCount to call method "numberWithInt:" of class "NSNumber" with parameter (call method "numberOfRows" of hostsDataSource)

And I get the error: Cant make current application into type anything. (-1700)

Any help? Thank you!

Are you looking to save a preference file, is this going to be user entry? If so, then you’ll want to use user defaults with a read and write function. Just do a search for user defaults in the AS studio forum.

I am looking to save the contents of the data source to a file. That section works fine (coded it separately and it worked). Here is the current code:


		set rowCount to call method "numberWithInt:" of class "NSNumber" with parameter (call method "numberOfRows" of hostsDataSource)
		repeat with i from 1 to rowCount
			set current row of hostsDataSource to i
			set theRow to the contents of the current row of hostsDataSource
			set servername to contents of data cell "servername" of theRow
			set serverport to contents of data cell "serverport" of theRow
			set docroot to contents of data cell "docroot" of theRow
			set zip to contents of data cell "zip" of theRow
			(write to file code)
		end repeat

I am trying to get the number of rows in the data source, repeat some code for every row and in that code save the data for the cell of each column to a file (code excluded, not the problem)

I think most of your problems, as is common, stem from not reading (or not interpreting) the AppleScript dictionary for Xcode projects. You can see it listed as AppleScriptKit.sdef in your project.

You can do this in pure AppleScript (no call method needed). ie:

set rowCount to count data rows in hostsDataSource

“current row” is not a property of a data source, as you can see in the dictionary. So you can’t set or get the current row of a data source. It doesn’t make sense and is a bit of a convoluted way to do it anyway. You would instead do a loop like this:

set rowCount to count data rows in hostsDataSource
repeat with i from 1 to rowCount
	set theRow to data row i in hostsDataSource

or by defining theRowRef as a reference to each successive element in the data rows, in a repeat loop like this:

repeat with theRowRef in data rows in hostsDataSource
	set theRow to contents of theRowRef

You can’t do that, since you’re setting theRow to be a list (“contents of” returns list or record, as you can see in the dictionary) and then asking for a data cell of it (which is an element of a data source but not a list or record, as you can see in the dictionary).

But you can just leave it as a data row:

repeat with theRowRef in data rows in hostsDataSource
	set theRow to contents of theRowRef
	set servername to contents of data cell "servername" of theRow

or put the contents into a list:

repeat with theRowRef in data rows in hostsDataSource
	set rowList to contents of contents of theRowRef
	set servername to servername of rowList

Looking at the bigger picture though, if all you want to do is write the data source to a file, you can avoid the cell extraction and repeat loop altogether, by simply writing the whole thing at once:

write contents of contents of hostsDataSource to file "MyFile"

or more completely:

set hostDataList to contents of contents of hostsDataSource
set dataFile to POSIX file "/Users/Shared/saved hostDataList"
set writeRef to open for access dataFile with write permission
try
	set eof writeRef to 0
	write hostDataList to writeRef
	close access writeRef
on error errorMessage
	close access writeRef
	error errorMessage
end try

You can later read the data back from the file to a list and, if needed, dump the list back into the data source of the table by:

set readList to read dataFile as list
set contents of contents of hostsDataSource to readList

Tom
BareFeet
http://www.tandb.com.au

Hey barefeet, thank you for your help. It works perfectly now! I hadn’t seen that file. I was scrounging through the built in documentation for xcode. I am currently writing the data using user defaults to reload the data upon relaunch and write the data to a file in a specific format (that’s why I am setting variables to data cells) Would you (or anyone else) recommend using that, or write it to two files (the first, a final file in a specific format, and a data file like shown in the last bits of code)?

Thanks again for your help, skyhighmac

Great to see :slight_smile:

I assume you mean the AppleScriptKit.sdef file. It’s a little obscure to find, but when AppleScripting anything, you should be first looking for the dictionary of that program. You’d get the same dictionary if you’d chosen XCode’s or Script Editor’s “Open Dictionary” and selected your program, or dropped your program onto the Script Editor icon. Viewing the dictionary of a program is essential to writing any script for it, otherwise it’s all guess work.

I wrote an AppleScript tutorial a few years back that focussed on using the dictionary. Part of it is at http://www.tandb.com.au/applescript/tutorial/. It used AppleWorks for the exercises, but I’m thinking of updating it to use XCode, since it’s the same principle.

I guess I’d only use the user defaults if what you are writing is truly preferences for your program. If it’s more accurately a user document, then I’d save it to a file, calling the read and write routines from within the “data representation” and “load data representation” handlers.

I don’t think you need to write two files. Whether you write a specific file (like a document) or write to the preferences file, you should be able to just output the contents of the data source as is, since you’re just going to read it back into the same table again.

Tom
BareFeet
http://www.tandb.com.au

Thanks for the tutorial. I’ll have to check it out. :slight_smile:

What I am talking about is either writing to user defaults or writing to another file (I have to write to one file, no matter what, using a specific format, so I have variables set to data cells, and then I write text & those variables in certain places, nothing like the wrtie contents would give me).

Thanks again for your help,
skyhighmac