Lists,records,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary

Hi,

I have been working my way through an example of binding two tables together and although I have followed the instructions carefully I keep getting an error concerning NSCFArray error about the object not being created.

I think it may have to do with how the array is formed but cannot solve it.

Can someone advise me of the correct way to form an array and a dictionary:

I have tried:


tell class "NSArray" of current application to set pKeys to its arrayWithObjects_("title", missing value)
tell class "NSArray" of current application to set pValues to its arrayWithObjects_("New Account", missing value)
tell class "NSMutableDictionary" of current application to set pProperties to its initWithObjects_forKeys_(pValues, pKeys)
tell class "NSMutableArray" of current application to set pTransactions to its alloc's init()

also


set pKeys to NSArray's arrayWithObjects__("title", missing value)
set pValues to NSArray's arrayWithObjects_("New Account", missing value)
set pProperties to NSMutableDictionary's initWithObjects_forKeys_(pValues, pKeys)
set pTransactions to NSMutableArray's allocs's init()

also


set pProperties to {title:"New Account"}
set pTransactions to {}

Are Applescript lists fully interchangeable with NSArray and NSMutableArray?

Are Applescript records fully interchangeable with NSDictionary and NSMutableDictionary?

All the best

Terry

Hi Terry,

This is the example project I created with Shane’s help for binding two tables without using any NSMutableArray stuff, just a simple record. So far it seems to work. There are only a few lines of code but the settings for the bindings have to be EXACTLY as they are for it to work. I would be glad to help walk you through it.

Don’t know if that is what you want…

http://rdutoit.home.comcast.net/~rdutoit/pub/two_bound_tables_sample.zip

Rob

They’re fine. But these aren’t:

tell class "NSMutableDictionary" of current application to set pProperties to its initWithObjects_forKeys_(pValues, pKeys)
tell class "NSMutableArray" of current application to set pTransactions to its alloc's init()

The problem is you’re trying to use instance methods on classes. Try these forms:

		tell class "NSMutableDictionary" of current application to set pProperties to its dictionaryWithObjects_forKeys_(pValues, pKeys)
		tell class "NSMutableArray" of current application to set pTransactions to its arrayWithCapacity_(10) -- number doesn't really matter

I’m not sure of the exact answer to that, but in most cases I’ve found that passing a list or record where an array or dictionary is required has worked fine.

Rob,

Thanks for your observations. I have downloaded your example and I understand how it works.

However, I have been doing an ASOC conversion of the eaxmple bindings project referred to in other threads and obtained here:

http://cocoadevcentral.com/articles/000080.php

I have followed the example exactly converting to ASOC statements and have failed to make it work.

I think my next step is to follow the instructions in the example above but using just Objective-C and see if it works, then take it from there.

All the best

Terry

Hi Shane,

Why is this not allowed in ASOC:


tell class "NSMutableDictionary" of current application to set pProperties to its initWithObjects_forKeys_(pValues, pKeys)

is it not the equivalent of :


NSMutableDictionary *pProperties = [[NSMutableDictionary alloc] initWithObjects:pValues forKeys:pKeys];

or should I have said:


tell class "NSMutableDictionary" of current application to set pProperties to its alloc's initWithObjects_forKeys_(pValues,pKeys)

or perhaps


set pProperties to NSMutableDictionary's alloc's initWithObjects_forKeys_(pValues,pKeys)

All the best

Terry

Yes, you have to “alloc” before you “init.” These are two separate messages but it is normal to include them in one statement.

Hi Terry,

Yes I started by adding the source files from the Cocoa Dev project into my ASOC project. Everything worked great with the bindings except we couldn’t figure out how to add something manually to the table. The call to the init methods in the OBJ-C class files wouldn’t work. Craig helped me set it up.

In the end Shane’s simple ASOC example worked and so I am going with that especially since it requires 6 fewer class files! If Shane’s idea hadn’t worked, the next step was to convert the Cocoa project to ASOC but without using bindings. Instead it was old “glue code.” I am still interested in seeing how one would set it up ASOC using the glue code, or the bindings method. Let me know if you succeed in doing that.

I have the project using the OBC-C files if you want it.

Rob

Hi,

I am currently working through converting the code to ASOC without bindings and will confirm back the outcome.

All the best

Terry

I am still working on the example with bindings (one last attempt) and still get the following error in the console.

02/10/2009 17:08:05 THAccounts[310] *** -[THAccount init]: The variable NSArray is not defined. (error -2753)
02/10/2009 17:08:05 THAccounts[310] Failed to create new object

Can anyone make sense of it in relation to the script below.

Thanks

Terry


script THAccount
	property parent : class "NSObject"
	
	property NSArray : class "NSArray"
	property NSMutableDictionary : class "NSMutableDictionary"
	property NSMutableArray : class "NSMutableArray"
	property NSString : class "NSString"
	
	property pProperties : missing value
	property pTransactions : missing value
	property pKeys : missing value
	property pValues : missing value
	
	
	on init()
		continue init()
		set pKeys to NSArray's arrayWithObjects_("title", missing value)
		set pValues to NSArray's arrayWithObjects_("New Account", missing value)
		set pProperties to NSMutableDictionary's dictionaryWithObjects_forKeys_(pValues, pKeys)
		set pTransactions to NSMutableArray's arrayWithCapacity_(10)
		return me
	end init
	
	on |properties|()
		return pProperties
	end |properties|
	
	on setProperties_(newProperties)
		if pProperties ≠ newProperties then
			set pProperties to NSMutableDictionary's dictionaryWithDictionary_(newProperties)
		end if
	end setProperties_
	
	on transactions()
		return pTransactions
	end transactions
	
	on setTransactions_(newTransactions)
		if pTransactions ≠ newTransactions then
			set pTransactions to NSMutableDictionary's dictionaryWithDictionary_(newTransactions)
		end if
	end setTransactions_
	
end script

To refer to properties in your script, you need to say “my whatever”. And your class properties should be declared outside the script object – they need to refer to classes of the application, not classes of the script object.

Hi all,

I am still playing with the example with bindings,

I cannot see where it is going wrong.

If anyone would care to take a look the project can be found here:

http://www.theaford.co.uk/Site/ApplescriptObjC_files/THAccounts.zip

All the best

Terry

Hi again,

I seem to have stumbled upon a solution to the example above. It is the same example but with different variables/tables etc.

I have commented out the sections that did not work so you can see what did.

If you are interested you can download it here:

http://www.theaford.co.uk/Site/ApplescriptObjC_files/THAccounts(working).zip

All the best

Terry

Thanks Terry,

I was looking for a way to set up the tables using arrays instead of a record. You went for bindings in the end, it looks like.

Rob

Hi Terry,

Thanks again for the great translation of the two tables from the oBJ-C example. Are you going to work out the archiving too?

Also I was wondering if you tried it first with the datasource methods or just go for the bindings straight up?

I do like my version which is very simple and uses the applescript record but am having some issues with the images in the table. Also, I need drag and drop for the table and you need datasource methods for that. I may tinker with mine some more , or yours and see if I can use the datasource too for the needed methods. This may also solve the image thing.

In the Dev example you used, Scott Stevenson says you can mix and match bindings with data source methods. I have seen this online and others mention it - so here I go.

Great job done with this!

Rob