run a single AHK command in windows cmd? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
surfactant
Posts: 99
Joined: 28 Jun 2019, 01:07

run a single AHK command in windows cmd?

28 Jun 2019, 01:15

I wonder whether it's possible to run a single AHK command in windows command line, like in the hypothetical (failing) command line below:

Code: Select all

"C:\Program Files\AutoHotkey\AutoHotkey.exe" FileRecycle, C:\000\*.txt
Thanks!
Rohwedder
Posts: 7551
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: run a single AHK command in windows cmd?

28 Jun 2019, 02:08

Hallo,
I don't think so.
But how about running a temporary script?
Try:

Code: Select all

q::
FileDelete, ~.ahk
FileAppend,
(
FileRecycle, C:\000\*.txt
),~.ahk, UTF-8
Run, ~.ahk
Return
surfactant
Posts: 99
Joined: 28 Jun 2019, 01:07

Re: run a single AHK command in windows cmd?

28 Jun 2019, 03:16

@Rohwedder Thank you for the solution! If AHK command can be used as windows native commands, many jobs could be simplified.
garry
Posts: 3738
Joined: 22 Dec 2013, 12:50

Re: run a single AHK command in windows cmd?

28 Jun 2019, 03:26

example for DOS window , size / position / color
dos commands DEL or ERASE is same as filedelete ( maybe use ahk command filerecycle )

Code: Select all

#warn
setworkingdir,%a_scriptdir%
wa:=a_screenwidth
ha:=a_screenheight
W :=(wa*50)/100    ;- width
H :=(ha*92)/100    ;- height

files=%a_desktop%\000\*.txt   ;- delete txt-files from here ( parameter /P asks for Y/N )

;- some dos commands example
e4x=
(Ltrim join&
@echo off
echo date=
date /t
echo time=
time /t
ver
del /P /S %files%
)
title2=DOS_TEST
run, %comspec% /T:0A /k "title %title2%&mode con lines=4000 cols=120&%e4x%,,,pid2
WinWait, ahk_pid %pid2%
WinMove, ahk_pid %pid2%, ,1,1,%w%,%h%       ;- move DOS window to the defined position
return
surfactant
Posts: 99
Joined: 28 Jun 2019, 01:07

Re: run a single AHK command in windows cmd?

28 Jun 2019, 06:26

garry wrote:
28 Jun 2019, 03:26
example for DOS window , size / position / color
dos commands DEL or ERASE is same as filedelete ( maybe use ahk command filerecycle )
Thank you for your reply!

As far as I know, dos commands DEL or ERASE simply delete the files from the disk completely, rather than moving them to the recycle being as the ask command FileRecycle does.
garry
Posts: 3738
Joined: 22 Dec 2013, 12:50

Re: run a single AHK command in windows cmd?

28 Jun 2019, 08:05

As far as I know, dos commands DEL or ERASE simply delete the files from the disk completely, rather than moving them to the recycle being as the ask command FileRecycle does.
Yes, it's same as ahk-command 'Filedelete' , so use ahk-command 'Filerecycle' , moves to 'RecycleBin'
ahk7
Posts: 574
Joined: 06 Nov 2013, 16:35

Re: run a single AHK command in windows cmd?

28 Jun 2019, 11:31

You can use a batch file to write a temp. ahk script and start that, place the following somewhere in your `path` as `a.cmd`

Code: Select all

@ECHO OFF
DEL onelinescript.ahk
ECHO %* > onelinescript.ahk
START /w onelinescript.ahk
DEL onelinescript.ahk
if you want to run an AHK command you prefix it with a+space+your command, like so
a MsgBox Hello There will show a msgbox.
:?:
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: run a single AHK command in windows cmd?

28 Jun 2019, 12:25

You could have a script that simply executes a function.
Where the first parameter is the function name.
And the other parameters are the function parameters.

This would work well in AHK v2, where all the commands have been converted to functions.
And in AHK v1 if you use custom backports of the AHK v2 functions.

commands as functions (AHK v2 functions for AHK v1) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=37&t=29689

Code: Select all

;==================================================

;func runner

;examples (call script from an AHK script):
;Run, "%A_Desktop%\func runner.ahk" MyMsgBox "hello world"
;Run, "%A_Desktop%\func runner.ahk" MyMsgBox hello
;Run, "%A_Desktop%\func runner.ahk" FileRecycle "%A_Desktop%\New Text Document.txt"

;examples (call script from a batch file):
;"C:\Users\%username%\Desktop\func runner.ahk" MyMsgBox "hello world"
;"C:\Users\%username%\Desktop\func runner.ahk" MyMsgBox hello
;"C:\Users\%username%\Desktop\func runner.ahk" FileRecycle "C:\Users\%username%\Desktop\New Text Document.txt"

;==================================================

if (A_Args.1 = "")
{
	MsgBox, % "error: no function specified"
}
vFunc := A_Args.RemoveAt(1)
if !oFunc := Func(vFunc)
{
	MsgBox, % "error: invalid function"
}
%oFunc%(A_Args*)
return

;==================================================

MyMsgBox(Text)
{
	MsgBox, % Text
}

;==================================================

;commands as functions (AHK v2 functions for AHK v1) - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=37&t=29689

FileRecycle(FilePattern)
{
	FileRecycle, % FilePattern
	return !ErrorLevel
}

;==================================================
There are some rundll32 examples, here, to run a Winapi dll function:
jeeswg's Explorer tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=31755

However, sending a file to the recycle bin, via the Winapi function SHFileOperation, is more complicated, you can't just specify a string, it needs a struct.
Last edited by jeeswg on 28 Jun 2019, 14:18, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
surfactant
Posts: 99
Joined: 28 Jun 2019, 01:07

Re: run a single AHK command in windows cmd?

28 Jun 2019, 13:08

ahk7 wrote:
28 Jun 2019, 11:31
if you want to run an AHK command you prefix it with a+space+your command, like so
a MsgBox Hello There will show a msgbox.
What did you mean? Where do you put the a MsgBox Hello There to run? Thanks!
surfactant
Posts: 99
Joined: 28 Jun 2019, 01:07

Re: run a single AHK command in windows cmd?

28 Jun 2019, 13:10

jeeswg wrote:
28 Jun 2019, 12:25
You could have a script that simply executes a function.
Are you sure you are talking about running a AHK command in a Windows .bat file or command line?

Thanks!
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: run a single AHK command in windows cmd?

28 Jun 2019, 14:19

I've updated my script above, to provide examples for use with a batch file. Cheers.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
ahk7
Posts: 574
Joined: 06 Nov 2013, 16:35

Re: run a single AHK command in windows cmd?

28 Jun 2019, 16:02

surfactant wrote:
28 Jun 2019, 13:08
What did you mean? Where do you put the a MsgBox Hello There to run? Thanks!
Copy that code I posted into a new batch file, save it as "a.cmd" - place that somewhere in your "path" environment, say c:\windows\ - that way you can call it from any directory in a dos command window by simply typing "a"

Once you have done that (created a.cmd and saved it somewhere in your "path" https://en.wikipedia.org/wiki/PATH_(variable)#DOS,_OS/2,_and_Windows ), open a command prompt (I assume you know how) and just type a anyahkcommandyouwish+enter and bob's your uncle (meaning it will work). Of course "dangerous" as there is no error checking etc but writing a script with a mistake in it can also be dangerous.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: run a single AHK command in windows cmd?  Topic is solved

28 Jun 2019, 17:22

You can do that by:

Code: Select all

echo MsgBox Hi | "C:\Program Files\AutoHotkey\AutoHotkey.exe" *
To make it easier, save the following to C:\Windows\ahk.bat:

Code: Select all

@echo %* | "C:\Program Files\AutoHotkey\AutoHotkey.exe" *
Now you have a ahk command :)

Code: Select all

ahk MsgBox, 64, Hello, World
surfactant
Posts: 99
Joined: 28 Jun 2019, 01:07

Re: run a single AHK command in windows cmd?

28 Jun 2019, 23:02

@tmplinshi @ahk7

Thank you both for the very very interesting solution!

(donot know why the forum doesnot allow more than one post to be accepted as the solution)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: jaka1, marypoppins_1, mikeyww, OrangeCat, RussF and 132 guests