AutoHotkey Community

It is currently May 27th, 2012, 12:54 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: January 23rd, 2006, 9:38 pm 
Offline

Joined: October 10th, 2005, 10:44 am
Posts: 299
Location: Germany
Since I couldn't find a function that processes command line parameters, I went to write one myself.
Not sure if it's perfect, but it seems to work fine (improvements are always welcome though):
Code:
; process command line parameters
getParams:
   Loop, %0% ; for each parameter
   {
      param := %A_Index%
      ; check for switches
      StringLeft, paramType, param, 1
      If paramType = - ; switch indicator
      {
          ; determine type of switch
          StringMid, switch, param, 2, 1
          ; settings file switch
         If switch = s
         {
             ; access value (= next parameter)
            param = % A_Index + 1
            file_settings := %param%
            MsgBox, settings file:`n%file_settings%
         }
      }
   }
Return
This script checks whether there are any parameters at all, then goes on to check whether a parameter is a switch (i.e. starting with a minus char - this can easily be modified to check for slashes though) and retrieves the accompanying value (in this case, the desired settings file).


Last edited by Ace_NoOne on February 12th, 2007, 10:48 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 23rd, 2006, 10:58 pm 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
You're right, I don't recall seeing any others than yours posted. This kind of processing will definitely be useful.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 16th, 2006, 7:24 pm 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
I shorted the function to its bare bone:
Code:
Loop, %0% {                       ;for each command line parameter
    If (%A_Index% = "/in")        ;check if known command line parameter exists
        param_in := A_Index + 1   ;assign next command line parameter as value
    Else If (%A_Index% = "/log")
        param_log := A_Index + 1
  }

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 12th, 2007, 10:47 am 
Offline

Joined: October 10th, 2005, 10:44 am
Posts: 299
Location: Germany
toralf wrote:
I shorted the function to its bare bone:
Code:
Loop, %0% {                       ;for each command line parameter
    If (%A_Index% = "/in")        ;check if known command line parameter exists
        param_in := A_Index + 1   ;assign next command line parameter as value
    Else If (%A_Index% = "/log")
        param_log := A_Index + 1
  }
I might be a little late with this, but I just noticed that param_log := A_Index + 1 will just assign a number, not the actual content of the respective variable.
This you will need something like
Code:
param_log := A_Index + 1
param_log := %param_log%

Anyways, I really like your solution, toralf!

PS: I've removed the If 0 > 0 part from my original code; it's redundant, as Loop, %0% does not run if there are no command line parameters.

_________________
Improving my world, one script at a time.
Join the AutoHotkey IRC channel: irc.freenode.net #autohotkey


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 13th, 2009, 4:50 am 
Offline

Joined: November 12th, 2008, 3:48 am
Posts: 15
Hi! I have a question, how could I make a ahk (say test.ahk) keep running and have it take command line parameters, for example, execute this:

test.ahk -test1

so that the script will respond to the command line parameter?

Now I make the script quit after handling each parameter but it's really not efficient.

_________________
David H.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 13th, 2009, 5:24 am 
Offline

Joined: April 18th, 2008, 7:57 am
Posts: 1390
Location: The Interwebs
As far as I know, you can't have an AHK-script accept command line parameters while it is running. You could, however, make a 'helper' AHK-script that accepts the parameters and sends them to the other script through Send/OnMessage, or something similar.


Report this post
Top
 Profile  
Reply with quote  
PostPosted: May 2nd, 2009, 9:45 am 
you can do it without a seperate "helper" script, all inclusive, like so:

Code:
#SingleInstance Off  ;Force
#Persistent
#NoTrayIcon
#NoEnv
SetWorkingDir, %A_ScriptDir%
DetectHiddenWindows, On
OnExit, ExitShopMon

 If %1% ;Command Line
  GoSub, SMServer
 WinGet, InstanceID, List, %A_ScriptFullPath%
 Loop %InstanceID%
  If A_Index <> 1
   PostMessage, 0x1111, 1, 65134, , % "ahk_id " InstanceID%A_Index%
 OnMessage(0x1111, "AnotherInstance")

 GoSub, Initialize
 GoSub, EnableHotKeys
 SetTimer, CheckForUpdate, % RefreshTime * 1000
Return

;OnMessage 0x1111
AnotherInstance(wParam, lParam)
{
  If lParam = 65134
    GoSub, ExitShopMon
}


I'm new to ahk and don't expect that to be the best way, but its how i'm using it (fyi, smserver: ends with an exitapp, and initialize: sets the tray icon back on after #notrayicon) The loop is to send to each instance, and the lparam is arbitrary in this example

- gwarble


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: bowen666 and 19 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group