Page 1 of 2

Need Simple Variable Help! Please :D

Posted: Thu Apr 17, 2014 12:31 am
by Alex_CTZ
Hi all,
I am a complete noob so please help ;)

Basically I want to make a simple server launcher,

This code should let the user find the server.exe and save it in the container serverLocation

Code: Select all

on mouseUp
   local server
   answer file "What is the server path?"
   put it into serverLocation
end mouseUp
Now I want this button, which i called start to run the server.exe by referring to the serverLocation container and i want my serverLocation container info to be put where i PUT IWANTMYVARIABLEHERE. This is so it can run the .exe file.

Code: Select all

   local serverLocationTwo
   put serverLocation into serverLaunch
   put serverLaunch into variableserver
put "start '' 'IWANTMYVARIABLEHERE'" into myShell
replace "'" with quote in myShell
get shell(myShell) 
I am a complete noob, today is my first day actually trying to write code. So please help I dont know whats wrong I just know its wrong but dont know how to fix it. Please help!
-Thanks

Re: Need Simple Variable Help! Please :D

Posted: Thu Apr 17, 2014 12:55 am
by magice
If I am understanding you correctly,
try

Code: Select all

 put "start " & yourVariable into my shell

Re: Need Simple Variable Help! Please :D

Posted: Thu Apr 17, 2014 1:01 am
by Alex_CTZ
magice wrote:If I am understanding you correctly,
try

Code: Select all

 put "start " & yourVariable into my shell
so it would be:?

Code: Select all

on mouseUp
   local serverLocationTwo
   put serverLocation into serverLaunch
   put serverLaunch into variableserver
put "start " & variableserver into myShell
replace "'" with quote in myShell
get shell(myShell) 
end mouseUp
cause it doesnt work

-Thanks

Re: Need Simple Variable Help! Please :D

Posted: Thu Apr 17, 2014 1:05 am
by Alex_CTZ
Alex_CTZ wrote:
magice wrote:If I am understanding you correctly,
try

Code: Select all

 put "start " & yourVariable into my shell
so it would be:?

Code: Select all

on mouseUp
   local serverLocationTwo
   put serverLocation into serverLaunch
   put serverLaunch into variableserver
put "start " & variableserver into myShell
replace "'" with quote in myShell
get shell(myShell) 
end mouseUp


cause it doesnt work

-Thanks
It says it cant find serverLocation, does that mean that the variable it on button 1 is not being stored?

Re: Need Simple Variable Help! Please :D

Posted: Thu Apr 17, 2014 1:19 am
by magice
I have not worked with the shell command, but I suspect you are making it harder than you need to. Look at "Launch URL" in the dictionary. I also believe that you are having a problem with variable persistence. I believe I know what the problem is, but I want to give you a chance to figure it out. I will give you two helpful hints. First the number 1 debugging tool for finding variable problems is this simple line answer myVariableName. By putting it in your script at various locations you can find where the problem starts. The second clue, is to look up local and global in the dictionary.

good luck, and if you still cant figure it out come back for more hints.

Re: Need Simple Variable Help! Please :D

Posted: Thu Apr 17, 2014 1:27 am
by Alex_CTZ
magice wrote:I have not worked with the shell command, but I suspect you are making it harder than you need to. Look at "Launch URL" in the dictionary. I also believe that you are having a problem with variable persistence. I believe I know what the problem is, but I want to give you a chance to figure it out. I will give you two helpful hints. First the number 1 debugging tool for finding variable problems is this simple line answer myVariableName. By putting it in your script at various locations you can find where the problem starts. The second clue, is to look up local and global in the dictionary.

good luck, and if you still cant figure it out come back for more hints.
The reason i didnt use launch url was because I want to give this to my friends, but I couldnt find out how to make a certain user a variable to find the file automatically, so my second thought was for them to define a location, save it in a container and put it as a variable in the shell.

Thanks for the help, i will look into what you said, thanks so much for helping me learn rather than just giving me the answer ;)

-Thanks again

Re: Need Simple Variable Help! Please :D

Posted: Thu Apr 17, 2014 1:30 am
by magice
Alex_CTZ wrote:
The reason i didnt use launch url was because I want to give this to my friends, but I couldnt find out how to make a certain user a variable to find the file automatically, so my second thought was for them to define a location, save it in a container and put it as a variable in the shell.

Thanks for the help, i will look into what you said, thanks so much for helping me learn rather than just giving me the answer ;)

-Thanks again
launch url can use a location in a variable as well.

Re: Need Simple Variable Help! Please :D

Posted: Thu Apr 17, 2014 1:35 am
by Simon
add this:

Code: Select all

...get shell(myShell)
put myShell 
end mouseUp
That will show you what is in myShell

But you also have not declared "local serverLocation" so it is only active in the mouseUp.

Simon

Re: Need Simple Variable Help! Please :D

Posted: Thu Apr 17, 2014 1:36 am
by Alex_CTZ
magice wrote:
Alex_CTZ wrote:
The reason i didnt use launch url was because I want to give this to my friends, but I couldnt find out how to make a certain user a variable to find the file automatically, so my second thought was for them to define a location, save it in a container and put it as a variable in the shell.

Thanks for the help, i will look into what you said, thanks so much for helping me learn rather than just giving me the answer ;)

-Thanks again
launch url can use a location in a variable as well.
so i could do?

Code: Select all

launch "C:\Users\$USERNAME\Desktop\Launcher"

Re: Need Simple Variable Help! Please :D

Posted: Thu Apr 17, 2014 1:39 am
by Alex_CTZ

Code: Select all

on mouseUp
put serverLocation into variableserver
put "start " & variableserver into myShell
replace "'" with quote in myShell
get shell(myShell) 
end mouseUp
I realized the local was unnecessary, but it still says it cant find the serverLocation container variable.

is there something wrong here:

Code: Select all

on mouseUp
   local server
   answer file "What is the server path?"
   put it into serverLocation
end mouseUp

Re: Need Simple Variable Help! Please :D

Posted: Thu Apr 17, 2014 1:45 am
by Simon
I don't see
"local server"
being used at all?

SImon

Re: Need Simple Variable Help! Please :D

Posted: Thu Apr 17, 2014 1:50 am
by Alex_CTZ
Simon wrote:I don't see
"local server"
being used at all?

SImon

Code: Select all

on mouseUp
   answer file "What is the server path?"
   put it into serverLocation
end mouseUp
Is there something that is not letting the start button access the serverLocation container

Re: Need Simple Variable Help! Please :D

Posted: Thu Apr 17, 2014 1:59 am
by magice
Alex_CTZ wrote: I realized the local was unnecessary, but it still says it cant find the serverLocation container variable.

is there something wrong here:

Code: Select all

on mouseUp
   local server
   answer file "What is the server path?"
   put it into serverLocation
end mouseUp
The variable serverLocation will not persist outside of the first handler unless it is told to do so. There are several ways to do this. The simplest yet least recommended is the use of global variables, and declaring that variable global in each handler in which it is used. A better method is to declare it local outside of any handlers. In that way it becomes persistent to any handler within that script. Of course sometimes you have initiate scripts with buttons. For this reason I prefer to create a custom handler in the stack script and call that handler with the button instead of putting the script in the button itself E.G.

Code: Select all

-- code in button
on mouseUp
doSomething
end mouseUp


--custom handler in stack script
on doSomething
--write button script here
end doSomething
in this way, you keep all your handlers in the stack script so all local variables declared outside of a handler persist.

another hint:
this note is in the dictionary under launch URL
Note : The urlToLaunch must be a standards-compliant URL, in particular file urls must be of the form file://<absolute path>.

Re: Need Simple Variable Help! Please :D

Posted: Thu Apr 17, 2014 2:05 am
by Alex_CTZ
magice wrote:
Alex_CTZ wrote: I realized the local was unnecessary, but it still says it cant find the serverLocation container variable.

is there something wrong here:

Code: Select all

on mouseUp
   local server
   answer file "What is the server path?"
   put it into serverLocation
end mouseUp
The variable serverLocation will not persist outside of the first handler unless it is told to do so. There are several ways to do this. The simplest yet least recommended is the use of global variables, and declaring that variable global in each handler in which it is used. A better method is to declare it local outside of any handlers. In that way it becomes persistent to any handler within that script. Of course sometimes you have initiate scripts with buttons. For this reason I prefer to create a custom handler in the stack script and call that handler with the button instead of putting the script in the button itself E.G.

Code: Select all

-- code in button
on mouseUp
doSomething
end mouseUp


--custom handler in stack script
on doSomething
--write button script here
end doSomething
in this way, you keep all your handlers in the stack script so all local variables declared outside of a handler persist.

another hint:
this note is in the dictionary under launch URL
Note : The urlToLaunch must be a standards-compliant URL, in particular file urls must be of the form file://<absolute path>.
Ahhh I love you! haha.

I did this:

Code: Select all

on mouseUp
   global serverLocation
   answer file "What is the server path?"
   put it into serverLocation
end mouseUp
and button 2

Code: Select all

on mouseUp
   global serverLocation
put serverLocation into variableserver
put "start " & variableserver into myShell
replace "'" with quote in myShell
get shell(myShell) 
end mouseUp
It works now :D Thank you so much, I now kind of understand the whole global and local variables :D

One more question,say I wanted to make a installer for something. How would I include a file so it could be put on the computer when installed? Can this be done?

Re: Need Simple Variable Help! Please :D

Posted: Thu Apr 17, 2014 2:16 am
by magice
Alex_CTZ wrote:
Ahhh I love you! haha.

I did this:

Code: Select all

on mouseUp
   global serverLocation
   answer file "What is the server path?"
   put it into serverLocation
end mouseUp
and button 2

Code: Select all

on mouseUp
   global serverLocation
put serverLocation into variableserver
put "start " & variableserver into myShell
replace "'" with quote in myShell
get shell(myShell) 
end mouseUp
It works now :D Thank you so much, I now kind of understand the whole global and local variables :D

One more question,say I wanted to make a installer for something. How would I include a file so it could be put on the computer when installed? Can this be done?
Good job. However, using globals is a bad habit to get into, especially when you get into larger projects. To answer your other question, look at the menu bar under file for "Import as a control"

your homework:
Do some searching on this forum. there are several threads on using custom properties to store variables. also play around with the script local variables by declaring them local outside of handlers.