Page 1 of 1

Math

Posted: 24 Mar 2017, 17:27
by Jay Kyburz
Anybody know if it possible, or better yet, if somebody has already done it.

I would like a script that can take whatever text is selected and if its a maths expression, replace the selection with the result.

So for example, if I select 10+2 and use the hot key, the text will be replaced with 12.

Re: Math

Posted: 24 Mar 2017, 18:21
by Helgef

Re: Math

Posted: 24 Mar 2017, 18:22
by wolf_II
Also try this (credit to TLM and tidbit):

Code: Select all

F1:: Send, % Eval(get_Highlight())



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

    Clipboard := ""                     ; empty clipboard
    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
    VarSetCapacity(ClipStore, 0)        ; free memory

    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")

    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.

Re: Math

Posted: 24 Mar 2017, 20:52
by Jay Kyburz
Thanks Guys. Perfect!

Re: Math

Posted: 06 Aug 2017, 06:30
by jeeswg
This Eval function is great. Btw there is an issue with E being replaced before ceil or exp, it appears that the function names must be lowercase. A quick fix is:

Code: Select all

    Exp := StrReplace(Exp, "Math.ceil", "Math.ceil")
    Exp := StrReplace(Exp, "Math.exp", "Math.exp")
Btw is there a good list of functions somewhere? And is there a way to choose a random integer other than via random and floor/ceil? Cheers.

[EDIT:] It looks like there are some answers here:
Math.random() - JavaScript | MDN
https://developer.mozilla.org/en-US/doc ... ath/random

Re: Math

Posted: 06 Aug 2017, 08:35
by wolf_II
Thanks jeeswg. I posted about you finding a bug and a fix in this thread: https://autohotkey.com/boards/viewtopic ... 134#p78134