Jump to content


Running ad-hoc scripts from the command line


  • Please log in to reply
15 replies to this topic

#1 Guests

  • Guests

Posted 04 November 2006 - 07:13 AM

It would be very useful for small scripts like

autohotkey.exe -script 'winactive, Total Commander'

so that the user doesn't have to create files for trivial scripts.

Of course, there should be a way to combine multiple commands. Something like

autohotkey.exe -script 'ifwinnotexist, MyWin { Run, MyApp} else winactivate'

#2 PhiLho

PhiLho
  • Fellows
  • 6850 posts

Posted 04 November 2006 - 09:23 AM

The given examples are not compelling... If you use these commands frequently, typing them is more annoying than putting them in a permanent script with some hotkey or common menu to run them.
And the combined syntax isn't even possible currently...

#3 Guests

  • Guests

Posted 04 November 2006 - 03:06 PM

I don't type them. I want to invoke them from non-autohotkey scripts and programs without having to create lots of tiny files with one or two commands.

The syntax was only an example. AH shouldn't rely on all commands being on separate lines. This limitation is very old-fashioned. None of the modern scripting languages I used had this kind of problem. Not even Python where the indenting level has special syntactical meaning,

#4 majkinetor !

majkinetor !
  • Guests

Posted 04 November 2006 - 03:23 PM

I agree

#5 Guests

  • Guests

Posted 04 November 2006 - 05:51 PM

Well, that restriction is in place for the sake of simplicity. If you are a more advanced scripter, it may seem inconvenient, but it helps the less coder-minded.

#6 Guests

  • Guests

Posted 04 November 2006 - 06:09 PM

Nothing would prevent the "less coder-minded" people to write commands into separate lines even if it was possible to write multiple commands in the same line, so your argument is a non sequitur.

#7 PhiLho

PhiLho
  • Fellows
  • 6850 posts

Posted 04 November 2006 - 06:35 PM

Several anonymous Guest arguing is disturbing. The first Guest should have taken a nickname before making his wish.
Allowing a command separator isn't in the spirit of AHK, where lot of commands end by a naked string, so it is hard to tell if some symbol is part of the string or a command separator.
Now, you can write a script that takes a parameter which is a scriptlet. It can even split the parameter along an arbitrary chosen separator, getting lines to process with the script able to dynamically interpret AHK commands.
So you can do:
autohotkey.exe ScriptExec.ahk "WinActive Some Editor"

#8 SKAN

SKAN
  • Administrators
  • 9062 posts

Posted 04 November 2006 - 06:46 PM

you can write a script that takes a parameter which is a scriptlet. It can even split the parameter along an arbitrary chosen separator, getting lines to process with the script able to dynamically interpret AHK commands.
So you can do:
autohotkey.exe ScriptExec.ahk "WinActive Some Editor"


Nice idea PhiLh:O
I would prefer to compile and have it as ScriptExec.exe
:D

#9 Guests

  • Guests

Posted 04 November 2006 - 08:38 PM

If someone manages to implement this script executor script which supports multiple AHK commands in a single command line then please post it to the script forum.

Thanks.

#10 another guest

another guest
  • Guests

Posted 05 November 2006 - 01:03 AM

You can use a simple script like this to run ahk code from a command line. It could be easily re-written to take ahk code as input and run that.

#NoTrayIcon
FileAppend,%1%,temp.ahk
FileSetAttrib,+H,temp.ahk
RunWait,AutoHotkey.exe temp.ahk
FileDelete,temp.ahk

It'll work as both a script or compiled, but obviously it'll need AHK installed in either case. To have it standalone, you'd probably have to manually code in every pertinent command and parse the input against all of them... :shock:

As for multiple commands on one line, you use either use `n in one parameter, or use multiple parameters and join them in the script:

#NoTrayIcon
Loop,%0%
	FileAppend,% "`n" . %A_Index%,temp.ahk
FileSetAttrib,+H,temp.ahk
RunWait,AutoHotkey.exe temp.ahk
FileDelete,temp.ahk

E.g. AutoHotkey.exe ahkexec.ahk "WinActivate,Calculator" "MsgBox,Script Completed."

#11 polyethene

polyethene

    Administrator

  • Administrators
  • 5473 posts

Posted 05 November 2006 - 01:30 AM

Here's a different version:
#NoTrayIcon
#SingleInstance ignore
StringReplace, code, 1, ``n, `n, All
If !code
	ExitApp
file = %A_Temp%\cmd%A_Now%.ahk
FileAppend, %code%, %file%
Run, %A_AhkPath% "%file%", %A_WorkingDir%, , pid
Sleep, 1000
FileDelete, %file%
FileAppend, %pid%, *
ExitApp
e.g. Run: cmd.ahk "var = 100`nmsgbox var = %var%"

#12 Guests

  • Guests

Posted 05 November 2006 - 06:12 AM

Thanks, I'll try it.

#13 Veovis not logged in

Veovis not logged in
  • Guests

Posted 05 November 2006 - 06:36 AM

Sweet posts! Thats so cool! Now i can use ahk in the console!!! :D :D :D

I am confused by a couple things in your code titan,

StringReplace, code, 1, ``n, `n, All

what is ``n for? At first i thought it was a work around for being able to have something like "msgbox, test`ntest" (a msgbox with multiple lines) encoded, but it doesnt work...

FileAppend, %pid%, *

In the ahk documentation it says that this will send it to stdout, but that it wont appear in the console. So why do you have that line?


But seriously, this is soooooooooooooooooooooooooooo cool! Mwhahahaha you should have seen me
cmd.ahk "msgbox, test"
cmd.ahk "msgbox, SWEET!"

cmd.ahk "msgbox, hmmm this is getting fun... MWAHAHAH Dancing window time!"

cmd.ahk  "wingetpos,x,y,,,A`nloop,20`n{`nwinmove,A,,x+5,y`nwinmove,A,,x,y+5`nwinmove,A,,x-5,y`nwinmove,A,,x,y-5`n}"

This is so cool! Do you know how many times i have needed to move some window (like an anoying pop up thing that wont go away) and winmove could do it in a heart beat, but i have to make a new file, type the thing, save and run. mwahahah this is sooo cool!

p.s. try the dancing window thing! its cool!

#14 Chris

Chris
  • Administrators
  • 10727 posts

Posted 05 November 2006 - 04:11 PM

Some kind of command-line interface to execute simple scripts would be nice to have. In the meantime, the workarounds above are really creative!

Thanks.

#15 majkinetor

majkinetor
  • Fellows
  • 4511 posts

Posted 06 November 2006 - 09:44 AM

This is very slow for console.

First, AHK itself is used to create another AHK script and to execute it. Much better (faster) solution is to create AHK script in batch and then execute it.