I'm Brand New to Livecode and Making a Simple Scipt

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
really34
Posts: 3
Joined: Wed Aug 19, 2020 12:44 am

I'm Brand New to Livecode and Making a Simple Scipt

Post by really34 » Wed Aug 19, 2020 12:56 am

I am making a simple script to show:

Serial Number
Hostname
Mac Address

on mouseUp

set the hideConsoleWindows to true
put empty into fld "fldOut"

put "wmic bios get serialnumber" into tCom1
put "Serial is " & line 3 of shell(tCom1) into line 1 of fld "fldout"

put "hostname" into tCom2
put "hostname is " & line 1 of shell(tCom2) into line 2 of fld "fldout"

end mouseUp


This works fine, but when I add the following to the script I get an error.
I think it might be because it is trying to interpret the PowerShell command even though it is in quotes.
What am I doing wrong here?

put "powershell "$(Get-NetAdapter -Name "Ethernet").MacAddress"" into tCom3
put "Mac Address is " & line 1 of shell(tCom2) into line 3 of fld "fldout"

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10048
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: I'm Brand New to Livecode and Making a Simple Scipt

Post by FourthWorld » Wed Aug 19, 2020 2:59 am

really34 wrote:
Wed Aug 19, 2020 12:56 am
This works fine, but when I add the following to the script I get an error.
What does the error say?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: I'm Brand New to Livecode and Making a Simple Scipt

Post by AxWald » Wed Aug 19, 2020 9:33 am

Hi,

assume here is the error:

Code: Select all

put "powershell "$(Get-NetAdapter -Name "Ethernet").MacAddress"" into tCom3
You want to put a string into tCom3, but this string contains quotes - this way the interpreter get's confused & throws an error.

2 ways to solve this:

1.) Replace all quotes but the outermost pair with " & quote & ":

Code: Select all

put "powershell " & quote & "$(Get-NetAdapter -Name " & quote & "Ethernet" & quote & ").MacAddress" & quote into tCom3
Looks confusing? ;-)

2.) Define a function to quote a string, and use this:

Code: Select all

put "powershell " & kwote("$(Get-NetAdapter -Name " & kwote("Ethernet") & ").MacAddress") into tCom3

function kwote what,sQuotes
   /* Quotes "what" with double quotes. Uses single quotes is sQuotes is true.
   - put "fortytwo" into myVar
   . put kwote(myVar) => "fortytwo"
   . put kwote(myVar, true) => 'fortytwo'   */
   
   if sQuotes is not true then put false into sQuotes  --  set default
   if not sQuotes then   --  double quotes
      return quote & what & quote
   else                  --  single quotes
      return "'" & what & "'"
   end if  
end kwote
Less confusing?

Well, concatenating correctly isn't easy at first. But it comes fast, with training. And a quoting function is essential.

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!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10319
Joined: Wed May 06, 2009 2:28 pm

Re: I'm Brand New to Livecode and Making a Simple Scipt

Post by dunbarx » Wed Aug 19, 2020 1:39 pm

Hi.

Do you see what AxWald meant? Imagine you had to use the five character string "foo" for some purpose, and the quotes had to be explicitly included with that string. You cannot, in LC say (pseudo):

Code: Select all

use the string "foo" 
Because LC will intrerpret that string as a literal, and provide only a three character string. But if you say:

Code: Select all

use the string quote & "foo" & quote
LC will pass a five character string, framed by ASCII 34.

It is the only way LC can interpret the use of what is ordinarily a unique character that defines literals as a character in its own right. The word "quote" is considered a constant.

Craig

really34
Posts: 3
Joined: Wed Aug 19, 2020 12:44 am

Re: I'm Brand New to Livecode and Making a Simple Scipt

Post by really34 » Wed Aug 19, 2020 11:15 pm

Thank You it worked!

My next question is will preopenstack have an effect on the load time of line 4 in fld out.
It takes a few seconds to get the MAC address though powershell, so I read about preopenstack.
What exactly is preopenstack intended for?


on preopenstack
put "powershell " & quote & "$(Get-NetAdapter -Name " & quote & "Ethernet" & quote & ").MacAddress" & quote into tCom4
end preopenstack

on OpenStack

set the hideConsoleWindows to true
put empty into fld "fldOut"

put "wmic bios get serialnumber" into tCom1
put "Serial is " & line 3 of shell(tCom1) into line 1 of fld "fldout"

put "hostname" into tCom2
put "Hostname is " & line 1 of shell(tCom2) into line 2 of fld "fldout"

put "wmic bios get smbiosbiosversion" into tCom3
put "Bios Version is " & line 3 of shell(tCom3) into line 3 of fld "fldout"

put "powershell " & quote & "$(Get-NetAdapter -Name " & quote & "Ethernet" & quote & ").MacAddress" & quote into tCom4
put "Ethernet Mac Address is " & line 1 of shell(tCom4) into line 4 of fld "fldout"

end OpenStack

really34
Posts: 3
Joined: Wed Aug 19, 2020 12:44 am

Re: I'm Brand New to Livecode and Making a Simple Scipt

Post by really34 » Wed Aug 19, 2020 11:15 pm

Thank You it worked!

My next question is will preopenstack have an effect on the load time of line 4 in fld out.
It takes a few seconds to get the MAC address though powershell, so I read about preopenstack.
What exactly is preopenstack intended for?


on preopenstack
put "powershell " & quote & "$(Get-NetAdapter -Name " & quote & "Ethernet" & quote & ").MacAddress" & quote into tCom4
end preopenstack

on OpenStack

set the hideConsoleWindows to true
put empty into fld "fldOut"

put "wmic bios get serialnumber" into tCom1
put "Serial is " & line 3 of shell(tCom1) into line 1 of fld "fldout"

put "hostname" into tCom2
put "Hostname is " & line 1 of shell(tCom2) into line 2 of fld "fldout"

put "wmic bios get smbiosbiosversion" into tCom3
put "Bios Version is " & line 3 of shell(tCom3) into line 3 of fld "fldout"

put "powershell " & quote & "$(Get-NetAdapter -Name " & quote & "Ethernet" & quote & ").MacAddress" & quote into tCom4
put "Ethernet Mac Address is " & line 1 of shell(tCom4) into line 4 of fld "fldout"

end OpenStack

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10319
Joined: Wed May 06, 2009 2:28 pm

Re: I'm Brand New to Livecode and Making a Simple Scipt

Post by dunbarx » Thu Aug 20, 2020 2:13 am

Hi.

Please enclose your code offerings between the code tags: "</>".

It makes them much easier to read.

Craig

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

Re: I'm Brand New to Livecode and Making a Simple Scipt

Post by AxWald » Thu Aug 20, 2020 8:53 am

Hi,

from the dictionary, bold by me:
PreOpenStack: Sent to the destination card when you open a stack.

Handle the preOpenStack message to update a card's appearance before the card appears on screen.

The preOpenStack message is sent before the openStack message. Unlike openStack, preOpenStack handlers are executed before the stack window appears. Because of this, the preOpenStack handler is a good place to put code that adjusts the size, position, and appearance of objects; the changes are made before the stack appears.
1.) Put it (and openStack!) into cd 1 of the stack where it is used. This way it's only called for this stack, and not for every stack you open.
2.) What's in preOpenStack happens before the window is drawn. So any code here will add to the "perceived startup time" of your stack.
3.) Additionally, preOpenStack is very early in the startUp order - be careful, there might be still some libraries not yet loaded!

For your problem:
Your shell calls will take a considerable time. I'd not have it run without user interaction - this just feels not good, staring at the screen, waiting for something to happen, not knowing what happens!

Instead make the user actively request the fetching of these data, with appropriate warning:

Code: Select all

Dear User!
I will now fetch some valuable system data:
- your MAC adress,
- your computer serial number,
- your hostname and
- your BIOS version.
This will take some time, please be patient!
[CANCEL]                              [OK]
Then show a busy cursor, and take your time - the user will accept it gladly.

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!

Post Reply