| View previous topic :: View next topic |
| Author |
Message |
help_me Guest
|
Posted: Sat Mar 20, 2010 10:22 pm Post subject: how to reduce lines in this code |
|
|
Hi I use the following hotkeys to copy text from any editor, process it using one of the many routines and then paste it back. In all these routines, only one line is different. how can i reduce lines and optimise code? thanks
| Code: | ; work in any editor
^+F9::
Send ^a ; select all
Sleep, 250 ; this improves reliability
Clipboard = ; start off with blank clipboard to improve reliability
Send ^c ; copy the full contents of the editor
ClipWait, 2 ; wait for clipboard to contain text
Clipboard := process1(Clipboard)
Send ^v ; paste the processed data
Sleep, 250 ; give time to paste before emptying clipboard
Clipboard = ; empty the clipboard after use
Return
^+F10::
Send ^a ; select all
Sleep, 250 ; this improves reliability
Clipboard = ; start off with blank clipboard to improve reliability
Send ^c ; copy the full contents of the editor
ClipWait, 2 ; wait for clipboard to contain text
Clipboard := process2(Clipboard)
Send ^v ; paste the processed data
Sleep, 250 ; give time to paste before emptying clipboard
Clipboard = ; empty the clipboard immediately after use
Return
^+F11::
Send ^a ; select all
Sleep, 250 ; this improves reliability
Clipboard = ; start off with blank clipboard to improve reliability
Send ^c ; copy the full contents of the editor
ClipWait, 2 ; wait for clipboard to contain text
Clipboard := process3(Clipboard)
Send ^v ; paste the processed data
Sleep, 250 ; give time to paste before emptying clipboard
Clipboard = ; empty the clipboard immediately after use
Return |
Only the red part is different in these three routines. how can i condense code? |
|
| Back to top |
|
 |
Long Bong
Joined: 29 Dec 2009 Posts: 12
|
Posted: Sat Mar 20, 2010 10:40 pm Post subject: |
|
|
Try this maybe [UNTESTED THOUGH]:
| Code: | ; work in any editor
FKeyNumber = 9
Loop, 3
{
j := A_Index
HotKey, ^+F%FKeyNumber%, HotKeyMaker
FKeyNumber += 1
}
return
HotKeyMaker:
Send ^a ; select all
Sleep, 250 ; this improves reliability
Clipboard = ; start off with blank clipboard to improve reliability
Send ^c ; copy the full contents of the editor
ClipWait, 2 ; wait for clipboard to contain text
Clipboard := process%j%(Clipboard)
Send ^v ; paste the processed data
Sleep, 250 ; give time to paste before emptying clipboard
Clipboard = ; empty the clipboard after use
return |
|
|
| Back to top |
|
 |
Leef_me
Joined: 08 Apr 2009 Posts: 5336 Location: San Diego, California
|
Posted: Sat Mar 20, 2010 10:55 pm Post subject: |
|
|
I don't know if you can call a function by using a variable with its full name. I know you can call a subroutine this way.
Since all the routines read from and write to clipboard, let the routined do the reading & writing
within their framework instead of using temp variables as function parameters and as return parameters.
Tested.
| Code: |
F1::
param=process1
goto common
F2::
param=process2
goto common
F3::
param=process3
goto common
^+F10::
^+F11::
common: ; these are the lines that are repeated for each hotkey
; whatever
gosub, %param%
; whatever else
Return
process1:
msgbox a
return
process2:
msgbox b
return
process3:
msgbox c
return
|
|
|
| Back to top |
|
 |
help_me Guest
|
Posted: Sat Mar 20, 2010 11:08 pm Post subject: |
|
|
Thanks to both of you for your help. The solution provided by Leef_me works great.  |
|
| Back to top |
|
 |
jaco0646
Joined: 07 Oct 2006 Posts: 3113 Location: MN, USA
|
Posted: Sun Mar 21, 2010 3:42 am Post subject: |
|
|
| Leef_me wrote: | | I don't know if you can call a function by using a variable with its full name. | Dynamically Calling a Function |
|
| Back to top |
|
 |
a_h_k
Joined: 02 Feb 2008 Posts: 626
|
Posted: Sun Mar 21, 2010 4:10 am Post subject: Re: how to reduce lines in this code |
|
|
Or this somewhat obvious way...
| Code: | ^+F9::
^+F10::
^+F11::
Send ^a ; select all
Sleep, 250 ; this improves reliability
Clipboard = ; start off with blank clipboard to improve reliability
Send ^c ; copy the full contents of the editor
ClipWait, 2 ; wait for clipboard to contain text
If A_ThisHotkey = ^+F10
Clipboard := process1(Clipboard)
Else If A_ThisHotkey = ^+F11
Clipboard := process2(Clipboard)
Else If A_ThisHotkey = ^+F12
Clipboard := process3(Clipboard)
Send ^v ; paste the processed data
Sleep, 250 ; give time to paste before emptying clipboard
Clipboard = ; empty the clipboard after use
Return |
|
|
| Back to top |
|
 |
help_me Guest
|
Posted: Sun Mar 21, 2010 5:30 am Post subject: |
|
|
Thank you a_h_k. Your solution wins the first prize.
And it's not that obvious. Unless confronted with situations like this, it's easy to miss the utility of A_ThisHotKey. |
|
| Back to top |
|
 |
MasterFocus
Joined: 08 Apr 2009 Posts: 3035 Location: Rio de Janeiro - RJ - Brasil
|
Posted: Sun Mar 21, 2010 6:01 am Post subject: |
|
|
| Code: | ^+F9::
^+F10::
^+F11::
Send ^a
Sleep, 250
Clipboard := "", Aux := 1 + Mod( SubStr(A_ThisHotkey,4) , 9 )
Send ^c
ClipWait, 2
Clipboard := process%Aux%(Clipboard)
Send ^v
Sleep, 250
Clipboard := ""
Return |
_________________ "Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried."
Antonio França
My stuff: Google Profile |
|
| Back to top |
|
 |
help_me Guest
|
Posted: Sun Mar 21, 2010 11:12 am Post subject: |
|
|
| Code: | | Aux := 1 + Mod( SubStr(A_ThisHotkey,4) , 9 ) |
Thanks for the new idea. This is a pretty interesting way to get 1, 2 and 3 from the assigned hotkeys and so far as reducing lines is concerned, it's great. But it loses out a bit on flexibility. It will be difficult if I have to change the hotkeys to something with non-consecutive numbers or to something non-numeric. |
|
| Back to top |
|
 |
|