What's wrong with "it" ??

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
WebiWan
Posts: 22
Joined: Fri Apr 20, 2018 2:21 am

What's wrong with "it" ??

Post by WebiWan » Thu Jan 03, 2019 6:06 pm

I'm not a complete beginner, but this seems like such a simple issue that I thought this forum would be best. I have code that works and has been running on the Windows platform for a couple of years. I'm finally doing a MAC version and for some reason the same code will not run.

My business is in remote support. We have a server running the remote support software. That software makes a permanent connection to the server, which a lot of our customers are not fond of. So I wrote this program to connect and disconnect (start/stop the service) to our server at will.

First it checks the state of the connection and the GUI displays that status along with the appropriate buttons.
Then user action connects or disconnects
Then it checks the state again and the GUI displays the current status and the appropriate buttons.

Now the good news. Almost everything ran fine on the MAC with minor changes in syntax. What doesn't work is checking the status of the connection.

On both Windows and Mac I do all the work of the program by shelling out to a hidden command window/terminal. The script I use to check the state of the connection on the Mac actually works. It returns the correct information (either "yes" or "no") and it puts that information into the "it" variable. However... the following code will NEVER evaluate appropriately. It will ALWAYS evaluate to "else". I've tested it multiple different ways and it just seems this bit of code will never evaluate properly despite the correct information appearing in the "it" variable. By the way... this bit of code has not changed from Win to Mac, but I don't think that's the problem.

I am happy to provide as much of the background code as you'd like to see, such as the bash script that checks the state of the connection. But that works fine, so I don't know if you need it or not.

I'm hoping this is just some simple "Oh yeah, you have to do such and such on a mac" problem.

command cmdGetState
put the mprState of me into tvScript -- bash script stored in custom property
get Shell(tvScript)
put it into tvState -- at this point either "Yes" or "No" will appropriately be in "it" and "tvState"

if tvState is "No" then
set the visible of image connected.png to false
set the visible of image disconnected.png to true
set the visible of button btnConnect to true
set the visible of button btnDisconnect to false
set the visible of button ckShutdown to false
set the prYesNo of me to "No"
else -- This second section will run every time regardless of the state
set the visible of image connected.png to true
set the visible of image disconnected.png to false
set the visible of button btnConnect to false
set the visible of button btnDisconnect to true
set the visible of button ckShutdown to true
set the prYesNo of me to "Yes"
end if
end cmdGetState

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: What's wrong with "it" ??

Post by dunbarx » Thu Jan 03, 2019 6:21 pm

Hi.

Sounds like you are a bit farther along than "complete beginner". :wink:

What if you change:

Code: Select all

get Shell(tvScript)
put it into tvState -
to:

Code: Select all

put Shell(tvScript) into tvState
It seems as though this should not matter. You are saving "it" right away, which is always good practice. I am just wondering.
I'm hoping this is just some simple "Oh yeah, you have to do such and such on a mac" problem.
That implies that the shell call is different on a PC as opposed to a Mac. Maybe. But "it" ought not be at all.

Also, always use the code gadget like I just did. Even for small snippets, like I just did.

Craig Newman

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: What's wrong with "it" ??

Post by Klaus » Thu Jan 03, 2019 6:33 pm

Did you compare the result of your shell commands character by character?
There MUST be some difference on the two platforms! Maybe some hidden characters an LF or wahtever?
If that is the case, you may workaround with:
...
if word 1 of tvState is "No" then
...

BTW: Your script shows that there is nothing wrong with IT! :D

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: What's wrong with "it" ??

Post by bogs » Thu Jan 03, 2019 8:09 pm

I'd vote with Craig's solution, because Klaus always tells me not to rely on 'it' at all, as 'it' can change when you least expect it, even though 'it' might not in your example.

To find out for sure, though, I'd run the debugger step by step through the handler, which will take you through all the other handlers not shown above to see if 'it' is being acted upon elsewhere.

Klaus's point about possibly hidden characters is also a good one, as an addition to it, I'd test the result of the else half of the code as well, sticking something like answer quote & tvState & quote (to see if there are any additional spaces in tvState, if you don't feel like going through the debugger itself where you can just check the variable at the line after).
Image

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: What's wrong with "it" ??

Post by dunbarx » Thu Jan 03, 2019 8:34 pm

Hmmm.

Even the simplest (seeming) issues are complicated.

if Klaus is right, that there might be hidden chars within what appears to be a three char "yes" and a two char "no", you might test the length of those strings and see.

If there are indeed hidden chars, you can always strip them out, in one of several cute little ways, and then the platform will not matter.

In a not so cute little way, if those hidden chars have some actual purpose, you can always use the "platform" function to determine where you are, and adjust accordingly.

Craig

WebiWan
Posts: 22
Joined: Fri Apr 20, 2018 2:21 am

Re: What's wrong with "it" ??

Post by WebiWan » Fri Jan 04, 2019 2:43 am

Thanks everyone for chiming in. I'll take things in order:

First, Dunbarx:
Sounds like you are a bit farther along than "complete beginner". :wink:
I drifted away from coding when .NET came along. I was too damn old to jump into that pool with all those youngsters. Visual Basic kept me fairly busy for a while after that, but on a much smaller scale.

You are correct... It makes no difference if I use 'put' instead of 'get'. But it was worth a try. And I'll use the code gadget in the future.

Klaus:
if word 1 of tvState is "No" then
Ding, ding, ding! Klaus wins the tournament!! {Tell him what he's won, Bob}

Believe it or not I had checked this by clicking on the variable during debugging and in the little window that pops up there were no extra characters evident, even with stepping through one by one. I'll bet it's an LF. But the best part is, with your solution I don't even need to go figure out what the character is! ;-)

Bogs:
Thanks for your input. We'll give you half credit for this:
Klaus's point about possibly hidden characters is also a good one
Dunbarx again:
Yes, now that I think about it, I checked the code for possible hidden characters by retyping it in a text editor and putting it back into Livecode. But I may have just looked at the variable and not inspected it better. The scripts that do the work (stored in the custom properties) are different by platform obviously, so it turned out not to be a situation of one platform doing one thing and the other doing something else.

Anyway... Thanks to all of you. I really love this Livecode thing. ;-) It's making me nostalgic.

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: What's wrong with "it" ??

Post by AxWald » Fri Jan 04, 2019 10:28 am

Hi,

shell commands often return "terminal-formatted text", with spaces and CRs - so I always test if something is somewhere in the result:

Code: Select all

if "No" is in tvState then
Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

WebiWan
Posts: 22
Joined: Fri Apr 20, 2018 2:21 am

Re: What's wrong with "it" ??

Post by WebiWan » Fri Jan 04, 2019 4:41 pm

AxWald wrote:
Fri Jan 04, 2019 10:28 am
Hi,

shell commands often return "terminal-formatted text", with spaces and CRs - so I always test if something is somewhere in the result:

Code: Select all

if "No" is in tvState then
Have fun!
Thanks! There are so many ways to accomplish the silly task that I totally missed. I'm happy you folks are here to set me straight. :-)

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”