AutoHotkey Community

It is currently May 27th, 2012, 11:55 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: October 1st, 2010, 7:46 pm 
Offline

Joined: October 9th, 2004, 8:55 pm
Posts: 217
Location: Budapest, Hungary
I needed this eval feature in AHK and I thought vbscript could be used, because it is installed on Windows by default AFAIK.

This script contains the necessary VB code in itself and creates the VB script on demand, so it can be used in simple cases when high performance (lots of calculations in a short time) is not required:

Code:
str_to_eval :=  "(8 * 9) / 32 + 12"

evalscript := A_Temp . "\eval_for_ahk.vbs"
evalresult := A_Temp . "\eval_for_ahk.result"

IfNotExist %evalscript%
  FileAppend,  ; The comma is required in this case.
(
On Error Resume Next
Set objFile = CreateObject("Scripting.FileSystemObject").CreateTextFile(WScript.Arguments.Item(1))
objFile.WriteLine(Eval(WScript.Arguments.Item(0)))
objFile.Close
), %evalscript%


RunWait, %comspec% /c %evalscript% "%str_to_eval%" "%evalresult%", , hide UseErrorLevel

if (errorlevel <> "ERROR")
{
  fileread eval_result, %evalresult%
  if (eval_result == "")
    msgbox eval error
  else
    msgbox % eval_result
}




Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 1st, 2010, 7:56 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
Did you saw Monster: evaluate math expressions in strings by Laszlo?

or Embedded Windows Scripting (VBScript & JScript) and COM by erictheturtle?

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 1st, 2010, 8:01 pm 
Offline

Joined: October 9th, 2004, 8:55 pm
Posts: 217
Location: Budapest, Hungary
Yes, but that's an external dependency and this solution is self contained (aside from the VB dependency, but that's given).

Anyway, for serious purposes Monster or the COM solution may be better, but for my simple needs this simple solution is enough.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 2nd, 2010, 3:12 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
If you copy Monster/ws4ahk into your script, they are self contained and do not require temporary files. If you use AutoHotkey_L, which has built-in COM support among other things, VBScript/JScript can be evaluated like so:
Code:
sc := ComObjCreate("ScriptControl"), sc.Language := "VBScript"
MsgBox % sc.Eval("(8 * 9) / 32 + 12")

Variables can also be used between calls:
Code:
sc.ExecuteStatement("x = (8 * 9) / 32 + 12")
MsgBox % sc.Eval("x * 2")


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Google Feedfetcher, notsoobvious, rrhuffy, Yahoo [Bot] and 28 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group