Suggestion (if not already implented)

12 views
Skip to first unread message

Johnny Rosenberg

unread,
Dec 5, 2009, 7:36:25 AM12/5/09
to autoke...@googlegroups.com
It would be nice with some kind of calculating functionality, like
expanding a formula to its result, maybe using a syntax like
OpenOffice.org Calc/MS Excel and so on:

=2+3/8
should expand to:
2,375 (or 2.375 for those who use ”.” as decimal separator)

Maybe including some nice functions, such as sqr(x) and pwr(x,y). Some
contants woudl also be nice, such as π (pi) and a few more.

But to begin with, +-*/^ would do, I guess.

Is this already doable with a script, perhaps?

Johnny Rosenberg

Derek

unread,
Dec 7, 2009, 12:25:37 PM12/7/09
to autokey-users
Johnny,

Yes, it's already there. All of the Python language is available to
your scripts, so for example:

# get square of same area as circle
import math
from __future__ import division # Ensures floating point division.
radius_text = "2+3/8"
radius = eval(radius_text) # Evaluates string as a Python expression.
circle_area = math.pi * math.pow(radius, 2)
square_side = math.sqrt(circle_area)
square_area = math.pow(square_side, 2)
square_side_text = str(square_side) # Converts number to string.
square_side_text_alt = square_side_text.replace('.', ',')

Check out http://docs.python.org/library/math.html

Derek

Johnny Rosenberg

unread,
Dec 7, 2009, 12:46:11 PM12/7/09
to autoke...@googlegroups.com
2009/12/7 Derek <dv8bo...@yahoo.com>:
> Johnny,
>
> Yes, it's already there.  All of the Python language is available to
> your scripts, so for example:
>
> # get square of same area as circle
> import math
> from __future__ import division  # Ensures floating point division.
> radius_text = "2+3/8"
> radius = eval(radius_text)  # Evaluates string as a Python expression.
> circle_area = math.pi * math.pow(radius, 2)
> square_side = math.sqrt(circle_area)
> square_area = math.pow(square_side, 2)
> square_side_text = str(square_side)  # Converts number to string.
> square_side_text_alt = square_side_text.replace('.', ',')
>
> Check out http://docs.python.org/library/math.html
>
> Derek

So I need to be some kind of a programmer to implement a thing like that?

Johnny Rosenberg

Luke Faraone

unread,
Dec 7, 2009, 12:53:45 PM12/7/09
to autokey-users
On Mon, Dec 7, 2009 at 12:46, Johnny Rosenberg <gurus....@gmail.com> wrote:
So I need to be some kind of a programmer to implement a thing like that?

Well, just as you needed to learn the syntax for using Excel's functions, if you want to do advanced calculations with Autokey, you need to use Python's functions.

Autokey lets you write in a scripting language, and one that is already widely used outside of Autokey. There are plenty of resources for learning it, but the basics you need to know are covered in Derek's post.

--
Luke Faraone
http://luke.faraone.cc

Johnny Rosenberg

unread,
Dec 7, 2009, 12:59:46 PM12/7/09
to autoke...@googlegroups.com
2009/12/7 Luke Faraone <lu...@faraone.cc>:
OK. Thanks (for making me feel stupid - I really love that feeling…).

Johnny Rosenberg

Derek

unread,
Dec 7, 2009, 1:07:12 PM12/7/09
to autokey-users
Johnny,

:-D no

I think this script may do what you have in mind:

# Evaluate selected text and replace it with a number
from __future__ import division # Ensures floating point division.
from math import *
text = clipboard.get_selection()
number = eval(text)
output = str(number)
clipboard.fill_clipboard(output)
keyboard.send_keys(output)

Put this script in AutoKey, assign it a hotkey, then select some text
like...

2*4
or
pi*pow(4, 2)
or
sqrt(2)

When you press the hotkey, the selected text will be evaluated and
replaced with the result. Check that link I gave you for more math
functions you can use.

Derek

On Dec 7, 9:59 am, Johnny Rosenberg <gurus.knu...@gmail.com> wrote:
> 2009/12/7 Luke Faraone <l...@faraone.cc>:
>
>
>
> > On Mon, Dec 7, 2009 at 12:46, Johnny Rosenberg <gurus.knu...@gmail.com>

Derek

unread,
Dec 7, 2009, 1:09:48 PM12/7/09
to autokey-users
Actually, that last line should be...

keyboard.send_keys("<ctrl>+v")

Derek

Chris Dekter

unread,
Dec 7, 2009, 6:57:47 PM12/7/09
to autoke...@googlegroups.com
Good work, this is exactly how I would have done it.

2009/12/8 Derek <dv8bo...@yahoo.com>:

Johnny Rosenberg

unread,
Dec 9, 2009, 12:05:04 PM12/9/09
to autoke...@googlegroups.com
2009/12/8 Chris Dekter <cde...@gmail.com>:
> Good work, this is exactly how I would have done it.
>
> 2009/12/8 Derek <dv8bo...@yahoo.com>:
>> Johnny,
>>
>> :-D  no
>>
>> I think this script may do what you have in mind:
>>
>> # Evaluate selected text and replace it with a number
>> from __future__ import division  # Ensures floating point division.
>> from math import *
>> text = clipboard.get_selection()
>> number = eval(text)
>> output = str(number)
>> clipboard.fill_clipboard(output)
>> keyboard.send_keys(output)
>>
>> Put this script in AutoKey, assign it a hotkey, then select some text
>> like...
>>
>>    2*4

Assigned the script to alt+ctrl+shift+m. Highlighted ”2*4”, pressed
ctrl+shift+alt+m, nothing happened. Hit ctrl+v, the result was 88… (8
typed twice).

But it wouldn't be possible to assignt a script to ”=”, evaluating the
text after the ”=” when hitting a space or Enter?
Such as this:
Key Display
= =
1 =1
+ =1+
2 =1+2
↵ 3


Johnny Rosenberg

Chris Dekter

unread,
Dec 9, 2009, 6:13:37 PM12/9/09
to autoke...@googlegroups.com
2009/12/10 Johnny Rosenberg <gurus....@gmail.com>:
> Assigned the script to alt+ctrl+shift+m. Highlighted ”2*4”, pressed
> ctrl+shift+alt+m, nothing happened. Hit ctrl+v, the result was 88… (8
> typed twice).

I would get rid of the clipboard.fill_clipboard() statement. It's
better to just send the output via keyboard.send_keys(). Either that
or cross-check your script against Derek's original script - it
doesn't quite match and that's why it's not working for you.

> But it wouldn't be possible to assignt a script to ”=”, evaluating the
> text after the ”=” when hitting a space or Enter?
> Such as this:
> Key    Display
> =             =
> 1             =1
> +             =1+
> 2             =1+2
> ↵             3
>

At the moment this is not possible. You would need access in your
script to AutoKey's internal key buffer - not impossible to set up,
just not possible as it stands.

Johnny Rosenberg

unread,
Dec 10, 2009, 3:33:30 PM12/10/09
to autoke...@googlegroups.com
2009/12/10 Chris Dekter <cde...@gmail.com>:
> 2009/12/10 Johnny Rosenberg <gurus....@gmail.com>:
>> Assigned the script to alt+ctrl+shift+m. Highlighted ”2*4”, pressed
>> ctrl+shift+alt+m, nothing happened. Hit ctrl+v, the result was 88… (8
>> typed twice).
>
> I would get rid of the clipboard.fill_clipboard() statement. It's
> better to just send the output via keyboard.send_keys(). Either that
> or cross-check your script against Derek's original script - it
> doesn't quite match and that's why it's not working for you.

Strange… I copied and pasted it directly… and I can't see the
difference. Here's what I have:

from __future__ import division # Ensures floating point division.
from math import *
text = clipboard.get_selection()
number = eval(text)
output = str(number)
clipboard.fill_clipboard(output)
keyboard.send_keys("<ctrl>+v")

Derek originally had ”keyboard.send_keys(output)” as his last row, but
he said that I should replace it by the one I have now:
Derek said: ”Actually, that last line should be...

keyboard.send_keys("<ctrl>+v")”

Or am I missing something else?

Johnny Rosenberg

Derek

unread,
Dec 10, 2009, 4:27:11 PM12/10/09
to autokey-users
Johnny,

That all looks correct. Maybe you are using it in a different
environment? I have Ubuntu 8.04, and I'm using "AutoKey (GTK UI)
0.61.0a". I assigned it to Alt+Shift+8. It works in Gedit and it
works here in Google Groups in Firefox. I haven't found it to not
work anywhere.

You might try Chris's suggestion and replacing the last two lines with
this:
keyboard.send_keys(output)

I used the clipboard pasting technique because each keystroke takes a
little time, and for a longer string, like '3.14159265359' sending
Ctrl-V outputs faster.

Derek

On Dec 10, 12:33 pm, Johnny Rosenberg <gurus.knu...@gmail.com> wrote:
> 2009/12/10 Chris Dekter <cdek...@gmail.com>:
>
> > 2009/12/10 Johnny Rosenberg <gurus.knu...@gmail.com>:

Johnny Rosenberg

unread,
Dec 11, 2009, 12:41:11 PM12/11/09
to autoke...@googlegroups.com
2009/12/10 Derek <dv8bo...@yahoo.com>:
> Johnny,
>
> That all looks correct.  Maybe you are using it in a different
> environment?  I have Ubuntu 8.04, and I'm using "AutoKey (GTK UI)
> 0.61.0a".  I assigned it to Alt+Shift+8.  It works in Gedit and it
> works here in Google Groups in Firefox.  I haven't found it to not
> work anywhere.
>
> You might try Chris's suggestion and replacing the last two lines with
> this:
> keyboard.send_keys(output)
>
> I used the clipboard pasting technique because each keystroke takes a
> little time, and for a longer string, like '3.14159265359' sending
> Ctrl-V outputs faster.

Thanks. Seems to work now, when I replaced those two last lines. The
only thing now to ”complain” about is that it doesn't seem to use the
system default decimal separator (Swedish decimal separator=comma, but
I get a decimal point instead).

Johnny Rosenberg

Derek

unread,
Dec 11, 2009, 1:02:48 PM12/11/09
to autokey-users
Johnny,

There's probably a smarter way (conforming to the system default), but
as a quick fix you can just edit this line to replace periods with
commas:

output = str(number).replace('.', ',')

Derek

On Dec 11, 9:41 am, Johnny Rosenberg <gurus.knu...@gmail.com> wrote:
> 2009/12/10 Derek <dv8box-...@yahoo.com>:

Johnny Rosenberg

unread,
Dec 11, 2009, 1:19:01 PM12/11/09
to autoke...@googlegroups.com
2009/12/11 Derek <dv8bo...@yahoo.com>:
> Johnny,
>
> There's probably a smarter way (conforming to the system default), but
> as a quick fix you can just edit this line to replace periods with
> commas:
>
> output = str(number).replace('.', ',')

Did the replace, but I can't test it. Nothing is happening (that
hotkey thing doesn't work very good on my system - sometimes it works,
but most of the time it does not).

Thanks for the hint, though.


Johnny Rosenberg

P.S. Seems like Autokey crashed, so I restarted it, and now it works.
So does the replace thing. Thanks! D.S.
Reply all
Reply to author
Forward
0 new messages