Question about global variables

Discuss and share scripts and script files...
Post Reply
Lare2
Posts: 65
Joined: 18 Sep 2006 08:29

Question about global variables

Post by Lare2 »

I'm trying to learn about scripting by doing baby steps here so don't laugh so hard about my code :D .

I assigned the code below to a custom button on the toolbar and call it using the load command. A menu comes up allowing me to select one of the actions to be performed.

As you can see on the code there are variables that are defined twice ($wget and $target). What i would like to do is to define then once so both scripts can see them.

Is this the kind of example where Global variables would help me. If so how can i make it work.


Code: Select all

/*------------------------------------------------------------------------------
WGET SCRIPT NUMBER 01
This script will download the URL on the clipboard

WGET ARGUMENTS = run """cmd"" /k ""[WGET PATH]"" --directory-prefix=[TARGET PATH] [URL]"
------------------------------------------------------------------------------*/

"Download File On Clipboard"

 $wget = <xypath>\wget\wget; // WGET EXE PATH 
 $target = C:\Saves; // TARGET DOWNLOAD FOLDER
 
 msg Would you like to download the following <br><br> <clipboard>, 1; // CONFIRM URL TO DOWNLOAD WITH MESSAGE BOX
                                                                                              
 run """cmd"" /k ""$wget"" -c --directory-prefix=$target <clipboard>"; // WGET COMMAND ARGUMENTS

/*------------------------------------------------------------------------------
WGET SCRIPT NUMBER 02
This script will attempt to download a series of links from a TXT file.
------------------------------------------------------------------------------*/

"Download From Links On Selected File"

 $wget = <xypath>\wget\wget; // WGET EXE PATH
 $target = C:\Saves; // TARGET DOWNLOAD FOLDER 
 $linktxt = readfile(<curitem>);
 msg The following files will be downloaded <br><br>$linktxt, 1; 

 run """cmd"" /k ""$wget"" -c --directory-prefix=$target --input-file=<curitem>";
                                       


TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Question about global variables

Post by TheQwerty »

Well to just transfer your script it could be:

Code: Select all

/*------------------------------------------------------------------------------
This script sets the global variables to be used by other WGET SCRIPTS.
------------------------------------------------------------------------------*/
"_setPaths"
	Global $wget, $target;
	$wget = <xypath>\wget\wget; // WGET EXE PATH
	$target = C:\Saves; // TARGET DOWNLOAD FOLDER


/*------------------------------------------------------------------------------
WGET SCRIPT NUMBER 01
This script will download the URL on the clipboard

WGET ARGUMENTS = run """cmd"" /k ""[WGET PATH]"" --directory-prefix=[TARGET PATH] [URL]"
------------------------------------------------------------------------------*/

"Download File On Clipboard"
	Global $wget, $target;
	Sub("_setPaths");

	msg Would you like to download the following <br><br> <clipboard>, 1; // CONFIRM URL TO DOWNLOAD WITH MESSAGE BOX

	run """cmd"" /k ""$wget"" -c --directory-prefix=$target <clipboard>"; // WGET COMMAND ARGUMENTS

/*------------------------------------------------------------------------------
WGET SCRIPT NUMBER 02
This script will attempt to download a series of links from a TXT file.
------------------------------------------------------------------------------*/

"Download From Links On Selected File"
	Global $wget, $target;
	Sub("_setPaths");

	$linktxt = readfile(<curitem>);
	msg The following files will be downloaded <br><br>$linktxt, 1;

	run """cmd"" /k ""$wget"" -c --directory-prefix=$target --input-file=<curitem>";
This declares that $wget and $target have global values, and the calls to Sub("_setPaths") ensure the variables get set in either case.


You could also convert the script to have a single entry point which would set the global variables and then use Load to display the menu, and the two scripts would only need to call Global().

Lare2
Posts: 65
Joined: 18 Sep 2006 08:29

Re: Question about global variables

Post by Lare2 »

Thank you. I'm paying attention to your modifications to get a better understanding of how global variables work.
You could also convert the script to have a single entry point which would set the global variables and then use Load to display the menu, and the two scripts would only need to call Global().
I'm going to take a look at Global() on the wiki to see what i can come out with. But could you explain to me how would you approach it.

Also i see you put parenthesis in Sub(""); which the syntax examples on the help doesn't mention. Any reason at all, or the help file is outdated.

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Question about global variables

Post by TheQwerty »

Lare2 wrote:Also i see you put parenthesis in Sub(""); which the syntax examples on the help doesn't mention. Any reason at all, or the help file is outdated.
Well the parenthesis are just syntactical sugar which in my opinion makes the scripts more legible, but judging from some of the shared examples I may be in the minority. They aren't required. The quotes around arguments may not be required but I always feel safer with them.

Lare2 wrote:I'm going to take a look at Global() on the wiki to see what i can come out with. But could you explain to me how would you approach it.
Well it's a mixed bag as to which is better here but for example you could do it as:

Code: Select all

/*------------------------------------------------------------------------------
This script is the default entry point.
------------------------------------------------------------------------------*/
"Main"
	Sub("_setPaths"); //Declare and set the global vars.
	Load("*", "_clip;_curItem"); //Load the desired menu, which can now use the global vars.

/*------------------------------------------------------------------------------
This script sets the global variables to be used by other WGET SCRIPTS.
------------------------------------------------------------------------------*/
"_setPaths"
   Global $wget, $target;
   $wget = <xypath>\wget\wget; // WGET EXE PATH
   $target = C:\Saves; // TARGET DOWNLOAD FOLDER


/*------------------------------------------------------------------------------
WGET SCRIPT NUMBER 01
This script will download the URL on the clipboard

WGET ARGUMENTS = run """cmd"" /k ""[WGET PATH]"" --directory-prefix=[TARGET PATH] [URL]"
------------------------------------------------------------------------------*/

"Download File On Clipboard : _clip"
   Global $wget, $target;

   msg Would you like to download the following <br><br> <clipboard>, 1; // CONFIRM URL TO DOWNLOAD WITH MESSAGE BOX

   run """cmd"" /k ""$wget"" -c --directory-prefix=$target <clipboard>"; // WGET COMMAND ARGUMENTS

/*------------------------------------------------------------------------------
WGET SCRIPT NUMBER 02
This script will attempt to download a series of links from a TXT file.
------------------------------------------------------------------------------*/

"Download From Links On Selected File : _curItem"
   Global $wget, $target;

   $linktxt = readfile(<curitem>);
   msg The following files will be downloaded <br><br>$linktxt, 1;

   run """cmd"" /k ""$wget"" -c --directory-prefix=$target --input-file=<curitem>";

To your original scripts I assigned labels (_clip & _curItem) and make them hidden from the default menu. I've also removed the calls to Sub("_setPaths") that I previously put in.

Left my "_setPaths" alone and added a new script "Main" which is the default shown when displaying the menu or executing the resource.

"Main" calls "_setPaths" which declares the vars as global and assigns the values.
Then "Main" uses Load to call the current script but by stating the labels it displays your two scripts in a menu.
The user makes their selection and after running the Global command, your scripts now see the value of the global vars.



I feel you gain some clarity over calling Sub("_setPaths") in every script that would use them, and you have better control over the displayed menu (for instance you could hide_curItem or skip the menu if no item is selected).

But you can't as easily call just one of your scripts from another source (UDC, CKS, etc.) as the paths won't get set; you'd instead have to call _setPaths first. That said if you are only putting this in a CTB that shouldn't matter as I don't think you can call the script from anywhere else (at least not as a script).

Lare2
Posts: 65
Joined: 18 Sep 2006 08:29

Re: Question about global variables

Post by Lare2 »

Thanks for taking the time to explain in all detail what you did in the code above. I think your approach makes more sense than the route previously mentioned. By reviewing your code I'm now getting a better grip about how to tackle this problems.

Thank you again for your help.

Post Reply