Use InputBox to trigger an action in another script Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Use InputBox to trigger an action in another script

Post by Ecimeric » 21 Jan 2022, 19:45

How can I devise an expansible means of using an InputBox to trigger an action in another script? Essentially, I want to:
  1. specify an action that creates the InputBox, such as search or shutdown;
  2. trigger the action in another script with args for the action type and string.

Code: Select all

; Menu.ahk (with pseudocode)
Google := Func("InputBox").Bind("g")
Menu, MyMenu, Add, Google, % Google
Menu, MyMenu, Show

InputBox(Action) {
    if "g" {
        Title := "Search Google"
        Prompt := "Enter a query:"
    } else if "s" {
        Title := "Shutdown"
        Prompt := "Enter a timecode:"
    }
    InputBox, % OutputVar, % Title, % Prompt
    Run, Action.ahk % Action OutputVar
}

; Action.ahk (with pseudocode)
if InStr(A_Args.1, "g" or "d") {
    OpenURL(A_Args.1, A_Args.2)
    ExitApp
} else if InStr(A_Args.1, "s") {
    Shutdown(A_Args.2)
    ExitApp
}

OpenURL(Engine, Query) {
    g := "https://www.google.com/search?q="
    d := "https://duckduckgo.com/?q="
    Run, % WebBrowser Engine Query
}

Shutdown() {
    ; Split timecode into h, m
    Run, shutdown.exe /H%h% /M%m%
}

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Use InputBox to trigger an action in another script

Post by amateur+ » 21 Jan 2022, 20:14

Have you tried this miracle written on AHK? https://www.quickaccesspopup.com/

For your topic you can read this: viewtopic.php?p=440134#p440134
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Use InputBox to trigger an action in another script  Topic is solved

Post by mikeyww » 21 Jan 2022, 20:25

You already have the script here, just needs syntax corrections. Why two scripts instead of one?

Code: Select all

Google := Func("box").Bind("g")
Menu, MyMenu, Add, Google, % Google
Menu, MyMenu, Show

box(action) {
 Static prompt := {g: ["Search Google"    , "a query"]
                 , d: ["Search DuckDuckGo", "a query"]
                 , s: ["Shutdown"         , "seconds"]}
 InputBox, param, % prompt[action].1, % "Enter " prompt[action].2,, 300, 125
 If (ErrorLevel || param = "")
  Return
 param := encodeDecodeURI(param)
 Run, %A_AhkPath% "%A_ScriptDir%\action.ahk" %action% %param%
}

encodeDecodeURI(str, encode := true, component := true) {
 ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=84825&p=372154#p372154
 Static doc, js
 If !doc {
  doc := ComObjCreate("htmlfile"), doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
  js  := doc.parentWindow
  (doc.documentMode < 9 && js.execScript())
 }
 Return js[(encode ? "en" : "de") "codeURI" (component ? "Component" : "")](str)
}

Code: Select all

Switch A_Args.1 {
 Case "g": Run, % "chrome.exe https://www.google.com/search?q=" A_Args.2
 Case "d": Run, % "chrome.exe https://duckduckgo.com/?q="       A_Args.2
 Case "s": Run, % "shutdown.exe /s /t "                         A_Args.2
}
ExitApp

Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Use InputBox to trigger an action in another script

Post by Ecimeric » 21 Jan 2022, 21:06

mikeyww wrote:
21 Jan 2022, 20:25
You already have the script here, just needs syntax corrections. Why two scripts instead of one?
Thank you, both! I hadn't thought of decoding the URI. I want two scripts because I don't necessarily want to show the actions in the main one.

For the shutdown action, I should have clarified that I want to pass a timecode like 18:30 to DShutdown.exe " /ATTIME /H%h% /M%m% /STANDBY /EXITPROGRAM".

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Use InputBox to trigger an action in another script

Post by mikeyww » 21 Jan 2022, 21:17

Code: Select all

time := StrSplit("18:30", ":")
MsgBox, % "DShutdown.exe /ATTIME /H" time.1 " /M" time.2 " /STANDBY /EXITPROGRAM"
;  Run, % "DShutdown.exe /ATTIME /H" time.1 " /M" time.2 " /STANDBY /EXITPROGRAM"

Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Use InputBox to trigger an action in another script

Post by Ecimeric » 21 Jan 2022, 21:53

Code: Select all

time := StrSplit(A_Args.2, "%3A")

Switch A_Args.1 {
...
Thank you.

Post Reply

Return to “Ask for Help (v1)”