Run javascript library within AHK

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
JoeSchmoe
Posts: 129
Joined: 08 Dec 2014, 08:58

Run javascript library within AHK

09 Apr 2023, 11:56

This is a follow-up from a v1 question from iseahound:
iseahound wrote:
07 Sep 2016, 18:04
I've been poking around for a good math eval() function on AHK but couldn't find one to suit my needs. So I decided to use mathJS, specifically this library. All I need to do is pipe a string into their eval function and return a string to AHK. However I can't seem to do this. <snip> I've tried Lexikos ActiveScript.ahk with no success either.

I'd like to get javascript libraries working with AHK because there's no need to reinvent the wheel when they've got a really nice eval() function that I desperately want.
I find myself in a situation similar to the above. I'd like to run a customized/modified version of the CommonMark Javascript Reference Markdown library. Is there any way to do this within Autohotkey?

The javascript library library will be stored in a file and could perhaps be attached to a dummy html file. Might it be possible to open the html file using Com and then, somehow, send it an argument?

The natural response is to use Pandoc instead. I'd personally rather use a customized Javascript library, but I'm desperate, so I've investigated this. I got it to work using both "ComObject("WScript.Shell").Run(cmd, 0, false)" and "ComObject("WScript.Shell").shell.Exec(cmd)," but I'm hoping to avoid:
  • clipboard use (the 'run' method - see below - uses this and it's slow and unreliable, perhaps because I'm using the clipboard twice so can't use clipwait)
  • things that flash on the screen (the 'exec' method - see below - has this problem)
  • temporary files (because I plan on running this 100s of times a day and don't want it to affect HD/SSD durability)
With that said, beggars can't be choosers, so I'm trying to get a sense of all of the approaches that are out there. For the record, here's my current code with Pandoc.

Code: Select all

; WScript.Shell: https://learn.microsoft.com/en-us/previous-versions//aew9yb99(v=vs.85)?redirectedfrom=MSDN
; Exec: https://learn.microsoft.com/en-us/previous-versions//2f38xsxe(v=vs.85)
    ; Exec offers Direct access to stdin and stdout but is painfully slow and flashes a huge black command prompt.
; Run https://learn.microsoft.com/en-us/previous-versions//d5fk67ky(v=vs.85)
    ; Run is Unpredictable because it depends on copy and paste.

; For Pandoc formats: https://pandoc.org/MANUAL.html#general-options
; https://pandoc.org/MANUAL.html#markdown-variants  gfm=Github Markdown
Pandoc(input:='*hi*', fromformat:='gfm', toformat:='html', method:='run', runsleep:=300) {
    If method = 'run' {
        clipSaved := ClipboardAll()
        a_clipboard := input

        ; how to get paste.exe: https://gist.github.com/jpflouret/19da43372e643352a1bf  Reproduced here: C:\Program Files\Pandoc
        cmd := 'cmd /c ""C:\Program Files\Pandoc\paste.exe" | "C:\Program Files\Pandoc\pandoc.exe" -t ' toformat ' -f ' fromformat ' | clip"'
        ; thanks @swagfag: https://www.autohotkey.com/boards/viewtopic.php?style=2&t=59286
        ComObject("WScript.Shell").Run(cmd, 0, false)

        Sleep(runsleep)
        retval := A_Clipboard
        A_Clipboard := clipSaved
        return retval
    } else if method = 'exec' {
        shell := ComObject("WScript.Shell")
        cmd := 'C:\Program Files\Pandoc\pandoc.exe -t ' toformat ' -f ' fromformat
        exec := shell.Exec(cmd)
        exec.StdIn.Write(input)
        exec.StdIn.Close()
        return exec.StdOut.ReadAll()
    } else msgbox "Error!"

    }
Bottom line: any way to run a javascript library from AHK? Any other suggestions?

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Descolada and 22 guests