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.
Math
Re: Math
Try this, in-line calculator.
Re: Math
Also try this (credit to TLM and tidbit):
I hope that helps.
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
}
Re: Math
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:
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
Code: Select all
Exp := StrReplace(Exp, "Math.ceil", "Math.ceil")
Exp := StrReplace(Exp, "Math.exp", "Math.exp")
[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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Math
Thanks jeeswg. I posted about you finding a bug and a fix in this thread: https://autohotkey.com/boards/viewtopic ... 134#p78134