AutoHotkey Tip of the Week: Channeling Text to a Tagged Window

When Collecting Information from Various Sources, Send the Text Directly to an Unseen Text Editing Field

Last time in “Tricks for Tracking and Activating Target Process Windows“, I demonstrated a technique for tagging and tracking a window using its Unique ID. After tagging any window with one Hotkey combination, you can instantly recall it with another. While a pretty cool trick, the question of when would you ever use it arises. This time I offer a practical tool for gathering information from various digital sources (Web pages, documents, e-books, etc.) into one text editor window—without jumping back-and-forth while doing cut-and-paste operations.

Collecting Resource Information

I originally wrote the CopyRef.ahk script for researchers and students looking for an easier way to garner notes from many sources into one location. (CopyRef.ahk represents a more robust version of the original SaveText.ahk script used to copy any selected text to ScratchPad by Desi Quintan—discussed in the e-book Digging Deeper Into AutoHotkey.) The CopyRef app merely opens a NotePad window and directs designate text from another source to that NotePad window without activating it. The fast Control, EditPaste command provides a robust conduit to insert the text into the unseen window. However, the original CopyRef.ahk script only utilizes untitled NotePad windows.

AutoHotkey AutoHotkey Library Deal!

Note: I discuss the specialized Control, EditPaste command in Chapter 13.1.7 “Control, EditPaste for Speed” of my book, Jack’s Motley Assortment of AutoHotkey Tips.

I’ve increased the flexibility of the script (now called CopyRegTagWin.ahk) to include other programs supported by the Control, EditPaste command by adding the windows tagging feature from last time—designating the target data receiving window with its Unique ID and Edit control. Although still limited to Edit controls, this approach allows me to use a number of different apps and any window names as the receiving bin.

Tip: For the Control, EditPaste command to work, the collection window must support Edit type fields. Most word processors and Web pages I tested do not. That limits the opportunities for this type of collection, but NotePad, WordPad, and AutoHotkey GUI Edit fields work well. In the worse case, you can use one of these options to do your data collection, then copy-and-paste the entire set into a new document or Web blog. To make it functional in more wordprocessing apps, I could have used the ControlSend command for similar results. However, that command operates more slowly and—depending upon the application—with variable reliability.

AutoHotkey Code Testing Tip: While I originally designed the script to collect reference data from multiple sources without the tedious window hopping, CopyRefTagWin.ahk works equally as well for testing AutoHotkey code using Philip Taylor’s (GeekDude) AutoHotkey CodeQuickTester. (I discuss this tool in “Save Time with CodeQuickTester for Testing and Modifying Scripts.”) Tag a CodeQuickTester window, then send code snippets from the AutoHotkey Forum directly to the tagged window for instant testing. You may want to switch to the ControlSetText command which operates in a manner similar to Control, EditPaste except it replaces the current text rather than adding to it:

ControlSetText, %Control%, %Clipboard%, ahk_id %WinTag%

While the ControlSetText command works in the same manner as Control, EditPaste for EDIT type fields (although replacing the entire text), it includes the added capability for changing the displayed text names of other controls (i.e. buttons, checkboxes, etc). For example, you can change the text visible in Button1 from the original to “Save“:

ControlSetText, Button1, Save, ahk_id %WinTag%

The CopyRefTagWin.ahk Script

The CopyRefTagWin.ahk script offers three distinct functions:

  1. Tag the window to receive text (Ctrl+Win+Alt+T). Must activate the window first.
  2. Copy text from any other app directly into the tagged window by selecting the desired text and executing the Ctrl+Alt+C Hotkey combination.
  3. Return to the tagged window (Ctrl+Win+Alt+R) to process text or, as in the case of a tagged “CodeQuickTest” window, run the copied code.

Tagging a Window for Data Collection

The Hotkey subroutine Ctrl+Win+Alt+T executes the routine for tagging a specific window:

^!#t::
  Click %A_CaretX% %A_CaretY%
  MouseGetPos, , , WinTag, Control
  WinGetTitle, WinTitle, ahk_id %WinTag%
  MsgBox The "%WinTitle%" window is tagged
        !`rUnique ID: %WinTag%`rControl
        : %Control%`rCtrl+Win+Alt+R to activate.
Return

Notable window tagging routine features:

  1. I use the MouseGetPos command because it can simultaneously capture both the Unique ID for a window and the control name (ClassNN) located under the mouse cursor. Other AutoHotkey commands require a separate command line for each.
  2. I added the Click, %A_CaretX%, %A_CaretY% command line to ensure the mouse cursor sits at the same window location as the text cursor.
  3. The WinGetTitle command captures the text title (WinTitle) of the window. You can also use the equivalent commands:
    WinGetActiveTitle, WinTitle

    or

    WinGetTitle, WinTitle, A
  4. CopyRefTagQuickCodeTesterThe MsgBox command line above uses line continuation techniques to wrap the code for display purposes. Since both the exclamation point (!) and colon (:) act as AutoHotkey operators—although not in this situation—they create a wrapped continuous line. (The image at the right shows the “CodeQuickTester” target window example used in this blog.)

After tagging a window, the user can visit various other documents or Web pages, highlight text, then copy each selection directly into the target.

Copy Text to the Target Window

In the example below, we tag a “CodeQuickTester” window, then select the code from an AutoHotkey Forum page—sending it to the tester (Ctrl+Alt+C).

CopyRefTag

The following Ctrl+Alt+C Hotkey (^!c:: ) subroutine inserts copied text into the tagged target window:

^!c::
If !WinTag
{
  MsgBox, Tag a target window in NotePad
               ,`rWordPad or a GUI edit field:`r`r 1
               . Activate    window`n 2
               . Press Ctrl+Win+Alt+T
  Return
}
; The Standard Clipboard Routine
OldClipboard := ClipboardAll
Clipboard := "" ;clears the Clipboard
SendInput, ^c
ClipWait 0 ; pause for Clipboard data
If ErrorLevel
{
  MsgBox, No text selected!
  Return
}
Control, EditPaste, %Clipboard% `n`n ,%Control%, ahk_id %WinTag%
ToolTip , Text inserted into %WinTitle%`rControl: %control%!
Sleep, 3000
ToolTip
Clipboard := OldClipboard
Return

This remote text pasting subroutine includes the following features:

  1. CopyRegNoWindowTaggedYou must first activate and tag a target window before attempting to send any text to it. Otherwise, the WinTag variable doesn’t exist (If !WinTag), thus trapping the subroutine, displaying a notification MsgBox (shown at right), then exiting (Return) the Hotkey subroutine.
  2. As discussed in my books (starting with Jack’s Beginner’s Guide to AutoHotkey), this subroutine uses the Standard Clipboard Routine to capture the selected text.
  3. The key operational portion of the Standard Clipboard Routine (in red) uses the Control, EditPaste command to direct the copied text to the target window (WinTag) and Edit control (Control).
    Note: In the posted CopyRegTagWin.ahk script, you will find the ControlSetText command (commented out) available for variations of the script which replaces the original text rather than adding to it:

    ControlSetText, %Control%, %Clipboard%, ahk_id %WinTag%

    You might want to set up conditional use of this command whenever accessing a tagged “CodeQuickTester” window to ensure a clean code set.

  4. CopyRefToolTipThe ToolTip commands merely offer visual confirmation (image at right) of the text insertion into the target window. The second ToolTip command turns off the message after a three-second delay (Sleep, 3000).

Since this subroutine sends the text in the background, you won’t see the results until you return to the target window (Ctrl+Win+Alt+R).

Activating the Target Window

Once tagged, you can activate the target window at any time using the Ctrl+Win+Alt+R Hotkey combination:

^!#r::
  WinActivate, ahk_id %WinTag%
Return

If you modify this script to test AutoHotkey scripts using the ControlSetText replacement command, then you may want to include this WinActivate command for automatic display of the “CodeQuickTester” window.

Find the complete CopyRefTagWin.ahk script just below, at the Free ComputorEdge AutoHotkey Scripts page, or download the ZIP file here: CopyRefTagWin

Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)

jack

This post was proofread by Grammarly
(Any other mistakes are all mine.)

(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)

Find my AutoHotkey books at ComputorEdge E-Books!

CopyRegWinTag.ahk:

^!c::
  If !WinTag
  {
    MsgBox, Tag a target window in NotePad
               ,`rWordPad or a GUI edit field:`r`r 1
               . Activate    window`n 2
               . Press Ctrl+Win+Alt+T
    Return
  }
  OldClipboard := ClipboardAll
  Clipboard := "" ;clears the Clipboard
  SendInput, ^c
  ClipWait 0 ; pause for Clipboard data
  If ErrorLevel
  {
    MsgBox, No text selected!
    Return
  }
  Control, EditPaste, %Clipboard% `n`n ,%control%, ahk_id %WinTag%
; To replace text (rather than add), 
; use the following ControlSetText command:
; ControlSetText , %Control%, %Clipboard%, ahk_id %WinTag%
  ToolTip , Text inserted into %WinTitle%`rControl: %control%!
  Sleep, 3000
  ToolTip
  Clipboard := OldClipboard
Return

^!#t::
  Click, %A_CaretX%, %A_CaretY%
  MouseGetPos, , , WinTag, Control
  WinGetTitle, WinTitle, ahk_id %WinTag%
  MsgBox The "%WinTitle%" window is tagged 
      !`rUnique ID: %WinTag%`rControl 
      : %Control%`rCtrl+Win+Alt+R to activate.
Return

^!#r::
  WinActivate, ahk_id %WinTag%
Return

 

One thought on “AutoHotkey Tip of the Week: Channeling Text to a Tagged Window

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s