Tuesday, December 12, 2017

#1 2009-09-30 03:15:51 am

tellboy
Member
Registered: 2005-06-23
Posts: 152

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:

Applescript:


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

Applescript:


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

Applescript:


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

Offline

 

#2 2009-09-30 04:48:21 am

robdut
Member
Registered: 2009-09-02
Posts: 301

Re: Lists,records,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary

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/~rdutoi … sample.zip

Rob

Offline

 

#3 2009-09-30 06:48:00 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: Lists,records,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary

tellboy wrote:

I have tried:

Applescript:

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)


They're fine. But these aren't:

Applescript:

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:

Applescript:

       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

Are Applescript lists fully interchangeable with NSArray and NSMutableArray?

Are Applescript records fully interchangeable with NSDictionary and NSMutableDictionary?


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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#4 2009-10-01 05:02:03 am

tellboy
Member
Registered: 2005-06-23
Posts: 152

Re: Lists,records,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary

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

Offline

 

#5 2009-10-01 05:14:51 am

tellboy
Member
Registered: 2005-06-23
Posts: 152

Re: Lists,records,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary

Hi Shane,

Why is this not allowed in ASOC:

Applescript:


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

is it not the equivalent of :

Applescript:


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

or should I have said:

Applescript:


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

or perhaps

Applescript:


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

All the best

Terry


Filed under: NSMutableDictionary

Offline

 

#6 2009-10-01 05:54:22 am

Craig Williams
Administrator
From:: Ft. Smith, AR
Registered: 2006-12-07
Posts: 888

Re: Lists,records,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary

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

Offline

 

#7 2009-10-01 06:40:30 am

robdut
Member
Registered: 2009-09-02
Posts: 301

Re: Lists,records,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary

Hi Terry,

tellboy wrote:

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.
y


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

Offline

 

#8 2009-10-01 10:12:54 am

tellboy
Member
Registered: 2005-06-23
Posts: 152

Re: Lists,records,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary

Hi,

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

All the best

Terry

Offline

 

#9 2009-10-02 11:16:16 am

tellboy
Member
Registered: 2005-06-23
Posts: 152

Re: Lists,records,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary

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



Applescript:


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

Offline

 

#10 2009-10-02 06:25:12 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: Lists,records,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#11 2009-10-03 02:04:52 am

tellboy
Member
Registered: 2005-06-23
Posts: 152

Re: Lists,records,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary

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/Applescr … counts.zip


All the best

Terry

Offline

 

#12 2009-10-03 07:37:44 am

tellboy
Member
Registered: 2005-06-23
Posts: 152

Re: Lists,records,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary

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/Applescr … ing%29.zip


All the best

Terry

Last edited by tellboy (2009-10-03 07:39:15 am)

Offline

 

#13 2009-10-03 01:18:41 pm

robdut
Member
Registered: 2009-09-02
Posts: 301

Re: Lists,records,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary

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

Offline

 

#14 2009-10-03 08:50:51 pm

robdut
Member
Registered: 2009-09-02
Posts: 301

Re: Lists,records,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)