Confused to why a script works for one KM variable and not the other KM variable

Hi,

I have two Applescripts doing the same thing but when I try to save the second Applescript as a Variable to KM Applescript returns an error message. The First script works perfectly.

First Script:

tell application "Daylite"
   eval "(selectedObjects lastObject) name."
end tell

tell application "Keyboard Maestro Engine"
   make variable with properties {name:"Client Name", value:result}
   delay 0.05
end tell

First Script Result

tell application "Daylite"
   eval "(selectedObjects lastObject) name."
      --> "Adrian Lee"
end tell
tell application "Keyboard Maestro Engine"
   make with properties {name:"Client Name", value:"Adrian Lee"} new variable
      --> variable "Client Name"
end tell

Second Script

activate application "Daylite"
tell application "System Events"
   tell process "Daylite"
      get value of text area 1 of UI element 1 of UI element 2 of UI element 1 of UI element 3 of UI element 1 of scroll area 2 of splitter group 2 of splitter group 1 of group 1 of window 1
      delay 0.05
   end tell
end tell

tell application "Keyboard Maestro Engine"
   make variable with properties {name:"Client Mobile Number", value:result}
   delay 0.05
end tell

Second Script Result: The Error Message:

tell application "Daylite"
   activate
end tell
tell application "System Events" to ¬
   get value of text area 1 of UI element 1 of UI element 2 of UI element 1 of UI element 3 of UI element 1 of scroll area 2 of splitter group 2 of splitter group 1 of group 1 of window 1 of process "Daylite"
   --> "(976) 444-4444 x234"

Result:
error "The variable result is not defined." number -2753 from result

Can anybody assist me? I can’t seem to identify where this has gone wrong or what i need to do to overcome this issue. Both scripts work until i try to make the second one a KM variable

Thank you

Neither script sets the AppleScript variable named result. The first script manages to get a result because the last action executed returned a result (namely the eval). The second script's last action is delay which does not return a result, hence the error.

Instead, use set theResult to eval … and set theResult to get value …, and then make the variable using theResult instead of the implicit result which is just asking for trouble.

Hey There,

Peter has covered this, but I'll add a couple of points.

When posting code please use the pre-formatted-code button in the editor (Ctrl-K) to make it more readable.

When writing AppleScript never use the result variable, as Peter mentions this is just asking for trouble.

You often end up with side effects you haven't imagined, and the code doesn't show it — as in your case where 'delay' eats the previous result.

I learned 20 years ago that using 'result' makes debugging vastly more difficult, and it makes the code you've written less readable.

--
Best Regards,
Chris

Thank you

Thanks, i managed to resolve this. As always your very helpful.