Contents of an empty text field / Multiple script files

I have 2 questions:

  1. When I try to get the contents of a text field that is empty I get no result. How to I check for this in my script? example:

set myVar to contents of text field “Blerf” of window “Main”
if myVar > 0 then … (* handle accordingly *)

The error I get is that myVar hasn’t been defined.

  1. If I have more than one Applescript file in a project, can handlers in one file be referenced (called) from the other file?

Thanks in advance,
Brad Bumgarner, CTA

I have no experience with AppleScript Studio but what happens if you use something like:

set myVar to ""
set myVar to contents of text field "Blerf" of window "Main"
if myVar is not "" then ... (* handle accordingly *)

I don’t know within the realm of Studio apps but it seems that you should be able to use ‘load script’ and then call the handlers as if they were within the calling script.

This error jumps usually when, effectively, “myVar” hasn’t been defined. You may check your window’s project and make sure the object you are referring IS a text field (not, eg, a text view into a scroll view), the object IS WHITHIN the window “Main” (not, eg, into a tab view or a group box, whithin window “main”), object names ARE “Blerf” and “Main”… And such stuff, which usually takes most of the time when developing in ASS :x

Rob & jj:
I have set myVar to “” to initialize it. Same result. :?

jj:
I have checked what you suggested. I DO run into those problems, so that is always the first thing that I check. (Thanks for the reminder though.)

Thank you both for your responses. I am still looking at what I can do to “idiot proof” my application. Truth is, I’m likely to be the only user–still it doesn’t hurt to do things right!

Thanks,
Brad Bumgarner, CTA

Brad,

Make sure you’re comparing apples to oranges, so to speak.
In your first example above, you wouldn’t likely be able to use a ‘>’ operand in your conditional statement if said text field doesn’t yet have anything in it.
And even if it does, it would need to be a number of some kind to be able to do a greater-than conditional, no?

Also, let me take this opportunity to reiterate OBJECT HIERARCHY.
Be sure your object tree is correct. ie.:

If the text field is WITHIN a tab view item, within a tab view, within a window, it is imperitive that all of those objects be represented in your statement like so:

set myVar to text field "foo" of tab view item "bar" of tab view "kung" of window "fu"

And, as JJ said, make sure you’re talking to a text field and not some other type of text-container.

Post again with more complete code if you need further help. I’m sure we can figure it out and get you on your way.
:wink:

I have just confirmed that I am using a text field (NSTextField). I have a number formatter attached to it, so it does return a number.

I have created a basic window. I am not using any tabs. I have learned the hard way about “the reference to the reference to the reference, etc., etc.” in applescript. In the beginning (about 5-6 years ago), that slowed me down more than anything. I KNEW what I was referring to, why didn’t the computer? :wink:

Thanks again,
Brad Bumgarner, CTA

An additional thought: I will try checking for a “” condition BEFORE checking the number value.

Have you tried retrieving info from outside of your app? Try this from any script editor:

tell app "Whatever called your app"
     properties of text field "Blerf" of window "Main"
end tell

Defiitivelly, if you don’t receive its properties, you’re not targetting OK such text field… But if you do, what do you see in property “contents”? :idea:

EXCELLENT SUGGESTION!! :smiley:

When the text field was empty the contents show up as “missing value”, however, the another property, double value returned 0.0 (as a number). Instead of pulling the contents, I will now pull the double value. Works for an empty text field as well as a text field with info. Next step, confirm that the formatter handles alpha character input properly (i.e. not allow it–I need dollor amounts in this field).

Thanks for all the help and suggestions,
Brad Bumgarner, CTA