Math Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Jay Kyburz

Math  Topic is solved

24 Mar 2017, 17:27

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.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Math

24 Mar 2017, 18:22

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.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Math

06 Aug 2017, 06:30

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
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Math

06 Aug 2017, 08:35

Thanks jeeswg. I posted about you finding a bug and a fix in this thread: https://autohotkey.com/boards/viewtopic ... 134#p78134

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], erikals and 25 guests