How to pass variables to GOSUB Subroutine?

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
sone
Newbie
Posts: 1
Joined: Thu Dec 11, 2003 6:49 pm

How to pass variables to GOSUB Subroutine?

Post by sone » Thu Dec 11, 2003 7:14 pm

Imagine, i have twice of the following tasks in my program. Therefore i would like to swap that task out into a subroutine "SaveFile".

The only variable to pass is the name of the saved file %file%:

SRT>SaveFile
IfFileExists>file,FileExists
Goto>SaveFile
Label>FileExists
DeleteFile>file
Label>SaveFile
Press ALT
Send>du
Release ALT
WaitWindowOpen>Save as
Send>filename
Press Enter
ENDSaveFile

Now here's my question: Is it possible to pass a variable to that subroutine? If not, what would be the best way to code this task only once? Maybe should i write a new Macro "SaveFile.scp" and call it like this from the main program:?
Macro>SaveFile.scp /file=c:\myfile.data

Or is there another, maybe better solution out there?

Thanks for help..

sone

User avatar
Captive
Macro Veteran
Posts: 213
Joined: Sun Oct 20, 2002 8:37 pm
Location: Colorado, USA

Post by Captive » Thu Dec 11, 2003 10:04 pm

Keeping it this simple, one of the easiest ways is to just define the variable before you call the subroutine. I'd go with this method:

Let>file=blahblah
Gosub>SaveFile

If you're strongly against mixing local/global variables, then you could convert the routine in to a VBScript function or procedure, and pass the variable in the VBRun command. That, of course, is trickier than the above method. :)

You could write the new macro, and pass the variable like you say, but then that slightly reduces the ability to give the script (compiled exe, etc) to someone else, or copy the script to another installation.

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Thu Dec 11, 2003 10:31 pm

Do the simple process defined by Captive. Just define %file% anywhere before going to SRT. Then you can have different sections redefine %file% any time you want before calling SRTSaveFile

Question?
Do you really intend to do Label>Save File after you do Delete File?
OR do you mean goto SRT>SaveFile?

Or did you mean to include a GOTO ENDSaveFile after Delete File, before Label>SaveFile?

I may be wrong here, but I think you may also run into a problem with a SRT and a label both named SaveFile. Perhaps not in this example but in other areas that may goto SaveFile. General practice by me is to not have duplicate names (due to past scar tissue that I can't recall right now).
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

jalbt51
Junior Coder
Posts: 31
Joined: Sat Mar 06, 2004 4:40 pm

passing a variable from a subroutine to the main program?

Post by jalbt51 » Thu Apr 08, 2004 1:46 am

Can anyone refer me to a simple program I can examine to learn how to pass variables from a sub-routine or from another program, to the main program? Programs with lots of annotations are sincerely appreciated!

Also, Have mercy in the style of its constuction! (the sample script in the menu was beautifully simple.) I am often confused as to when a program is refering to a variable, and when to a file name. Everyone seems to love to use the word "file" for both.

When I view a topic like this and have questions, should I send them to the answerer, or to everybody?
Thanks for the help! jalbt

By the way, thanks so much for putting that manual together. I'm still cranking my way through it

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Thu Apr 08, 2004 3:30 am

When I view a topic like this and have questions, should I send them to the answerer, or to everybody?
Best to keep dialogs in the forum under the same subject. that way everyone gets to benefit from the process, and you gain the benefit of having more than one response to help you out. We all make mistakes at some time, and this also provides the ability for others to help point out and correct out errors.
Can anyone refer me to a simple program I can examine to learn how to pass variables from a sub-routine or from another program, to the main program?
I am afraid we may be caught up in terminlolgy again. What do you mean by "another program"? What is "the main program"?

If you mean a "program" to be a Macro Scheduler script file, then you should check out the Help section on Macro>. From the Help file:
Macro>file_name [/variable=value|variable [/variable=value|variable] ... ]

Executes another script file. file_name must be a filename of a macro file. For macros that appear in Macro Scheduler's control panel, add '.scp' to the end of the macro name. It is also advisable to specify the full path should the path change during the execution of the calling macro.

To pass values to the macro specify each one after a / character. The variable name given should exist in the script to be run. The value to assign to that variable is specified after the = character.

If the calling macro has a log file the script being called with the Macro command will log to the same log file. You can override this using the /LOGFILE parameter to set a log file for the sub macro.

Abbreviation : Mac

Examples

Macro>Defragment Disk.scp

Macro>MyMoveFile.scp /source=c:\temp\myfile.bat /destination=c:\temp\myfile.bak
If you mean something else by "program" then you will have to more specific about what you are trying to do. Pass a value into a Macro Scheduler script? Pass a value from a Macro Scheduler script into a different program like a cell value in a spreadsheet? Or a varialbe to be used in a batch file? Or a switch to be used with a command line program?

Lots of help availble here, but need specifics to do it right.

And don't forget that you already have a good working answer from Captive about how to deal with variables in Sub Routines as noted earlier.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts