Sunday, July 22, 2018

#1 2014-12-18 03:23:38 pm

KCScott
Member
Registered: 2010-08-04
Posts: 5

Test if variable is defined.

Unlike other scripting languages I have used, Applescript does not have an isDefined() function. After much searching and struggling with this, I came up with a solution—pretty basic, but not immediately obvious to me.

Applescript:


   try
       someVar
   on error errmsg number num
       if num is -2753 then
           set someVar to "defaultValue"
       end if
   end try

I hope this is helpful to someone. I would welcome a more elegant solution if anybody has one. Unfortunately, I don't have a way to use it as a function.

Last edited by KCScott (2014-12-18 03:32:36 pm)

Offline

 

#2 2014-12-18 05:54:12 pm

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Test if variable is defined.

Hello.


I think the morale here is that you should always assign a value, even if that value is missing value , and test for that, or null if your prefer that one as "undefined". You then may test for undefined.

An undefined value may arise, if a variable is assigned the result of a function, that has just the return statement, like so:

Applescript:


to undefine()
return
end undefine
set a to undefine()

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)