OmegaT tags one by one

Situation

When working with a tag-rich text in OmegaT where tags are really needed (for instance, with HTML files), you can either insert the source into translation input field (Ctrl+Shift+I or Ctrl+Shift+R) and then overwrite the original with your translation between the tags, or put them all into the input field without any original text (Ctrl+Shift+T) and write your translation between the appropriate tags. That’s if you wanna use the keyboard only. With the mouse you can select, copy and paste them one by one, screeching your teeth over each one of them and hating the waste of time.

Problem

What you want is a possibility to select one tag at a time, insert it, select the next one, and all that without the mouse.

Solution

On GNU/Linux you can use the following script:

#!/bin/bash
##########
# Tag Insert — Version 0.1, 20090517
# By Roberto Bechtlufft
# <<a class="linkification-ext" title="Linkification: http://www.bechtranslations.com.br" href="http://www.bechtranslations.com.br">http://www.bechtranslations.com.br</a>>
# Modified by Kos Ivantsov
# <<a class="linkification-ext" title="Linkification: https://libretraduko.wordpress.com" href="https://libretraduko.wordpress.com">https://libretraduko.wordpress.com</a>>
##########
# Requires:
# cat, sed, grep, tr, xte, xclip
# zenity (to locate OT installation dir),
# notify-send (to show selected tag)
#
# xte can be substituted with xmacro or xdotool
# xclip can be substituted with xsel or xclipboard
##########

##########
# create a file that contains OT installation directory
# it's used later on to find OT icon for notifications
# if there's no need for icons, this part can be deleted
if [ ! -f $HOME/.omegat/ompath ]; then
	dirname `zenity --file-selection --title="Navigate to OT directory" \
	--text="Select OmegaT.jar from the folder where OmegaT is installed"` \
	> $HOME/.omegat/ompath
fi
OM_PATH=`cat $HOME/.omegat/ompath`
##########

##########
# shows currently selected tag with libnotify popup
function notify {
	notify-send --icon="$OM_PATH/images/OmegaT.png" `cat $currenttag`
}
##########

##########
# checks if OT is running, used later on during invocation of the
# script either to execute, or to exit
function test_omegat () {
	export OTPID=`ps ax|grep OmegaT.jar|grep java|cut -d " " -f 2`
	if [ -z $OTPID ]; then
	echo "NORUN"
	else
	echo "RUN"
	fi
}
##########

##########
# set up some variables and files to store tags at different stages
#
source=$HOME/.omegat/script/source.txt
tagsdir=/tmp/ottags
tags=$tagsdir/tags
tagnumber=$tagsdir/tagnumber
currenttag=$tagsdir/currenttag
pidfile=$tagsdir/pidfile
##########

##########
# Create temp dir for temp files with tags
if [ ! -d $tagsdir ]; then
mkdir -p $tagsdir
fi
##########

##########
# If the script is run without parameters, it checks if another instance
# is already running, and if so, exits, otherwise proceeds
# and then loops in the background
if [ -z $1 ]; then
	if [ -e $pidfile ]; then
	rm $pidfile
	fi
echo $$ > $pidfile
export PID=`cat $pidfile`
test `pgrep taginsert|grep -v $PID|wc -l` -ge 2 && rm $pidfile && exit 0
#
# then it watches (every 2 seconds) if OT's exported source segment
# has changed
# if changed, gather all the tags
# (one on each line with preceding number) into $tags
# set $tagnumber to "1" and set $currenttag to string #1 from $tags
# (without preceding digit(s))
#
old=`stat $source | grep Modify`
while [ i != 0 ]; do
	sleep 2
	[ "$(test_omegat)" = "NORUN" ] && rm -r $tagsdir && exit 0
	new=`stat $source | grep Modify`
	if [ "$old" != "$new" ]; then
		rm -r $tagsdir/*
		old="$new"
		grep -o "<.[^>]*>" $source |grep -n "<" > $tags
		echo "1" > $tagnumber
		grep "^1:" $tags | cut -d ":" -f 2 > $currenttag
	fi
done
fi
##########

##########
# if invoked with parameters, then select prev./next tag
# selection goes in loop, ie next to the last is the first,
# and previous to the first is the last
# Upon changing selection popup notification is brought up,
# showing OT's icon and currently selected tag
# Before inserting current tag, clipboard is stored into a variable,
# and upon inserting the tag, it gets restored.
case $1 in
	previous)
	[ "$(test_omegat)" = "NORUN" ] && rm -r $tagsdir && exit 0
	export numberoftags=`cat $tags|wc -l`
	if [ `cat $tagnumber` -ge 2 ]; then
	expr `cat $tagnumber` - 1 > $tagnumber
	else
	echo $numberoftags > $tagnumber
	fi
	tagnumber=`cat $tagnumber`
	grep "^$tagnumber:" $tags | cut -d ":" -f 2 > $currenttag
	notify
	;;
	next)
	[ "$(test_omegat)" = "NORUN" ] && rm -r $tagsdir && exit 0
	export numberoftags=`cat $tags|wc -l`
	if [ `cat $tagnumber` -lt ${numberoftags} ]; then
	expr `cat $tagnumber` + 1 > $tagnumber
	else
	echo "1" > $tagnumber
	fi
	tagnumber=`cat $tagnumber`
	grep "^$tagnumber:" $tags | cut -d ":" -f 2 > $currenttag
	notify
	;;
	insert)
	[ "$(test_omegat)" = "NORUN" ] && rm -r $tagsdir && exit 0
	export PREVCLIP=`xclip -o -selection clipboard`
	cat $currenttag | tr -d '\n' |xclip -i -selection clipboard
	xte "keydown Control_L" "key v" "keyup Control_L"
	sleep 2
	echo "$PREVCLIP"| xclip -i -selection clipboard
	;;
	*)
	exit 0
	;;
esac

The script should be saved under any convenient filename (taginsert here) and made executable:
chmod +x taginsert.
Once OmegaT is running, ivnoke taginsert (or whatever name you’ve given it) without any parameters.
Then assign three key combinations (this is done by your Window Manager or by tools like xbindkeys):

  1. One for taginsert previous — it will select previous tag (in circle)
  2. One for taginsert next — it will select next tag (in circle)
  3. One for taginsert insert — it will insert the selected tag (and keep the selection)

Check the beginning comments in the script to see the requirements.


The idea was taken from Roberto Bechtlufft’s taginsert scripts. Any improvements, criticism, comments are welcome.


In case you’re a Windows user

Here’s an AutoIt script, shared by Kerry Swatridge and based on Samuel Murray’s original idea.

#Include
Global $prev_source
Global $j
HotKeySet("^.", "NextTag")
HotKeySet("^,", "PrevTag")
$j = 0
$prev_source = ""
while True
	sleep(1000)
WEnd

; ================================

Func NextTag ()
Local $fileopen, $source, $tags, $numtags
If WinActive ("OmegaT", "") Then
	$fileopen = FileOpen ("source.txt", 128)
	$source = FileRead ("source.txt")
	FileClose ("source.txt")
	$tags = StringRegExp ($source, '(<[/]{0,1}[a-z]{0,3}[0-9]{0,5}[/]{0,1}>)', 3)
	$numtags = UBound($tags)
	if $numtags > 0 then
		if $source <> $prev_source then
			$j = 0
		else
			If $j = $numtags - 1 Then
				$j = 0
			Else
				$j = $j + 1
			EndIf
		EndIf
		$prev_source = $source
		ClipPut ($tags[$j])

		Send ("^v")
	endif
EndIf
EndFunc

Func PrevTag ()
Local $fileopen, $source, $tags, $numtags

If WinActive ("OmegaT", "") Then
	$fileopen = FileOpen ("source.txt", 128)
	$source = FileRead ("source.txt")
	FileClose ("source.txt")
	$tags = StringRegExp ($source, '(<[/]{0,1}[a-z]{0,3}[0-9]{0,5}[/]{0,1}>)', 3)
	$numtags = UBound($tags)
	if $numtags > 0 Then
		if $source <> $prev_source then
			$j = 0
		else
			If $j = 0 Then
				$j = $numtags - 1
			Else
				$j = $j - 1
			EndIf
		EndIf
		$prev_source = $source
		ClipPut ($tags[$j])

		Send ("^v")
	EndIf
EndIf
EndFunc

In lines 4 and 5 there are two hotkeys assigned to “insert previous tag” and “insert next tag” actions. You can change them to your likings.


If you’re a happy OmegaT 2.7 and above user

There’s almost no need in these scripts anymore because OmegaT, beginning with version 2.7 (which isn’t released as of this update on April 7, 2013), got smart enough to recognize which tags are already present in the input area. There is a menu item Edit → Insert Next Missing Tag which is sufficient in most cases. To add a shortcut to this action, you need to put a file MainMenuShortcuts.properties into your OmegaT config directory (it’s $HOME/.omegat by default on GNU/Linux). The file should contain a line editTagNextMissedMenuItem with the assigned shortcut. So, if you want to insert next missing tag with Ctrl+Shift+Down, your line will look like this:
editTagNextMissedMenuItem=ctrl shift DOWN (ALL CAPS are mandatory).


But as of now,
Good Luck!

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 )

Facebook photo

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

Connecting to %s