Page 1 of 1

LiveCode Macros / Environment Variables

Posted: Sat Oct 25, 2014 11:38 am
by stoavio
I have 2 questions. The first is more general and the second is more specific about handling a certain task. I come from using AutoIT which conveniently has read-only variables for quick and easy use: https://www.autoitscript.com/autoit3/docs/macros.htm

Image

So if I need to quickly reference a user's temp folder, home directory, desktop directory, or maybe even computer name or "logged on user", I can choose the appropriate macro. Does LiveCode offer anything remotely similar to this or do I have to figure out some other way of getting this information? I'd like to be able to quick grab an environment variable without running a shell command and parsing the output back into variables, if possible.

If there isn't an easy way to get system environment variables back quickly, can someone recommend a "best practice" approach? Would you run the shell command "set"? If so, how would you proceed to store and manage that return data? Would you read it into an array and then loop through it setting each item as a variable?

Thanks!

Re: LiveCode Macros / Environment Variables

Posted: Sat Oct 25, 2014 12:41 pm
by stoavio
I just discovered

Code: Select all

specialfolderpath
which is nice! That will handle a lot of what I am trying to do. There are still other environment variables I want to get, many that are custom on certain systems. Any thoughts on getting those?

Re: LiveCode Macros / Environment Variables

Posted: Sat Oct 25, 2014 1:42 pm
by bn
Hi Stoavio,

here are some of those
platform
processor
machine
version
systemVersion
environment
screenRect
working screenRect
effective screenRect
errorDialog
exit [handlerName]
exit to top
look them up in the dictionary and have a look at "see also" in the respective entries. "see also" is often helpful to find related topics.

Kind regards
Bernd

Re: LiveCode Macros / Environment Variables

Posted: Sat Oct 25, 2014 1:54 pm
by stoavio
Hey BN -

Thanks for those!

I started some code to store the return from a shell command to an array, but I am not sure if I am storing the data correctly. I essentially want an array with 2 elements: variable (which for example would contain SystemDrive) and value (which would contain C:). I am doing this for each environment variable returned by my "set" command.

Code: Select all

   //CLI command to see system environment variables
   put shell("set") into tEnvVar
   
   repeat for each line tLine in tEnvVar
      
      //Signifies this is a line worth keeping
      if tLine contains "=" then
         set itemdelimiter to "="
         //Store variable name to array
         put item 1 of tLine into EnvVar["variable"]
         //Store variable value to array
         put item 2 of tLine into EnvVar["value"]
      else
         //Nothing
      end if
   end repeat
After doing this however I am not sure how I can see the contents of my array for each element. Can you help me out? Am I on the right track?

Re: LiveCode Macros / Environment Variables

Posted: Sat Oct 25, 2014 2:16 pm
by bn
Hey stoavio,
After doing this however I am not sure how I can see the contents of my array for each element. Can you help me out? Am I on the right track?
you are doing the array not right.

Code: Select all

on mouseUp
   //CLI command to see system environment variables
   put shell("set") into tEnvVar
   set itemdelimiter to "=" -- move this out of the repeat loop, itemDelimiter will be reset at end of handler to comma
   repeat for each line tLine in tEnvVar
      
      //Signifies this is a line worth keeping
      if tLine contains "=" then
         
         //Store variable name to array, name will be the key of the array, in this case item 1
         //Store variable value to array -- variable value is item 2 in this case
         put item 2 of tLine into EnvVar[item 1 of tLine]
      end if
   end repeat
   combine EnvVar by return and tab -- this makes a "usual" variable of the array, it is not an array anymore
   put EnvVar into field "fRes"
end mouseUp
make a field "fRes" to see the key value pairs separated by tab. Important: once you do a combine on an array the array is not an array anymore.

you could also insert a breakpoint after the end or the repeat loop and look at the array in the debugger. The debugger lets you look at keys and content.


You might also want to look up $ in the dictionary, it gives you some environment variables.

Kind regards
Bernd

Re: LiveCode Macros / Environment Variables

Posted: Sat Oct 25, 2014 2:32 pm
by stoavio
Thanks bn!

This was mostly an exercise for me to better understand arrays. One last question for you on this. Let's pretend $SystemDrive didn't exist in LC and we had to use your code to look it up from the "key value pair".

How would you go about finding SystemDrive in item 1 and displaying its corresponding value from item 2?

That might help me "close the loop" mentally on how this works.

Re: LiveCode Macros / Environment Variables

Posted: Sat Oct 25, 2014 2:44 pm
by bn
Hi stoavio,

to look something up , or get something out of an array you say

Code: Select all

put "SystemDrive" into tTemp
put EnvVar[tTemp] into field "fRes"
or

Code: Select all

put EnvVar["SystemDrive"] into field "fRes"
what is in the square brackets of an array is the key. Either provide a variable or a quoted string of the key to get at the value of the key.

You may also want to look up "the keys"

Code: Select all

put the keys of EnvVar into field "fRes"
this puts the keys of EnvVar as return delimited list into field "fRes"
BUT the keys are not ordered (not even for numeric keys), if you you want to have the keys ordered you would have to do a sort on the keys-list first.

Kind regards
Bernd

Re: LiveCode Macros / Environment Variables

Posted: Sat Oct 25, 2014 4:56 pm
by stoavio
Bernd

You're awesome, thanks for helping a new guy out! I think I was getting confused by the explanation here: http://revolution.byu.edu/arrays/introToArrays.php

Image

After viewing this graphic, I was under the impression each element (breakableStuff, coldStuff, etc.) could contain multiple items. For example, I thought this would be acceptable and each item would be stored in the "coldstuff" element without being overwritten by the one after it:

Code: Select all

put "milk" into myBox["coldstuff"]
put "butter" into myBox["coldstuff"]
put "icecream" into myBox["coldstuff"]

I guess not, huh?

Re: LiveCode Macros / Environment Variables

Posted: Sat Oct 25, 2014 5:23 pm
by stoavio
Actually, this does seem possible using "after" instead of "into":

Code: Select all

on mouseUp
   put "april" & space after Names["girl"]
   put "randy" & space after Names["boy"]
   put "bobby" & space after Names["boy"]
   put "mason" & space after Names["boy"]
   
   put Names["boy"] into BoyNames
   put BoyNames into fld "fldNames"
end mouseUp

Re: LiveCode Macros / Environment Variables

Posted: Sat Oct 25, 2014 5:37 pm
by FourthWorld
stoavio wrote:I just discovered

Code: Select all

specialfolderpath
which is nice! That will handle a lot of what I am trying to do. There are still other environment variables I want to get, many that are custom on certain systems. Any thoughts on getting those?
As you found in the Dictionary entry, most OS constants can be used with that function in addition to the mnemonic constants for the most common ones provided by LiveCode. A fairly complete list of these constants on OS X and Windows is available here:
http://www.sonsothunder.com/devres/live ... ile010.htm

Also, the globalNames function will return a list of all global variables, which includes not only those defined within LiveCode but also those defined by the system, such as $USER, $PATH, etc.

For anything else. Bernd's suggestion on shell is very helpful.

Re: LiveCode Macros / Environment Variables

Posted: Sat Oct 25, 2014 6:01 pm
by bn
Hi slavio,
I think I was getting confused by the explanation here: http://revolution.byu.edu/arrays/introToArrays.php
if you read it carefully he explains it well. Actually Devin is very good at explaining. That is what he does for a living :) Arrays are a thing you have got to get used to. They took me a while to figure out.
Actually, this does seem possible using "after" instead of "into":
you can stuff anything you like into an array, even binary data, or lists(return delimited) or items (delimited by what you decide, usually comma it nothing else is specified). You can do chunk operations on the values

e.g.

in your case you delimited by space

Code: Select all

on mouseUp
   put "april" & space after Names["girl"]
   put "randy" & space after Names["boy"]
   put "bobby" & space after Names["boy"]
   put "mason" & space after Names["boy"]
   
   put Names["boy"] into BoyNames
   put BoyNames into fld "fldNames"
   
   set the itemDelimiter to space
   put return & item 2 of names["boy"] after field "fldNames"
end mouseUp

and you can append to the content of the value as you found out. It all depends on the degree of granularity you need. If you need the list of name of boys then you are ok. Otherwise you would have to parse the list of boy names to get at an individual name.

Wait until you discover nested (mulilevel) arrays, that is where the fun begins :)

Kind regards
Bernd

Re: LiveCode Macros / Environment Variables

Posted: Sun Oct 26, 2014 4:09 pm
by bn
Hi Stoavio,

here is an example of a nested array. If you find it confusing just forget it.

I like to think of one-dimensional arrays and multi-dimensional arrays as a way to indicate an address.

Here I use a street: main street and have 2 houses on main street 52 and 53, each house has floors and on each florr lives one person.

I populate the array accordingly and there is a function to search for a name in the array.

Code: Select all

on mouseUp
   put "Stoavio" into tHouseArray["main street"]["53"]["first floor"]
   put "Bernd" into tHouseArray["main street"]["53"]["second floor"]
   put "Klaus" into tHouseArray["main street"]["53"]["third floor"]
   put "Simon" into tHouseArray["main street"]["52"]["first floor"]
   put "Jean-Marc" into tHouseArray["main street"]["52"]["second floor"]
   
   ask "who are you looking for"
   if it is "" then exit mouseUp
   put it into tPerson
   
   put findAPerson (tHouseArray, tPerson) into field "fRes"
end mouseUp

function findAPerson pArray, pWhom
   put the keys of pArray["main street"] into tHouseNumberArray
   repeat for each line aHouseNumber in tHouseNumberArray
      put the keys of pArray["main street"][aHouseNumber] into tFloorArray
      repeat for each line aFloor in tFloorArray
         
         if pArray["main street"][aHouseNumber][aFloor] = pWhom then
            
            return pWhom & cr & "lives on: " & the keys of pArray & cr & tab & "in house number: " & aHouseNumber & cr & tab & tab & "on floor: " & aFloor 
         end if
      end repeat
   end repeat 
   return pWhom && "not found"
end findAPerson
put this into a button and make a field "fRes" and you might want to have a look at the function in the debugger to better understand what goes on.

To keep this simple I have no error checking and at the first hit the function returns the address.

I hope this helps

Kind regards
Bernd

Re: LiveCode Macros / Environment Variables

Posted: Sun Oct 26, 2014 5:28 pm
by stoavio
Bernd, you're the best! I think this is a great example of multi-dimensional arrays although admittedly, I am still trying to wrap my head around it. I will continue to study your example until it starts sinking in.

You have been a tremendous help so thank you! FourthWorld, thanks a lot for that specialFolderPath resource. I know that will come in handy.

....More beginner questions to come. (just a warning!)

Re: LiveCode Macros / Environment Variables

Posted: Sun Oct 26, 2014 7:24 pm
by Simon
I like a ground floor place...
I can get out into the garden! :)

Simon

Re: LiveCode Macros / Environment Variables

Posted: Sun Oct 26, 2014 8:52 pm
by bn
Simon,

you did not tell me you had a garden, cool. I live right next door, do you mind if I come over?
Why not invite the others too and lets have a party. I hope it will not disarray your garden :)

Kind regards
Bernd