AutoHotkey Community

It is currently May 24th, 2012, 6:51 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 27 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: October 11th, 2005, 7:34 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
There were several dynamic expression evaluation methods discussed here. Some of them might be of general interest, so I post an application of the simplest one here.

Evaluating arithmetic expressions in documents: Select it, and the Win-Alt-e HotKey puts the result after it. Like, in a week there are 7*24*60 = 10080 minutes. Here " = 10080" was written by the script. (In some editors the insertion point must be at the end of the expression, so select it from left-to-right.)
Code:
#!e::                    ; Evaluates dynamic expression, Paste " = " <Result>
  Send ^c
  FileDelete $temp$.ahk  ; Here will be the temp script written
  FileAppend #NoTrayIcon`nClipBoard:=%ClipBoard%, $temp$.ahk
  RunWait $temp$.ahk     ; Run AHK to execute temp script
  Send {Right} = ^v      ; Paste the result after the expression
Return
If you want, save the current ClipBoard or ClipBoardAll in the beginning and restore it at the end. (With ClipBoardAll you have ugly side effects in MS Word and WordPerfect. Other programs seem to be OK with it.)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 13th, 2005, 7:36 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Here is a somewhat enhanced version. If nothing is selected, the text in the current line is used between the insertion point and the last colon ":" on the left, after removing the colon. (If the expression spans more than one line, we have to manually select it for evaluation.) It speeds up typing, like in "There are 24*7*60 = 10080 minutes in a week". After typing "…are: 24*7*60" press the hotkey, which removes ":" and inserts " = 10080" after the expression. It works in MS Word and Notepad, but some editors copy the current line to the clipboard if nothing is selected. They need more complicated scripts, but there is probably no universal solution for every application.
Code:
#!e::                    ; Evaluate dynamic expression, Paste " = " <Result>
   ClipBoard =
   Send ^x
   ClipWait 0.25
   IfEqual ClipBoard,, {      ; No expression is selected
      Send +{Home}+{Right}^x  ; Paragraph shouldn't become empty
      ClipWait 0.25
      IfEqual ClipBoard,,Return
      StringGetPos p, ClipBoard, :, R
      StringLeft x, ClipBoard, p ; get text before last ":"
      StringTrimLeft ClipBoard, ClipBoard, p+1
      SendRaw %x%             ; Write back beginning of line
   }
   Send ^v                    ; Paste back expression
   FileDelete C:\$temp$.ahk   ; Here will be the temp script written
   FileAppend #NoTrayIcon`nClipBoard:=%ClipBoard%, C:\$temp$.ahk
   RunWait C:\$temp$.ahk      ; Run AHK to execute temp script
   Send ` = ^v                ; Paste the result after the expression
   FileDelete C:\$temp$.ahk
Return
In this version it does not matter where the insertion point is left after selecting the expression to be evaluated.


Report this post
Top
 Profile  
Reply with quote  
PostPosted: February 23rd, 2007, 4:31 pm 
Offline

Joined: February 20th, 2007, 1:37 pm
Posts: 198
Location: D.C.
One trivial modification makes it more useful for me. Simply remove the "{Right} = " and it replaces your expression with its solution.

Code:
#!e::                    ; Evaluates dynamic expression, Paste " = " <Result>
  Send ^c
  FileDelete $temp$.ahk  ; Here will be the temp script written
  FileAppend #NoTrayIcon`nClipBoard:=%ClipBoard%, $temp$.ahk
  RunWait $temp$.ahk     ; Run AHK to execute temp script
  Send ^v      ; Replace the expression with its result
Return


I am going to impress some women with this one, I'll tell you! Thanks everybody! :D


Last edited by ribbet.1 on February 23rd, 2007, 4:42 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 23rd, 2007, 4:37 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
No, you will not.

Women are not interested in math expressions, generaly speaking.

Buy a BIG chocolate and talk about celebrities all the time, and you may lay something :P

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
PostPosted: February 23rd, 2007, 6:40 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
ribbet.1 wrote:
One trivial modification makes it more useful for me.
Then use it your way. However, your script could have problems in other applications:
- Between sending ^c and using the ClipBoard there may not be enough time for Windows to set up the ClipBoard. In some applications, sometimes you will see its old content, or nothing.
- In some applications ^c removes the selection. Depending on where the insertion point was, you end up inserting the result before or after the original selection.
- It is an extra step to select the expression before evaluation. It is more convenient (faster) to get the expression selected automatically. With the new regular expression functions you can do it simply and more intelligently.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 23rd, 2007, 9:06 pm 
Offline

Joined: February 20th, 2007, 1:37 pm
Posts: 198
Location: D.C.
Well, I compiled it, tried out the compiled version, and then gave it to a girl, and Wham!! it didn't work. So you are right, this is no way to impress the ladies. You must be able to provide a working prototype at least.

I see that the script creates a temporary ahk script that it requires ahk to execute, so as it is you cannot compile this as a standalone, unless the person using it also has ahk installed. I now see that is why I can't impress women.

I want to work around this, but I don't have the time ATM. If anybody would like to suggest how to do this I would appreciate. I am new to AHK. I love this script and a few years ago spent some time looking for something just like this with no success. If it could be a standalone it would be extremely useful for just about anybody.

I eventually settled on Supercalc by Plamen Djonev, which at least integrates some of the features of a text processor with a calculator, and is still about the best calculator for my purposes. But this script really fills a need that I perceive has yet to be met by a standalone application.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 23rd, 2007, 10:25 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
If you only want arithmetic expressions to be evaluated, you can incorporate one of the expression parsers posted here.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 24th, 2007, 7:00 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8775
Very Nice Sir. :D


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 24th, 2007, 5:05 pm 
Offline

Joined: February 20th, 2007, 1:37 pm
Posts: 198
Location: D.C.
Skan wrote:
Very Nice Sir. :D

Agreed. This kind of thing should be compiled and offered as freeware, with appropriate disclaimers and credits of course. I would like to try at least one of these code snippets you showed me, and get back to you, perhaps at the page you linked instead of here.

I honestly searched several freeware sites for something like this a few years ago. And while I truly admire Mr. Djonev's Supercalc, I have always felt it could be taken further, and he is too busy with other things.

I think the ability to evaluate a simple math equation without leaving your text editor is one of those things that should just about be included with any OS, but never has been. Instead we all get our own virtual Radio Shack calculator. I have never been able to impress the ladies with that one, even the real one with its own holster.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 24th, 2007, 5:11 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
ribbet.1 wrote:
I have never been able to impress the ladies with that one, even the real one with its own holster.
Have you tried one with diamonds inlayed? :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 24th, 2007, 6:13 pm 
Offline

Joined: February 20th, 2007, 1:37 pm
Posts: 198
Location: D.C.
Laszlo wrote:
Have you tried one with diamonds inlayed? :wink:


I save that for the black tie kind of affair, not just everyday.


Report this post
Top
 Profile  
Reply with quote  
PostPosted: February 24th, 2007, 6:29 pm 
Offline

Joined: February 20th, 2007, 1:37 pm
Posts: 198
Location: D.C.
Laszlo wrote:
ribbet.1 wrote:
One trivial modification makes it more useful for me.
Then use it your way. However, your script could have problems in other applications:
- Between sending ^c and using the ClipBoard there may not be enough time for Windows to set up the ClipBoard. In some applications, sometimes you will see its old content, or nothing.
- In some applications ^c removes the selection. Depending on where the insertion point was, you end up inserting the result before or after the original selection.
- It is an extra step to select the expression before evaluation. It is more convenient (faster) to get the expression selected automatically. With the new regular expression functions you can do it simply and more intelligently.


Agreed on all counts. And BTW, I'm not familiar with AHK much yet, but I noticed a ClipboardWait function. I wonder if this would apply to the problem you mentioned.

So I am actually painting the bedroom right now, and just stopping in, but it appears to me adding a ClipboardWait at the juncture you point out, and an optional auto highlight would make this nicer for some folks. I would be more inclined to have the auto highlight be activated by a separate optional hotkey so I don't have to do it that way. It is only more convenient and intelligent insofar as that is what you want. I can easily imagine scenarios where you would not want it necessarily. For example, simplifying an equation by resolving smaller portions within it with this script.

But it would be nice to start to learn how to work w/ regexp's since I have bought the book and everything so I could do well to try that little function as well. I want to make a few search tools that would include proximity features, but that is another topic. At any rate, this is really exciting to me.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 26th, 2007, 2:57 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
I tried this script for the first time today and I am not very happy about its speed, due to the fact that it runs another ahk....


Expressions should be executed within script itself.
This can be probably done the easiest way using some COM object.

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 26th, 2007, 3:29 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
majkinetor wrote:
I am not very happy about its speed, due to the fact that it runs another ahk....
Expressions should be executed within script itself.
This can be probably done the easiest way using some COM object.
You missed my link above. There is no need for COM, the script there finishes faster than an external program or another AHK copy could be started.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 26th, 2007, 3:53 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
why didn't you provide the above script using that function to evaluate expressions ?

I dont' have time to read every post carefuly, and I imagine I am not the only one....

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 27 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Exabot [Bot], S0und and 15 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