ExecuteLine

Stelle Fragen zur Programmierung mit Autohotkey

Moderator: jNizM

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

ExecuteLine

Post by BoBo » 04 Jul 2022, 07:41

Hallo Leuts,
ich habe gerade mal in Matric's Discord-AutoHotkey-SubChannel vorbeigeschaut - und dort wurde die Frage gestellt ob AHK auch "command line" kann!?
Wobei ich dann doch feststellen musste das es dabei offensichtlich um eine andere Auslegung ging. Hier wurde nicht gefragt ob AHK als %comspec% agieren kann, sonder ob es seine eigenen Befehle via Befehlszeile auch "von Außen" ausführen/interpretieren kann. AutoIt macht das wohl, nur ich kann mich nicht erinnern ob AHK das auch kann? Dafür gab's glaube ich eine UDF, odä??? :wtf:

https://discord.com/channels/537764560791273472/766604002615230474
Nod: C:\Program Files (x86)\AutoIt3>AutoIt3.exe /autoit3executeline "msgbox( 0, 'test', 'test command line')"
to contribute if anyone has an idea to use it
:?: :think:

User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: ExecuteLine

Post by jNizM » 05 Jul 2022, 02:05

Probier dein Glück mit Args (https://www.autohotkey.com/docs/Scripts.htm#cmd)

Verstehe nur den Anwendungsfall nicht, warum man das machen sollte
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

Rohwedder
Posts: 7551
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: ExecuteLine

Post by Rohwedder » 05 Jul 2022, 02:28

Hallo,
etwa sowas?:

Code: Select all

#SingleInstance, Force
If !A_IsAdmin
	Try Run *RunAs "%A_ScriptFullPath%"
InputBox, Line, ExecuteLine,,,,100,,,,, MsgBox,, Test,`% 4+3
ExecScript(Line)
ExecScript(Script, Wait:=true)
{ ;https://www.autohotkey.com/docs/commands/Run.htm#ExecScript
    Shell := ComObjCreate("WScript.Shell")
    Exec := Shell.Exec("AutoHotkey.exe /ErrorStdOut *")
    Exec.StdIn.Write(Script), Exec.StdIn.Close()
    Return, Wait?Exec.StdOut.ReadAll():""
}

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: ExecuteLine

Post by BoBo » 05 Jul 2022, 15:29

jNizM wrote:
05 Jul 2022, 02:05
Probier dein Glück mit Args (https://www.autohotkey.com/docs/Scripts.htm#cmd)

Verstehe nur den Anwendungsfall nicht, warum man das machen sollte
Mit Args verwalte ich doch (lediglich) die über die Kommandozeile übergebenen Parameter? :think:
Der Anwendungsfall stellt sich wie folgt dar:

Eine :arrow: Matrix Schaltfläche (vergleichbar einem Windows Desktop Shortcut, hier jedoch unter Android) wird per "Tastendruck" mit definierter Kommandozeile ausgeführt.
Auf der Empfängerseite (PC/MAC/Linux) führt die installierte Serveranwendung diesen Task aus (z.B. auf einem PC die Ausführung eines AHK-Scriptes). Das geht auch problemlos (bei Matric wurde, als ich es ausprobierte via clipboard übergeben). Doch die AHK-Befehle werden dabei vom AHK-Interpreter mittels AHK-Script ausgeführt.

Der AutoIt 'ExecuteLine'-Befehl (AFAICS) agiert wohl so, das ein Befehl wie oben angeben direkt vom Interpreter ausgeführt wird, was ich ähnlich z.B. von in Browseradresszeilen (Kommandozeile) übergebenem JavaScript-Code (wie bookmarklets) kenne - also ohne explizit vorhandenes script.
javascript:{window.alert("Hello World")}

@Rohwedder - Interessant. So (oder ähnlich) dürfte eine Funktion im AU3 Interpreter den Befehl umsetzen. Doch der AHK-Interpreter geht hier den Umweg über ein script. :shifty:

:)

ExecLine.ahk (basierend auf der ExecScript()-function aus der AHK-Hilfe)
Aufruf über die Kommandozeile (Win+R). Die gezeigten Beispielaufrufe sind bzgl der Pfade zur AutoHoteky.exe/ExecLine.ahk entsprechend anzupassen.

Code: Select all

#SingleInstance, Force
SetWorkingDir, A_ScriptDir

if !A_IsAdmin
	Try Run *RunAs "%A_ScriptFullPath%"
if (A_Args.Count()=0)
   Exit

ExecLine(A_Args[1])

ExecLine(Line, Wait:=true) {                         ;https://www.autohotkey.com/docs/commands/Run.htm#ExecScript
   Shell := ComObjCreate("WScript.Shell")
   Exec := Shell.Exec("AutoHotkey.exe /ErrorStdOut *")
   Exec.StdIn.Write(Line), Exec.StdIn.Close()
;  SoundBeep
   Return, Wait?Exec.StdOut.ReadAll():""
   }


; C:\_MyPrograms\AutoHotkey\AutoHotkey.exe    C:\_MyPrograms\AutoHotkey\ExecLine.ahk   "MsgBox,, Test,% 4+3"
; C:\_MyPrograms\AutoHotkey\AutoHotkey.exe    C:\_MyPrograms\AutoHotkey\ExecLine.ahk   "FileAppend, %A_Now%`n, ExecLine.txt"

;"C:\_MyPrograms\AutoHotkey\AutoHotkey.exe " "C:\_MyPrograms\AutoHotkey\ExecLine.ahk " "MsgBox,, Test,% 4+3"
;"C:\_MyPrograms\AutoHotkey\AutoHotkey.exe " "C:\_MyPrograms\AutoHotkey\ExecLine.ahk " "FileAppend,% A_Now ""`n"", ExecLine.txt"
;"C:\_MyPrograms\AutoHotkey\AutoHotkey.exe " "C:\_MyPrograms\AutoHotkey\ExecLine.ahk " "SoundBeep"
;"C:\_MyPrograms\AutoHotkey\AutoHotkey.exe " "C:\_MyPrograms\AutoHotkey\ExecLine.ahk " "Run calc.exe"


F12::Run ExecLine.txt
Das ExecScript aus der AHK-Hilfe mit ein paar zusätzlichen Beispiel-Aufrufen (to whom it may concern).

Code: Select all

#SingleInstance, Force
SetWorkingDir, A_ScriptDir

ExecScript(Script, Wait:=true) {                                  ; https://www.autohotkey.com/docs/commands/Run.htm#ExecScript
    shell := ComObjCreate("WScript.Shell")
    exec := shell.Exec("AutoHotkey.exe /ErrorStdOut *")
    exec.StdIn.Write(script)
    exec.StdIn.Close()
    if Wait
        return exec.StdOut.ReadAll()
   }

ExecScript("MsgBox % " 3*4)                                       ; MsgBox displaying '12'
ExecScript("IniWrite," A_Now ", " A_ScriptName ", section, key")  ; updating the below "embedded" INI's 'section' key with the current date&time stamp
ExecScript("FileAppend, % " A_Now "," A_ScriptName)               ; appending the current date&time stamp to the last line of this script
ExecScript("Run calc.exe")                                        ; well, guess what!
ExecScript("SoundBeep")                                           ; making some noize.

/*
[section]            ; press F5 for seeing the key getting updated
key=0
*/

PS. @geek(Dude) hat gerade im Matric-Discord noch folgende Info zu Alternativen (cmd bzw. PowerShell) offeriert:
GeekDude: You can achieve a similar effect using CMD.exe to provide the stdin value, i.e. cmd.exe /c "echo msgbox, 0, test, test | AutoHotkey.exe *"
GeekDude: [...], you can use PowerShell too, powershell -ExecutionPolicy Bypass -Command "echo 'msgbox, 0, test, test' | & 'C:\ProgramFiles\AutoHotkey\AutoHotkey.exe' '*'"

User avatar
Gerdi
Posts: 185
Joined: 03 Aug 2015, 18:48
Location: Germany
Contact:

Re: ExecuteLine

Post by Gerdi » 13 Jul 2022, 04:33

Interessant, ich kann ein AHK-Skript starten ohne es in Dateiform ab zu speichern,

Code: Select all

powershell -ExecutionPolicy Bypass -Command "echo 'msgbox, 0, test, Es folgen  %A_Hour%  `nPeeps' `n ' Loop % A_Hour ' `n ' SoundBeep,500,500' | & 'C:\Program Files\AutoHotkey\AutoHotkey.exe' '*'"
aber den sinnvollen Einsatzfall suche ich noch.
Win 10 Home (x64) and Win 11 Pro N
https://github.com/Grrdi/ZackZackOrdner/archive/master.zip --> get folders on the quick

Post Reply

Return to “Ich brauche Hilfe”