missing value, nil, null, true, false

Hi,

Could someone please explain for my benefit and perhaps others the relationship of missing value, nil, null, true and false and how they are used within ASOC calls into or items returned from Cocoa.

For example, a large number of Cocoa calls return nil in the Objective-C environment. What do these calls in ASOC return, missing value?

Thanks in advance

Terry

From an ASObjC point of view, nil and NULL are the same and both equate to missing value. Objective-C uses YES and NO for true and false, and they also equate to 1 and o.

The reason I asked this question is because the following did not beep when supplied with a filename that was not pointing to an image, in fact I passed in a string of “” for the filename. Perhaps the string “” should be converted to an NSString?



set pImage to NSImage's alloc()'s initWithContentsOfFile_("")

if pImage = missing value then
beep
end if


Any ideas

Thanks

Terry

“” is not null/nil/missing value it has a string value of nothing, not a value of nothing. Try

if pImage is in {"",missing value}

Perhaps being an image the rules are different but pImage is a value that happens to contain nothing (or a framework for an image that doesn’t exist)… My best logical guess.
What happens passing the init missing value rather than “”?

Hi,

I must apologise the code as follows does in fact work, there was a mistake in my drawRect code of the NSView containing the NSImage:


	on setImageWithPath_(tImagePath)
		set pImagePath to NSString's stringWithString_(tImagePath)
		set pImage to NSImage's alloc's initWithContentsOfFile_(tImagePath)
		
		if pImage = missing value then
			beep
		end if	
	end setImageWithPath_

All the best

Terry

You probably don’t need the stringWithString_ line. You should only need to explicitly make an NSString when you want to run one of the NSString methods on it, otherwise a normal AS string will be fine.

Hello,

How can nil and null have the same (missing) value as “nil” is not an ASOC keyword (in XCode 4)?

But I suppose it’s OK to put at the beginning of the script

and then, everywhere in code, use this “nil” without problem?

In fact, cocoa does “intercept” calls to nil to prevent the program going to the fourth dimension? What is nil? A pointer to 0? Just wondering.

A few minutes with Google will tell you all you want to know, and much more. Essentially nil is used for object pointers, and NULL for C-style pointers. Just like the scripting bridge converts both C-style ints and NSNumbers containing ints to the same thing in AS, so NULL and nil are translated as missing value.

nil, Nil and NULL are eventually the same but you have to use Nil and nil for objects. Nil and nil are __DARWIN_NULL and NULL and __DARWIN_NULL are the same so they are all ((void *)0) which is a pointer to 0. So yes nil is a pointer to 0.

Equivalent or not, missing value is the sole term recognized for IBoutlets. If you define:

property mainWindow : null (or nil, or Nil)

mainWindow is not considered as an outlet. So in this particular case, we have no choice. But it’s OK for

property nil : missing value
.
property myRecord {aValue : nil, anotherValue : nil}
.
if mainWindow ≠nil then.

No and because there is in fact no IBOutlet at all defined in Objective-C either. It’s a compiler undefined variable only used for interface buider to make connections. It’s not used at runtime. So what they did was that interface builder can read ApplescriptObjC files too and interpret property variable : missing value as an BOutlet. The IBAction on the other hand is just a the same as void but again this is defined just for interface builder again. For ApplescriptObjC they made a that on IBAction_(sender) would be interpreted as an IBAction.

And then there is NSNULL.

While on the subject, I would just like to warn ASOC coders of a potential “gotchas” that I myself have stumbled on a few times:

In lists that have become NSArrays you may be certain to have supplied a ‘missing value’ as one of the elements, but when you use it, it simply won’t work, and an obscure error message quibbles about ‘NSNULL’, and you may think it is the same as missing value, since that is what you supplied. However, searching the documentation reveals:

So, NSNull is an object and not a value, so it fails on tests for ‘missing value’ (which you thought it really was).

In the log output, you may see 2 different “kinds” of null:

(null)

The first one is the NSNULL object, whereas the second one is the null value which in ASOC can be treated as ‘missing value’. They don’t equate. It’s really irritating.

One simple way to “fix” this is to simply coerce the NSArray back to a list (. as list) which resurrects ‘missing value’ from NSNull. The following illustrates it:

set a to {missing value}
set b to current application's NSArray's arrayWithArray_(a)
log b's objectAtIndex_(0)
log item 1 of (b as list)

--> <null>
--> (null)

This crutch may be ok for small stuff, but if the list is large, there is a significant performance penalty, so other methods would be preferred.

I’m sure others on this forum could provide other (better) workarounds – I just would like to highlight the potential problem, since it can be really time consuming to find out what the problem is, especially if that ‘missing value’ is very infrequent.

PS: Maybe this would be a good thing to include in Shane’s excellent book on gotchas?

Heb,

I think this is good to point this out for new asoc scripters but the problem is not applescript or NULL. Your example is just perfect because you use an NSarray as an example. The missing value that normally is converted to an nil can’t be converted because an NSArray need objects as values. Because NULL, Nil and nil aro not objects you can insert an NSNull object instead just like missing value is an object in applescript. When someone has an automatic filled list in the script there could be missing values in there. Luckily the AppleScriptObjC framework notice this and coerce the missing value to an NSNull value for us. I would say that it more like a genius feature than a problem.

The problem you’re trying to point out is nothing more than a common mistake about focussing.

  1. You create an array in applescript.
  2. tell cocoa to make a new NSArray with an coerced array (it doesn’t make a array from your list, it is coerced with AppleScriptObjC before it reaches the cocoa environment which is already an NSArray)
  3. Get the object from the array in the cocoa envorinment and pass the result through AppleScriptObjC (which is NSNull)
  4. Get the array first through AppleScriptObjC (coerced back into list) and then get the object (which is missing value)

Good point, Harald. FYI, you can make your own NSNull:

set theNSNull to current application's NSNull's |null|()