set variable to null

Hi, I'm new to Applescript, so this is a basic question.

To set a variable as NULL, do I:
set X to ""

BTW, what are good on-line tutorials for a beginner?

Thanks,
Tony

MacBook /, Mac OS X (10.6.2), / Windows xp (Boot Camp)

Posted on Dec 23, 2009 11:24 AM

Reply
8 replies

Dec 23, 2009 1:29 PM in response to Tony T1

set X to ""


This will set X to an empty string - not quite the same thing as 'null' since it still has a class ('text').

You can:

set X to null


which sets it to the 'null' class object - it still has a 'class' but no value.

To completely void a variable, use:

set X to missing value


now it doesn't have any content, not even a class. Whether or not you need this level of emptiness depends on your application.

Dec 23, 2009 4:13 PM in response to Niel

I'm testing an event, and if true, set X to "Yes". If the event is false, and if I later test if X is "Yes", the script fails (I guess as X is undefined). If I set X to "" at the start of the script, it works, but didn't know if this was the preferred way (I could have used an else set X to "No" in the test)

Dec 28, 2009 5:32 PM in response to Tony T1

Hello

When you're managing two states, a straightforward way is to use boolean true and false as the codes below. Of course you can use 'yes' and 'no', 1 and 0, etc. as you like.


set test to some item of {0, 1}
if test = 1 then
set x to true
else
set x to false
end if
return {test, x}

set x to false
set test to some item of {0, 1}
if test = 1 then set x to true
return {test, x}

set test to some item of {0, 1}
set x to test = 1
return {test, x}


As for documentation, you may read The AppleScript Language Guide.
http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangG uide/

Cheers,
H

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

set variable to null

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.