Math at .AHK

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Wuff

Math at .AHK

23 Aug 2017, 13:43

Could it be possible to make a script that copies your clipboard to a variable (example: %Cliboard% is 5+3-1.2) and the the script would automaticlly calculate it and output at Term1
Example/What I HAve Got So Far:

Loop,
{
test := Clipboard
term1 := "%test%"
msgbox % term1 " = " calc( term1 )
}
User avatar
runie
Posts: 304
Joined: 03 May 2014, 14:50
Contact:

Re: Math at .AHK

23 Aug 2017, 14:39

You need to write something that will parse the string and figure out what calculations to do.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Math at .AHK

23 Aug 2017, 14:48

Try this:

Code: Select all

/* Test area
5+3-1.2
*/

F12:: MsgBox, % (Term := get_Highlight()) " = " Eval(Term)



;-------------------------------------------------------------------------------
get_Highlight() { ; get highlighted text
;-------------------------------------------------------------------------------
    _AutoTrim := A_AutoTrim             ; remember current setting
    AutoTrim, Off                       ; keep leading/trailing white spaces
    ClipStore := ClipboardAll           ; remember current content

    Send, ^c                            ; copy highlighted text
    ClipWait, 1                         ; wait for change
    Result := Clipboard                 ; store highlighted text

    Clipboard := ClipStore              ; restore previous content
    AutoTrim, %_AutoTrim%               ; restore previous setting
    Return, Result
}



;-------------------------------------------------------------------------------
Eval(Exp) { ; use Javascript/COM, by TLM and tidbit
;-------------------------------------------------------------------------------
    static Constants := "E|LN2|LN10|LOG2E|LOG10E|PI|SQRT1_2|SQRT2"
    static Functions := "abs|acos|asin|atan|atan2|ceil|cos|exp|floor"
                     .  "|log|max|min|pow|random|round|sin|sqrt|tan"

    Transform, Exp, Deref, %Exp% ; support variables

    Exp := Format("{:L}", Exp) ; everything lowercase
    Exp := RegExReplace(Exp, "i)(" Constants ")", "$U1") ; constants uppercase
    Exp := RegExReplace(Exp, "i)(" Constants "|" Functions ")", "Math.$1")
    Exp := StrReplace(Exp, "Math.ceil", "Math.ceil")
    Exp := StrReplace(Exp, "Math.exp", "Math.exp")

    Obj := ComObjCreate("HTMLfile")
    Obj.Write("<body><script>document.body.innerText=eval('" Exp "');</script>")
    Return, InStr(Result := Obj.body.innerText, "body") ? "ERROR" : Result
}
I hope that helps.
User avatar
Capn Odin
Posts: 1352
Joined: 23 Feb 2016, 19:45
Location: Denmark
Contact:

Re: Math at .AHK

23 Aug 2017, 15:09

Code: Select all

Gui, Add, ActiveX, xs w500 h500 vpwb, Shell.Explorer
pwb.Navigate("about:<!DOCTYPE HTML><meta http-equiv=""X-UA-Compatible"" content=""IE=Edge"">")
#x::MsgBox, % pwb.Document.parentWindow.eval(Clipboard)
Edit:

Code: Select all

Gui, Add, ActiveX, vpwb, Shell.Explorer
pwb.Navigate("about:<!DOCTYPE HTML><head><meta http-equiv=""X-UA-Compatible"" content=""IE=Edge""><script>window.onerror = function(message, url, lineNumber) {return true;};</script></head>")

#x::
	t := Clipboardall
	Clipboard := ""
	Send, ^c
	ClipWait, 2
	try {
		if(StrLen(res := pwb.Document.parentWindow.eval("try {" Clipboard "} catch (e) { }"))) {
			Clipboard .= " = " res
			Send, ^v
		}
	}
	Clipboard := t
return
Last edited by Capn Odin on 23 Aug 2017, 17:15, edited 1 time in total.
Please excuse my spelling I am dyslexic.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Math at .AHK

23 Aug 2017, 17:13

You could try using ExecScript from:
Run / RunWait
https://autohotkey.com/docs/commands/Run.htm#Examples

Code: Select all

q:: ;one example via InputBox
vDefault := "1 + 2 - Sqrt(9) * 4 / 5 ** 6"
InputBox, vCalc,, % "write out an AutoHotkey-style calculation e.g. 1+2+3",,,,,,,, % vDefault
if ErrorLevel
	return
vRet := ExecScript("FileAppend % (" vCalc "), *")
MsgBox % vScript " = " vRet
return

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

w:: ;multiple calculations including the use of a custom mathematical function
vList := "Cbrt(1),Cbrt(8),Cbrt(27),Cbrt(64)"
vScript := " ;continuation section
(Join`r`n
Cbrt(vNum)
{
	return vNum**(1/3)
}
)"

Loop, Parse, vList, % ","
	vScript .= "`r`nvOutput .= """ A_LoopField " = "" (" A_LoopField ") ""``r``n"""
vScript .= "`r`nFileAppend, % vOutput, *"
MsgBox, % vScript
vRet := ExecScript(vScript)
MsgBox, % SubStr(vRet, 1, -2)
return

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

;Run / RunWait
;https://autohotkey.com/docs/commands/Run.htm#Examples

; ExecScript: Executes the given code as a new AutoHotkey process.
ExecScript(Script, Wait:=true)
{
    shell := ComObjCreate("WScript.Shell")
    exec := shell.Exec("AutoHotkey.exe /ErrorStdOut *")
    exec.StdIn.Write(script)
    exec.StdIn.Close()
    if Wait
        return exec.StdOut.ReadAll()
}

;==================================================
You could also try typing calculations direct into Google, or into:
Wolfram|Alpha: Computational Knowledge Engine
http://www.wolframalpha.com/

Other links:
Math - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=29602
eval (using JS/COM) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=15389
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
WuffTheCoder
Posts: 35
Joined: 23 Aug 2017, 13:35

Thanks wolf_II

24 Aug 2017, 02:02

But is there a a way replace characters so the script could understand example:
the platform that im using the script on has "x" instead of "*" for multipliyng
and ":" for "/"
Please Help
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Math at .AHK

24 Aug 2017, 02:18

I would leave the functions as they are, they are tested and reliable. Only change them to fix bugs.
So what else is there? you can do the necessary replacements in the hotkey subroutine like so:
Try this:

Code: Select all



;-------------------------------------------------------------------------------
F12:: ; hotkey
;-------------------------------------------------------------------------------
    Term := get_Highlight()             ; get the text

    Tnew := StrReplace(Term, "x", "*")  ; replace multiplication
    Tnew := StrReplace(Tnew, ":", "/")  ; replace division
    Tnew := StrReplace(Tnew, ",", ".")  ; replace decimal delimiter

    MsgBox, % Term " = " Eval(Tnew)

Return



;-------------------------------------------------------------------------------
get_Highlight() { ; get highlighted text
;-------------------------------------------------------------------------------
    _AutoTrim := A_AutoTrim             ; remember current setting
    AutoTrim, Off                       ; keep leading/trailing white spaces
    ClipStore := ClipboardAll           ; remember current content

    Send, ^c                            ; copy highlighted text
    ClipWait, 1                         ; wait for change
    Result := Clipboard                 ; store highlighted text

    Clipboard := ClipStore              ; restore previous content
    AutoTrim, %_AutoTrim%               ; restore previous setting
    Return, Result
}



;-------------------------------------------------------------------------------
Eval(Exp) { ; use Javascript/COM, by TLM and tidbit
;-------------------------------------------------------------------------------
    static Constants := "E|LN2|LN10|LOG2E|LOG10E|PI|SQRT1_2|SQRT2"
    static Functions := "abs|acos|asin|atan|atan2|ceil|cos|exp|floor"
                     .  "|log|max|min|pow|random|round|sin|sqrt|tan"

    Transform, Exp, Deref, %Exp% ; support variables

    Exp := Format("{:L}", Exp) ; everything lowercase
    Exp := RegExReplace(Exp, "i)(" Constants ")", "$U1") ; constants uppercase
    Exp := RegExReplace(Exp, "i)(" Constants "|" Functions ")", "Math.$1")
    Exp := StrReplace(Exp, "Math.ceil", "Math.ceil")
    Exp := StrReplace(Exp, "Math.exp", "Math.exp")

    Obj := ComObjCreate("HTMLfile")
    Obj.Write("<body><script>document.body.innerText=eval('" Exp "');</script>")
    Return, InStr(Result := Obj.body.innerText, "body") ? "ERROR" : Result
}
I hope that helps. Untested , I don't know where you want to use it.

Of course you can do whatever you want with any of the code, just saying, I would change the add-ons not the back bone.
User avatar
WuffTheCoder
Posts: 35
Joined: 23 Aug 2017, 13:35

Re: Math at .AHK

24 Aug 2017, 02:25

Thanks this works perfectly
User avatar
WuffTheCoder
Posts: 35
Joined: 23 Aug 2017, 13:35

Re: Math at .AHK

24 Aug 2017, 02:33

Also could this work with SplashText Instead Of Msgbox?
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Math at .AHK

24 Aug 2017, 02:50

Try this:

Code: Select all



;-------------------------------------------------------------------------------
F10:: ; hotkey for MsgBox
;-------------------------------------------------------------------------------
    Term := get_Highlight()
    Tnew := custom_replace(Term)
    MsgBox, % Term " = " Eval(Tnew)

Return



;-------------------------------------------------------------------------------
F11:: ; hotkey for SplashText
;-------------------------------------------------------------------------------
    Term := get_Highlight()
    Tnew := custom_replace(Term)
    SplashTextOn,,, % Term " = " Eval(Tnew)
    SetTimer, Off, -2500

Return



;-------------------------------------------------------------------------------
F12:: ; hotkey for Tooltip
;-------------------------------------------------------------------------------
    Term := get_Highlight()
    Tnew := custom_replace(Term)
    ToolTip, % Term " = " Eval(Tnew)
    SetTimer, Off, -2500

Return



Off:
    ToolTip ; Off
    SplashTextOFF
Return



;-------------------------------------------------------------------------------
custom_replace(Term) { ; custom replacements
;-------------------------------------------------------------------------------
    Tnew := StrReplace(Term, "x", "*")  ; replace multiplication
    Tnew := StrReplace(Tnew, ":", "/")  ; replace division
    Tnew := StrReplace(Tnew, ",", ".")  ; replace decimal delimiter

    Return, Tnew
}



;-------------------------------------------------------------------------------
get_Highlight() { ; get highlighted text
;-------------------------------------------------------------------------------
    _AutoTrim := A_AutoTrim             ; remember current setting
    AutoTrim, Off                       ; keep leading/trailing white spaces
    ClipStore := ClipboardAll           ; remember current content

    Send, ^c                            ; copy highlighted text
    ClipWait, 1                         ; wait for change
    Result := Clipboard                 ; store highlighted text

    Clipboard := ClipStore              ; restore previous content
    AutoTrim, %_AutoTrim%               ; restore previous setting
    Return, Result
}



;-------------------------------------------------------------------------------
Eval(Exp) { ; use Javascript/COM, by TLM and tidbit
;-------------------------------------------------------------------------------
    static Constants := "E|LN2|LN10|LOG2E|LOG10E|PI|SQRT1_2|SQRT2"
    static Functions := "abs|acos|asin|atan|atan2|ceil|cos|exp|floor"
                     .  "|log|max|min|pow|random|round|sin|sqrt|tan"

    Transform, Exp, Deref, %Exp% ; support variables

    Exp := Format("{:L}", Exp) ; everything lowercase
    Exp := RegExReplace(Exp, "i)(" Constants ")", "$U1") ; constants uppercase
    Exp := RegExReplace(Exp, "i)(" Constants "|" Functions ")", "Math.$1")
    Exp := StrReplace(Exp, "Math.ceil", "Math.ceil")
    Exp := StrReplace(Exp, "Math.exp", "Math.exp")

    Obj := ComObjCreate("HTMLfile")
    Obj.Write("<body><script>document.body.innerText=eval('" Exp "');</script>")
    Return, InStr(Result := Obj.body.innerText, "body") ? "ERROR" : Result
}
I hope that helps.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: prototype_zero, w_i_k_i_d and 314 guests