The HTTP route is probably the right way to go. You can easily set up a small Server using Node.js Node.js, and have it spawn your AHK script using one of the Child Process functions. I don't know how the Chrome Extension security model works, so you might have to mitigate cross-site scripting concerns. I think that's solvable, but haven't done so.
Answer from Wade Hatler on Stack Overflowjquery - How can I activate an AutoHotkey script using JavaScript? - Stack Overflow
Need to run node.js through autohotkey. So far my way is too slow.
Add the directory to your computer's path, then a simple "Run, node main.js" should work.
More on reddit.comRunning AutoHotkey from JavaScript
any currently updated library that can do something like autohotkey in on javascript?
thanks. actually, those are the 2 i am looking at right now. Robots seems to be the most used one, but it stopped being updated 2 years ago. and nuts look solid, but almost no people seem to be using it.
do you have any personal experience using any of those?
More on reddit.comThe HTTP route is probably the right way to go. You can easily set up a small Server using Node.js Node.js, and have it spawn your AHK script using one of the Child Process functions. I don't know how the Chrome Extension security model works, so you might have to mitigate cross-site scripting concerns. I think that's solvable, but haven't done so.
Generally, two ways come to mind:
1. Emulating a HTTP Server using AHK
Using an own local web server (ideally a firsthand AHK implementation), you can directly communicate via AJAX calls. The server can then receive and delegate any kind of commands. For AHK, there exists Sparrow, but the project seems to be dead, and the source code links in the post are down; you'd have to get them from somewhere else (e.g. some svn repository). You could also implement a command line tool like KF Web Server and use it in conjunction with your script.
2. Defining your own protocol
Another solution would be to simply specify your own protocol and assign it to an executable, e.g. your script or some kind of dispatcher, that starts other applications depending on the parameters you pass it. This answer describes how to define your own protocol in windows. You can also check out the respective windows documentation.
If you're able to call custom protocols from within your browser with JavaScript strongly depends on your browser, and maybe its security settings. You'll need to check that out for your environment.
Security
No matter what solution you go with, giving a webpage the ability to run applications or even execute code opens up serious vulnerabilities. If an attacker finds out about your protocol, webserver, etc., they could easily inject their own stuff when you visit a website controlled by them. That's why you should at least implement two things:
a) Use some kind of authentication mechanism (e.g. HMAC). This makes exploitation at least a bit harder.
b) Never directly execute arguments as code. Always parse them and check their integrity.
Does anybody know a way in which I can write my code in JavaScript and still use basic AHK-Functionality like... hotkeys... Exo doesn't support it and ActiveScript seems to be the other way around.
Why? It seems so weird to me to set up a Hotkey and only then assigning a label to it without even referring to this specific Hotkey in any way:Hotkey, IfWinActive, ahk_class Notepad
Hotkey, ^!e, MyLabel ; Creates a hotkey that works only in Notepad.
I can't really comprehend that. And writing everything in for example C# seems a bit too... dedicated?.
I have to apologize for my bad english, it's not my main language.
» npm install node-ahk
So I've avoided trying to interact with webpages via JavaScript since I started learning AHK, but curiosity has gotten the best of me.
You guys know I have the basics of AHK down and JavaScript was my very first "learn on my own" programming language. I'm so-so with it and haven't done a lot of it in YEARS.
Would any of you be able to give me a quick crash course on how to actually pass javascript commands to a browser?
I've seen people just do direct JavaScript injection via the address bar which I find to be very neat.
Are there other ways to do it?
I understand the DOM and how to find specific elements I want to change and what not. I can write basic webpages from scratch and put script tags in there. Just needing help bridging the two worlds :)
Any takers?
u/korbo-
u/radiantcabbage
u/testerteeto
u/bitx0r
1...2...3...GO!
Your output is only the Process ID (PID). You need to run your .js in a WSH wrapper, and your .js needs to return StdOut as follows:
Here is the AHK "secret sauce" RunWaitStdOut:
MsgBox % RunWaitStdOut("C:\path_to_script\index.js")
RunWaitStdOut(command)
{
shell := ComObjCreate("WScript.Shell")
exec := shell.Exec(ComSpec " /c node " command)
return exec.StdOut.ReadAll()
}
And in the meantime, the end of your .js should have something like the following generating the StdOut:
process.stdout.write("The Result this javascript returns to AHK is " + myValue);
And remember, StdOut is a string, so if your myValue is a number or such, you may need the toString() method (as OP noticed, per OP's comment):
process.stdout.write( myValue.toString() );
Hth,
From the docs:
RunWait sets
ErrorLevelto the program's exit code (a signed 32-bit integer). If UseErrorLevel is in effect and the launch failed, the wordERRORis stored.
So you can get the exit code via ErrorLevel:
RunWait, C:\path_to_node\node.exe C:\path_to_script\index.js
output := ErrorLevel
MsgBox, %output%
Note that the exit code is a 32-bit signed integer and is set to 1 if node encounters an error. If you need proper output, use stdout, as suggested by PGilm.