AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

how to reduce lines in this code

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
help_me
Guest





PostPosted: Sat Mar 20, 2010 10:22 pm    Post subject: how to reduce lines in this code Reply with quote

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

PostPosted: Sat Mar 20, 2010 10:40 pm    Post subject: Reply with quote

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
View user's profile Send private message
Leef_me



Joined: 08 Apr 2009
Posts: 5336
Location: San Diego, California

PostPosted: Sat Mar 20, 2010 10:55 pm    Post subject: Reply with quote

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
View user's profile Send private message
help_me
Guest





PostPosted: Sat Mar 20, 2010 11:08 pm    Post subject: Reply with quote

Thanks to both of you for your help. The solution provided by Leef_me works great. Smile
Back to top
jaco0646



Joined: 07 Oct 2006
Posts: 3113
Location: MN, USA

PostPosted: Sun Mar 21, 2010 3:42 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
a_h_k



Joined: 02 Feb 2008
Posts: 626

PostPosted: Sun Mar 21, 2010 4:10 am    Post subject: Re: how to reduce lines in this code Reply with quote

Or this somewhat obvious way... Smile
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
View user's profile Send private message Visit poster's website
help_me
Guest





PostPosted: Sun Mar 21, 2010 5:30 am    Post subject: Reply with quote

Thank you a_h_k. Your solution wins the first prize. Very Happy

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

PostPosted: Sun Mar 21, 2010 6:01 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
help_me
Guest





PostPosted: Sun Mar 21, 2010 11:12 am    Post subject: Reply with quote

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
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group