AutoHotkey: send block comment based on activewindows process name

Luis Diaz
Luis Diaz used Ask the Experts™
on
Hello experts,
I was wondering how to set up an AutoHotkey to send comment block based on activewindows.
I need to send:
'-- ======================================================================
'-- Author:
'-- Creation date:
'-- Description:
'-- ======================================================================

Open in new window

If Ssms.exe process name is active.


Rem ======================================================================
Rem Author:
Rem Creation date:
Rem Description:
Rem ======================================================================

Open in new window

If Notepad++ process name is active.

'*************************************************************************
' Author:
' Creation date:
' Description:
'***************************************************************************

Open in new window


If excel.exe process or vbsedit.exe process name is active.

#===========================================================================
# Author:
# Creation date:
# Description:
#===========================================================================

Open in new window


If powershell_ise.exe is active.

I attached the various windows related to each process.
If you have questions, please contact me.
VBA.png
Ssms.png
VBS.png
Notepadplusplus.png
Powershell.png
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Joe WinogradDeveloper
Fellow 2017
Most Valuable Expert 2018

Commented:
Hi Luis,

I'll be happy to provide an AutoHotkey script to do this, but I'm wondering where things stand on two other questions of yours for which I've presented solutions:

Autohotkey: Open/edit file based on file extension
AutoHotkey: copy path of office documents

Regards, Joe
Joe WinogradDeveloper
Fellow 2017
Most Valuable Expert 2018

Commented:
> If powershell_ise.exe is active.

What you want for PowerShell is missing from your post, but since PowerShell uses hash (#) for commenting a single line, I'll use the same text that you posted for excel.exe/vbsedit.exe (but let me know if you want something different).
Developer
Fellow 2017
Most Valuable Expert 2018
Commented:
Hi Luis,
Based on the screenshots you posted (and my comment above about PowerShell), this AutoHotkey script should do what you want:

!^F12::
If ((WinActive("ahk_exe excel.exe")) or (WinActive("ahk_exe vbsedit.exe")) or (WinActive("ahk_exe powershell_ise.exe")))
  CommentBlock:="#===========================================================================`n"
              . "# Author:`n"
              . "# Creation date:`n"
              . "# Description:`n"
              . "#===========================================================================`n"
Else
If (WinActive("ahk_exe ssms.exe"))
  CommentBlock:="Rem ======================================================================`n"
              . "Rem Author:`n"
              . "Rem Creation date:`n"
              . "Rem Description:`n"
              . "Rem ======================================================================`n"
Else
If (WinActive("ahk_exe notepad++.exe"))
  CommentBlock:="'*************************************************************************`n"
              . "' Author:`n"
              . "' Creation date:`n"
              . "' Description:`n"
              . "'***************************************************************************`n"
Else
{
  MsgBox,4144,Error,Active window is not a supported program
  Return
}
SendRaw %CommentBlock%
Return

Open in new window


As you can see, I used the EXE file names for the WinActive calls. I tested this with excel.exe, notepad++.exe, and an unsupported program...all three worked. I did not test it with vbsedit.exe, powershell_ise.exe, or ssms.exe (but if the EXE names that you posted are correct, they should work, too).

I made the hotkey Alt+Ctrl+F12. As always, change it to whatever you want. Regards, Joe
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Luis DiazIT consultant

Author

Commented:
Joe,
My apologies concerning feedback of previous questions.
I will provide feedback soon. I don't why I didn't received alerts e-mails related to your previous comments.
Joe WinogradDeveloper
Fellow 2017
Most Valuable Expert 2018

Commented:
> didn't received alerts e-mails related to your previous comments

No worries, Luis, has happened to me, too.
Luis DiazIT consultant

Author

Commented:
Joe,
I tested your proposal on excel, vbsedit, notepad++, powershell, sciTE and ssms and it works perfectly!
I adjusted it based on my personal need like this:
;====================================
;Send blocks comment
;====================================

:*:blc::
If ((WinActive("ahk_exe excel.exe")) or (WinActive("ahk_exe vbsedit.exe")))
  CommentBlock:="' *************************************************************************`n"
              . "' Author:`n"
              . "' Creation date:`n"
              . "' Description:`n"
              . "' ***************************************************************************`n"
Else
If (WinActive("ahk_exe notepad++.exe"))
  CommentBlock:="Rem ======================================================================`n"
              . "Rem Author:`n"
              . "Rem Creation date:`n"
              . "Rem Description:`n"
              . "Rem ======================================================================`n"
Else
If (WinActive("ahk_exe powershell_ise.exe"))
 CommentBlock:="# ===========================================================================`n"
              . "# Author:`n"
              . "# Creation date:`n"
              . "# Description:`n"
              . "# ===========================================================================`n"
Else
If (WinActive("ahk_exe sciTE.exe"))
 CommentBlock:="; ===========================================================================`n"
              . "; Author:`n"
              . "; Creation date:`n"
              . "; Description:`n"
              . "; ===========================================================================`n"
Else
If (WinActive("ahk_exe ssms.exe"))
 CommentBlock:="-- ===========================================================================`n"
              . "-- Author:`n"
              . "-- Creation date:`n"
              . "-- Description:`n"
              . "-- ===========================================================================`n"
Else
{
  MsgBox,4144,Error,Active window is not a supported program
  Return
}
SendRaw %CommentBlock%
Return

Open in new window


Thank you again for your help!!
Joe WinogradDeveloper
Fellow 2017
Most Valuable Expert 2018

Commented:
> I tested your proposal on excel, vbsedit, notepad++, powershell, sciTE and ssms and it works perfectly!

Great news!

> I adjusted it based on my personal need like this

Very nice! Using a hotstring instead of a hotkey for that one is a good idea, since no text is selected at the time.
Luis DiazIT consultant

Author

Commented:
Thank you Joe for your help on this. I will use it on a regular basis!
Joe WinogradDeveloper
Fellow 2017
Most Valuable Expert 2018

Commented:
You're welcome, Luis. I'm very glad to hear that this will be so helpful for you.
Joe WinogradDeveloper
Fellow 2017
Most Valuable Expert 2018

Commented:
Hi Luis,

Being a fellow AutoHotkey coder, I want to bring to your attention a new AutoHotkey feature that I discuss in my latest Experts Exchange article:

AutoHotkey Switch-Case

Also, note that the example that I use in the article is from the code in this thread...thanks for that! I hope you enjoy the article and learn something from it. Regards, Joe

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial