Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

AutoHotkey: move the cursor based on string

Hello experts,
I have the following AutoHotkey which allows me to send a comment block:
:*:blc::
FormatTime,CurrentDateTime3,,yyyy/MM/dd HH:mm:ss
If ((WinActive("ahk_exe excel.exe")) or (WinActive("ahk_exe vbsedit.exe")))
  CommentBlock:="' *************************************************************************`n"
              . "' Author: `n"
              . "' Creation date: " . CurrentDateTime3 . "`n"
              . "' Description:`n"
              . "' ***************************************************************************`n"
Else
If (WinActive("ahk_exe notepad++.exe"))
  CommentBlock:="Rem ======================================================================`n"
              . "Rem Author: `n"
              . "Rem Creation date: " . CurrentDateTime3 . "`n"
              . "Rem Description:`n"
              . "Rem ======================================================================`n"
Else
If (WinActive("ahk_exe powershell_ise.exe"))
 CommentBlock:="# ===========================================================================`n"
              . "# Author:  `n"
              . "# Creation date: " . CurrentDateTime3 . "`n"
              . "# Description:`n"
              . "# ===========================================================================`n"
Else
If (WinActive("ahk_exe sciTE.exe"))
 CommentBlock:=";====================================`n"
              . ";`n"
              . ";====================================`n"
Else
If (WinActive("ahk_exe ssms.exe"))
 CommentBlock:="-- ===========================================================================`n"
              . "-- Author: `n"
              . "-- Creation date: " . CurrentDateTime3 . "`n"
              . "-- Description:`n"
              . "-- ===========================================================================`n"
Else
{
  MsgBox,4144,Error,Active window is not a supported program
  Return
}
SendRaw %CommentBlock%
Return

Open in new window

I would like to put the cursor at the end of description for all the process which contain description string at comment block and to the beginning of second line for sciTE.exe which doesn't contain description string.
if you have questions, please contact me.
Thank you for your help.
SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Luis Diaz

ASKER

Thank you Bill for your comment.
Another approach will be to avoid searching a string a send directly the keys since you know that the position will be the same no matter the application in which you send  the %CommentBlock%.
I was thinking to do like this:
SendRaw %CommentBlock%
Sleep, 100
Send {Up 2} 
Sleep, 100
Send {End}
Return

Open in new window

I tested in the various applications and it works.
However I would like to have an expert advice on this.
Thank you for your help.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you Bill.
To be honest with you I don't know how to set the flag as recommended.
Hi Luis,
I've been AFK for a while and in case Bill is now, I'll jump in. You don't actually need the flag mechanism, because there will always be a non-null CommentBlock when you reach the SendRaw (due to the Return in the Else path above it). So it is safe to put your Up 2 AutoHotkey code right after the SendRaw...CommentBlock (and, thus, Description) will never be null at that point. Regards, Joe
Thank you Joe.
So it is safe to put your Up 2 AutoHotkey code right after the SendRaw...CommentBlock
Without a sleep prior the sendraw or can I put an sleep without an issue?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Noted. Thank you.
You're welcome, Luis, happy to help. Regards, Joe