Page 1 of 1 1
Topic Options
#177657 - 2007-07-09 02:45 PM Passing parameters from environment ?
MACE Offline
Starting to like KiXtart

Registered: 2004-09-07
Posts: 150
Loc: Manchester UK
I am sure I must have done this in the past but I now can't find the means of passing command line parameters into a script. I know there are functions for the script location, name and EXE but I need to pass into the script other information and don't really want to have to set environmental variables.
I.e. in a batch file you have %0 to %9 then SHIFT to go above %9 which corresponds to say %0 = KIX32.exe, %1 = KIXscript.kix, %2 ~ %9 my switches.

Would someone please REMIND ME how it is supposed to work as I have come to an abrupt halt over this.

Top
#177660 - 2007-07-09 02:56 PM Re: Passing parameters from environment ? [Re: MACE]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
kix32.exe myscript.kix $var=something

It is explained in the manual.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#177663 - 2007-07-09 03:05 PM Re: Passing parameters from environment ? [Re: Les]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Oh, and while you have your nose in the latest manual, check out GetCommandLine().
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#177668 - 2007-07-09 04:14 PM Re: Passing parameters from environment ? [Re: Les]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Here's some kode I keep handy to refresh my understanding of parsing args via GetCommandLine.

Note that certain args that are passed, like -d or /d are interpreted by Kix32.exe.. I generally use "--" as argument flags that get passed to the script - this way you can run things like "Kix32 MyScript --d --E:flag"

If you associate .KIX files with kix32.exe, make sure the registry Open\Command value looks like >> C:\local\bin\kix32.exe "%1" %* << so the arguments are properly passed.

Glenn

 Code:
Break On

'GetArg - ' @KIX ? ?

; $ARG can be passed on the command line, or can be
; gathered by the GetCommandLine() function

; $ARG will be declared (Global) if it was passed on the command line

If IsDeclared($ARG)
  'ARG passed' ?
Else
  If CDbl(@KIX) < 4.53
    "Can't enumerate args in Kix v" @KIX ?
    Quit 0
  EndIf

  Global $ARG
  'ARG captured' ?

  ; Parse the command line
  $CommandLine = GetCommandLine(1)

  ; Complain and exit if no arguments were specified.
  If UBound($CommandLine) = 1
    'No input arguments were specified.' ?
    Quit 1
  EndIF

  ; Have arguments - figure out what to do
  $ARG = ''
  $Sep = ''
  For $I = 2 to UBound($CommandLine)
    $ARG = $ARG + $Sep + $CommandLine[$I]
    $Sep = ','
  Next
EndIf

'ARG: ' $ARG ?

_________________________
Actually I am a Rocket Scientist! \:D

Top
#177976 - 2007-07-16 02:07 PM Re: Passing parameters from environment ? [Re: Glenn Barnas]
MACE Offline
Starting to like KiXtart

Registered: 2004-09-07
Posts: 150
Loc: Manchester UK
Thanks one and all.
Could not get to work the traditional method as not all values were passing. The GetCommandLine() is new to me and now I am aware of it it is the best tool for the job. I am sure I can proceed with this help.

EDIT >>

The registry comment is VERY pertinant.
It is in fact not KIX which is the problem but the fact it is NOT getting the parameters in the first place.

I have been using the following registry setting to envoke kix:(This is written on setting up each PC during logon script once wkix32 is installed.)

$ = WRITEVALUE ("HKEY_CLASSES_ROOT\KX_auto_file\shell\open\command", "", '%SystemRoot%\system32\wkix32.exe "%1"', "REG_SZ")

I have tried today the following...
$ = WRITEVALUE ("HKEY_CLASSES_ROOT\KX_auto_file\shell\open\command", "", '%SystemRoot%\system32\wkix32.exe "%1" %2 %3 %4 %5 %6 %7 %8 %9', "REG_SZ")

and

$ = WRITEVALUE ("HKEY_CLASSES_ROOT\KX_auto_file\shell\open\command", "", '%SystemRoot%\system32\wkix32.exe "%1" "%2" "%3" "%4" "%5" "%6" "%7" "%8" "%9"', "REG_SZ")

If I have a command line as follows:
WKIX32.EXE MYSCRIPT.KIX PARAMETER
the script runs, passes the parameter correctly but on exiting the script KIX then tries to run a script per the value of PARAMETER and errors as of course the script does not exist.

If as is intended I have a command line as follows:
MYSCRIPT.KIX PARAMETER

ERROR: Failed to open/find script [2]!

The start of the MYSCRIPT.KIX script is as follows...

$CL=GetCommandLine(0)
If IsDeclared($CL)
$CL=GetCommandLine(1)
$X = UBound($CL)
$ = MYFUNCTION($CL[$X])
EndIf

Here $X is returning 2 but $CL[2] does not exist ?

Can anyone offer suggestions on
1] how to sort out the registry settings and
2] let kix actually read the correct value of command line parameters.
( If I use SPLIT on the GetCommandLine(0) I could achieve the desired result but does not help until I can successfully pass a full command line without it erroring either on entry or exit.)


Edited by MACE (2007-07-16 04:11 PM)

Top
#177983 - 2007-07-16 05:58 PM Re: Passing parameters from environment ? [Re: MACE]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
It has something to do with how Kix32.exe handles arguments.

"Kix32 script1 script2 script3" is valid, and will cause the 3 scripts to be executed in sequence. If "script2" is actually an argument, and isn't processed/understood by Script1, then kix32 assumes it is a script, and complains when it can't find it. Exits with status 2 (file not found).

You can circumvent this by exiting Script1 with QUIT 0 instead of EXIT 0. This terminates the current and all following scripts. Not always a clean or convenient process, though. The true fix is to make sure Kix treats your script args properly, passing them to the script and not using them directly.

I've found it's really important to use "--" or "//" to identify kix SCRIPT arguments. When Kix sees the leading "-" or "/", it knows not to process it as a script name. The need to double the argument identifier prevents kix from seeing "-download" as "-d" and invoking debug mode! Remember, kix32 runs and interprets the command line FIRST. It accepts "unadorned" args as script names, and anything beginning with "-" or "/" as a potential modifier. It will use them itself, even though it passes all the args to your script. Thus, you often get unwanted results.

Bottom line
"Kix32 MyScript filename" will usually fail because "filename" can't be found, or isn't a script.
"kix32 MyScript --f:filename" will work, because kix32 knows to pass it to the script. Of course, you need to verify the "--f:" part and Split on the ":" to get the filename. More up-front work, but much higher reliability.

"kix32 MyScript -download" will invoke kix debug mode, while
"kix32 MyScript --download" will run normally.

Hope this helps.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#177984 - 2007-07-16 06:42 PM Re: Passing parameters from environment ? [Re: Glenn Barnas]
MACE Offline
Starting to like KiXtart

Registered: 2004-09-07
Posts: 150
Loc: Manchester UK
I understand the implications of the - or / switch and can deal with that, I do however not wish to have to call the exe as part of the command line, and this is where it gets tricky as I am unable to even invoke the script without getting the registry entry right. Will see what the research throws up but I think it is as much KIX as the OS !
Top
#177986 - 2007-07-16 06:58 PM Re: Passing parameters from environment ? [Re: MACE]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
The registry value I defined earlier works perfectly for me, and I never invoke Kix32 manually unless I'm testing something and want a specific version (ie: kix32.452 MyScript).

I use command line arguments on a large number of scripts ever since the release of the GetCommandLine function. Even with that, you need to accept that Kix will interpret all the arguments before the script, regardless of how you pass the arguments, or what your registry settings are. arguments without leading "-" or "/" will always be interpreted by Kix32 as script names.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#177998 - 2007-07-17 09:56 AM Re: Passing parameters from environment ? [Re: Glenn Barnas]
MACE Offline
Starting to like KiXtart

Registered: 2004-09-07
Posts: 150
Loc: Manchester UK
Are you literally using...
kix32.exe "%1" %*

ie
$ = WRITEVALUE ("HKEY_CLASSES_ROOT\KX_auto_file\shell\open\command", "", '%SystemRoot%\system32\wkix32.exe "%1" %*', "REG_SZ")

Top
#177999 - 2007-07-17 10:30 AM Re: Passing parameters from environment ? [Re: MACE]
MACE Offline
Starting to like KiXtart

Registered: 2004-09-07
Posts: 150
Loc: Manchester UK
Using the above I get the parameters passed but still with anomolies.
I have changed the approach successfully but as you will see UBOUND gives a false value and some of the arguments are blank.
break on

$CL=GetCommandLine(0)

If IsDeclared($CL)
$CL ?
$CL=split(GetCommandLine(0)," ")
$X=0
for each $n in $CL
? "Parameters "+$x+"="+$n+@crlf
$X=$X+1
if $N <>""
$ARG=$N
Endif
next
? "Last Parameter = "+$ARG
EndIf
Quit()

Top
#178005 - 2007-07-17 01:40 PM Re: Passing parameters from environment ? [Re: MACE]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Yes, that is the literal registry configuration!

I don't use "GetCommandLine(0)" - only "GetCommandLine(1)", returning the array.
With your code on my system, I get:
 Code:
COMPUTER - C:\Temp>mart -1 -2 -3
"C:\local\bin\kix32.exe" "C:\Temp\mace.kix"  -1 -2 -3
Parameters 0="C:\local\bin\kix32.exe"
Parameters 1="C:\Temp\mart.kix"
Parameters 2=
Parameters 3=-1
Parameters 4=-2
Parameters 5=-3

Last Parameter = -3

But changing
$CL=split(GetCommandLine(0)," ")
to
$CL = GetCommandLine(1)
returns:
 Code:
COMPUTER - C:\Temp>mart -1 -2 -3
"C:\local\bin\kix32.exe" "C:\Temp\mace.kix"  -1 -2 -3
Parameters 0=C:\local\bin\kix32.exe
Parameters 1=C:\Temp\mart.kix
Parameters 2=-1
Parameters 3=-2
Parameters 4=-3

Last Parameter = -3

Which seems to be more what you want, isn't it?

I removed the Quit() from the end for this test, which should really be "Quit 0" if you actually need it. Also - I don't understand the "IsDeclared(CL)" statement. You're implicitly declaring it with the statement $CL=GetCommandLine(0), so that test will always be true.

If you look at my code, I'm using IsDeclared to determine if a var called ARG was passed on the command line (kix32 MyScript $ARG=xxx). If it wasn't, than $ARG is created via the GetCommandLine function. You can simplify your code to:
 Code:
break on

; Illustrate the full command line string
'Comand line is: ' GetCommandLine(0) ?

; Load the Command Line array
$CL = GetCommandLine(1)

; Show count of args
UBound($CL) - 1 ' arguments were passed' ?

; process the args if any were passed
If UBound($CL) - 1 
  for $X = 2 to UBound($CL)
    "Parameter " $x-1 "=" $CL[$X] ?
  next
  "Last Parameter = "+$CL[UBound($CL)] ?
EndIf


Try this without args, with "tom dick harry" as args, then repeat the command line with single and then double "-" in front of each arg. You'll get 3 different results! First will complain that the script Tom isn't found (no Quit 0), then it will see the "T" in "Tom" and try to tokenize "Dick". Finally, it will pass ALL of the args to the script without error or complaint.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#178007 - 2007-07-17 02:56 PM Re: Passing parameters from environment ? [Re: Glenn Barnas]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4567
Loc: USA
I think this exercise proves it's still too complicated to get free style arguments from the command line. [sigh]
Top
#178015 - 2007-07-17 05:36 PM Re: Passing parameters from environment ? [Re: Allen]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
The parameters starting with slash or dash are free ;\)
Top
#178027 - 2007-07-18 12:07 AM Re: Passing parameters from environment ? [Re: Witto]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Not quite - parameters starting with double slash or dash are free. Those starting with single slash/dash are free so long as the first char following the slash/dash doesn't conflict with the chars recognized by Kix32.

I use script args extensively now that they are available, and am willing to double-dash the D,F,R,U, and T to prevent conflicts. Since I now mostly write with double-dash arg parsing overall, it makes it easy to remember. When I need to pass an argument like a file name, I do it with --f:filename and split on the first ":".

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#178041 - 2007-07-18 12:51 PM Re: Passing parameters from environment ? [Re: Glenn Barnas]
MACE Offline
Starting to like KiXtart

Registered: 2004-09-07
Posts: 150
Loc: Manchester UK
Not withstanding all the invaluable contributions above, can anyone explain why and suggest a fix to the following:
I issue the KIX command:
$ = WRITEVALUE ("HKEY_CLASSES_ROOT\KIX_auto_file\shell\open\command", "", '%SystemRoot%\system32\wkix32.exe "%1" %*', "REG_SZ")

but the registry says: C:\WINDOWS\system32\wkix32.exe "*

I Have to manually edit the registry to get:
C:\WINDOWS\system32\wkix32.exe "%1" %*

Then I get the results discussed above.

EDIT>>
Trial and error but finally got it to write the registry properly:
$ = WRITEVALUE ("HKEY_CLASSES_ROOT\KIX_auto_file\shell\open\command", "", '%SystemRoot%\system32\wkix32.exe "%1" '+"%*", "REG_SZ")

Don't know why the %* causes the problem !


Edited by MACE (2007-07-18 12:57 PM)

Top
#178042 - 2007-07-18 01:45 PM Re: Passing parameters from environment ? [Re: MACE]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
I use the Associate() UDF when defining the registry keys. Here's the prep code from my script that updates the registry via Associate().

FYI - $BinPath is where the Kix32 exe's are placed; $KIXEDITOR is "notepad.exe", but can be your favorite Kix editor - just so there's no mystery.

The Associate UDF does all the housekeeping needed to create Open and Edit command definitions for a particular file extension. It's posted here on KORG (and part of the KixDev package on my web site). The real point here is to show how I assembled the Open registry value. The problem is that "%" has special meaning, and will be interpreted by Kix32 as an environment value. You should use "%%" instead of "%" so Kix will ignore it. Thats why I used Chr(37) in the example below.

Glenn

 Code:
  ;Define the executables to associate with OPEN and EDIT functions
  ; Modified to pass all args to support V4.53 GetCommandLine()
  ; KiXtart Command Line Scripts - Kix32.exe "%1" %*
  $KXOPEN = $BinPath + '\kix32.exe ' + CHR(34) + Chr(37) + '1' + CHR(34) + ' '  + Chr(37) + '*'
  ; KixForms GUI Scripts
  $KWOPEN = $BinPath + '\Wkix32.exe /I ' + CHR(34) + Chr(37) + '1' + CHR(34) + ' '  + Chr(37) + '*'
  ; Editor for all non-tokenized KiXtart scripts
  $KXEDIT = $KIXEDITOR + ' ' + CHR(34) + Chr(37) + '1' + CHR(34)

  Associate('.KIX', 'KixScript', 'Kixtart Script', $KXOPEN, $KXEDIT, 0)
  Associate('.KX', 'KixTokenizedScript', 'Kixtart Script', $KXOPEN, '', 0)
  Associate('.KXW', 'KixWScript', 'Kixtart GUI Script', $KWOPEN, $KXEDIT, 0)
  Associate('.KW', 'KixTokenizedWScript', 'Kixtart GUI Script', $KWOPEN, '', 0)
_________________________
Actually I am a Rocket Scientist! \:D

Top
#196416 - 2009-10-21 11:49 AM Re: Passing parameters from environment ? [Re: Glenn Barnas]
MACE Offline
Starting to like KiXtart

Registered: 2004-09-07
Posts: 150
Loc: Manchester UK
Just reviewing this while thinking about a current little project:
explorer.exe shell extension and passing parameters to kixscript.
On the command line I can pass parameters to a script no problem but due to the limitations of explorer.exe only the 1st parameter is passed.
All I have read says you need a .dll designed to extend explorer to pass multiple parameters.
In my case I want to be able to select multiple files in a window, then menu click them and select my Kix-context entry to process them.
Works if only one item but fails if multi select.

Anyone any ideas as to whether a kix script could be leveraged as an explorer.exe extension or even where to start with this. I have seen VBasic examples but they can be compiled and follow the recognised process...

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 764 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.073 seconds in which 0.027 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org