AutoHotkey Community

It is currently May 27th, 2012, 3:07 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: March 20th, 2010, 11:22 pm 
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?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 20th, 2010, 11:40 pm 
Offline

Joined: December 29th, 2009, 7:02 pm
Posts: 12
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 20th, 2010, 11:55 pm 
Offline

Joined: April 8th, 2009, 7:49 pm
Posts: 6071
Location: San Diego, California
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 21st, 2010, 12:08 am 
Thanks to both of you for your help. The solution provided by Leef_me works great. :)


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 21st, 2010, 4:42 am 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
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


Report this post
Top
 Profile  
Reply with quote  
PostPosted: March 21st, 2010, 5:10 am 
Offline

Joined: February 2nd, 2008, 4:35 am
Posts: 643
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 21st, 2010, 6:30 am 
Thank you a_h_k. Your solution wins the first prize. :D

And it's not that obvious. Unless confronted with situations like this, it's easy to miss the utility of A_ThisHotKey.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 21st, 2010, 7:01 am 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
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.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 21st, 2010, 12:12 pm 
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.


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Google [Bot], rbrtryn, Yahoo [Bot] and 27 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