Add tags with a single keystroke

Here's one  and I collaborated on. We've been meaning to write about it in our blogs, but here it is in the meantime. Further explanation to come in the blog article, but in a nutshell it allows you to insert tags sequentially by pressing a single key while ignoring placeables. As you can see, this script uses the Left Windows key.

The script assumes that the default Ctrl+Enter shortcut is used to confirm segments. If a different shortcut is used, it needs to be replaced accordingly on the last line of the script.

;Add tags with LWin key

n:=1
return

Lwin::
if (n = 1)
Send, ^!{Right}{Esc}{Right}
else
{
Send, {Ctrl Down}{Alt Down}{Right %n%}
Sleep 200
Send, {Ctrl Up}{Alt Up}{Esc}{Right}
}
n++ ; increment n
return

~^Enter::n := 1 ;script is reset with Confirm and Move to Next Segment shortcut, tilde is needed to pass through the original action for Ctrl+Enter

  • Very smart this one guys... quite amazing what can be accomplished with AHK!
  • Hello Nora,

    thanks a lot for this neat little helper. Is the blog post out yet?
    I was trying to use this and adding in a ctrl-a so as to select all and enter enclosing tags, but the script didn't like that and was no longer working reliably, so a few details in how the script works and how to possibly mod it would be great.

    Matthias
  • Hi Matthias,

    I'm glad you've found it helpful! The blog post is not ready yet, but I may be able to help with what you need. A script to select all the content of the segment and enclose it in tags would be:

    Use the Windows key + A to activate

    ;Enclose segment in tags
    #a::
    Send ^a          ;selects all
    Sleep 100
    Send ^!{Right}   ;applies tags
    Sleep 100
    Send {Right}     ;moves the cursor to the right to deselect the text
    Return

    Hope that helps!

  • Hi Nora,

    that works famously. Thank you so much!

    Initially I was a little confused as to why this script looks rather different than the other one. I know AHK has several methods of pushing keystrokes into the UI, but maybe you could comment a little?!

    When I was tinkering with your original script the other day, I tried to mod it by adding in

    Send ^a
    Pause 20

    or something similar. But that wouldn't highlight the text. Maybe the break was too short. In any case the script would sometimes run ok, but then run crazy often enough. You could watch it highlight the tags one after another in a merry go round.



    Kind regards,
    Matthias
  • Hi Matthias,

    I gues you mean
    Sleep, 20
    right?

    20 ms would be too little to allow Windows to select text. Increase it to 100 or 200 at least. Check that the timer selected works for you, otherwise, increase it…

    … Jesús Prieto …
  • Hi Matthias,

    If you add these two lines:

    Send ^a
    Sleep 100 (or however long is needed, 20 ms may be too short indeed)

    as the first two lines of the original script, it should also do what you need, but I think this may be overkill as the original script uses a variable (n) to cycle consecutively through the tags present in the segment each time the right arrow key is pressed while holding down Ctrl+Alt, but to enclose an entire segment in tags, a simpler set of steps is needed, namely, 1. Select all, 2. Apply tags.
  • Dear @Nora Días,

    Your script is very helpful, but I wonder if there is a way to insert tags separately with a single keystroke, without having to select the text inside the tags. What I mean is that every time we press "LWin key" (or other key you choose) Studio inserts the next following partial tag (opening tag or closing tag) which are in source segment.

    For instance, if we have something like that:
    <tag1>Gracias</tag1> a la vida <tag2>que me</tag2> ha dado tanto.

    we could do this:
    press LWin key >> Studio inserts the first opening tag
    type Gracias
    press LWin key >> Studio inserts the first closing tag
    type a la vida
    press LWin key >> Studio inserts the second opening tag
    type que me 
    press LWin key >> Studio inserts the second closing tag
    type ha dado tanto.

    I think memoQ has something like that (with F9).

    If I do not made myself clear, please ask me.

    Un abrazo, 
    Silvia

  • Hi Silvia,

    Partial tags would require using Quickplace and that has two complications for a script like this: 1. Other non-tag placeables are included in the list and 2. The opening and closing partial tags do not appear sequentially in the Quickplace list.

    However, here's a variation of the script that may help a little. It places the cursor inside the tag pair, where you can then simply start typing. Would that help? Keep in mind that it will also move the cursor to the left of any placeholder tags inserted this way.

    #IfWinActive ahk_exe SDLTradosStudio.exe

    n:=1
    return

    Lwin::
    if (n = 1)
    Send, ^!{Right}{Esc}{Right}{Left}
    else
    {
    Send, {Ctrl Down}{Alt Down}{Right %n%}
    Sleep 200
    Send, {Ctrl Up}{Alt Up}{Esc}{Right}{Left}
    }
    n++ ; increment n
    return

    ~^Enter::n := 1 ;script is reset with Confirm and Move to Next Segment shortcut, tilde is needed to pass through the original action for Ctrl+Enter

  • Hi, @Nora Díaz,

    Sorry for not answering you earlier. I did some tests here with your new script and I think it can helps in some way. Thank you very much for your kind attention.

    Un abrazo,

    Sílvia