Selecting a particular text inside a line from a specified character until another specified character

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
monk Sarana
Posts: 7
Joined: 22 Jun 2017, 17:42

Selecting a particular text inside a line from a specified character until another specified character

22 Jun 2017, 21:09

Hello,
if this is a question answered elsewhere, then I apologize in advance. I did search, and didn't find :( .

I work on a large dictionary (Burmese-English-Czech) with a huge database (over hundred thousand entries). I would like to use AHK (I use Pullover's Macro Creator to decrease potential mistakes) to do batch edits for each term in the dictionary. The edits I would like to do are based on detecting certain characters (e.g. brackets - "(", ")" , etc.) and moving the contents of the brackets into a different line. I use a special dictionary-making software, which is based on database creation (so it makes database of each dictionary term). It has a special edit box for each aspect of the terms, i.e. different edit box for the Burmese word, different box for English translation, different box for grammatical features, etc.



See the picture of the program here:
TLex screenshot.png
Now in the picture, in the center, you can see inside the "English-Term:" line - "suitcase (with clothes, “clothes box”)"
Now what I need is to find a command or a set of commands, which, upon me inserting cursor in the beginning of the "English-Term:" line, would:
1. find the opening bracket inside the "English-Term:" line ("(")
2. start selecting text right after the opening bracket (i.e. in this case from the character "w", but this is different in different terms)
3. finish selecting the text right before the first quote (""") (i.e. including the space which is before the starting quote) (the selected text here will be: "with clothes, " (without quotes))
4. "Cut" the text into the Clipboard
5. Move by the keyboard button "Tab" downward (which is the way to move to the lines below, in this program)
6. paste the text into the line of "English Etymology"
7. Press the keyboard button "End" (to get to the end of the line, because upon pasting all of the line mihgt be selected)
8. Press the keyboard button "Delete" two times, to delete the final "space" and "comma" at the end of the Cut-Pasted line
9. move upward (by Alt-Tabs) to the line of Czech-Term - the Czech line here is: "kufr (s oblečením, “krabice¹ oblečeníº”)"
10. There again select the text after the opening bracket and before the first quote - here the cut text will be: "s oblečením, " (without quotes)
11. "Cut" that text into the Clipboard
12. And by the keyboard button "Tab" again move downward to a different line with Czech Etymology
13. Paste it there and remove the "space" and "comma" by the "Delete" keyboard button from the end of the line
(14. Return back to the "Czech-Term" line by repeated pressing the keyboard buttons "Alt-Tab")

Now at this moment I have created two scripts of which one does the steps 4-9 and another for the steps 11-14.
Until now I was doing the steps 1-3 and 8 manually, myself. I wasn't able to do the steps 1-3 and 8 because I don't know how to explain in the script that I want a particular text inside a line to be selected, and from where to where.

Please see below my scripts and compassionately help me create the steps 1-3 and 8. If you can, it will save me (I believe) hundreds of hours. I will of course use your suggestion for other similar cases of editing the terms. There are plenty like that.

Please see my script for the steps 4-9 below, Copy-Pasted from Pulover's Macro Creator (v5.0.1) -
WinActivate, tlTerm - [C:\Users\Javana\Documents\TLex\MyanEnCzed Second Edition3.tterm] (*UNSAVED CHANGES) [New Document Object Model] ahk_class wxWindowNR
Sleep, 5
Send, {LControl Down}
Sleep, 5
Send, {x}
Sleep, 5
Send, {LControl Up}
Sleep, 5
Send, {Tab 22}
Sleep, 5
Send, {LControl Down}
Sleep, 5
Send, {v}
Sleep, 5
Send, {LControl Up}
Sleep, 5
Send, {Backspace 2}
Sleep, 5
Send, {LShift Down}
Sleep, 5
Send, {Tab 21}
Sleep, 5
Send, {LShift Up}
Sleep, 5
Send, {End}
Sleep, 5

See below my script for the steps 11-14 -
WinActivate, tlTerm - [C:\Users\Javana\Documents\TLex\MyanEnCzed Second Edition3.tterm] (*UNSAVED CHANGES) [New Document Object Model] ahk_class wxWindowNR
Sleep, 5
Send, {LControl Down}
Sleep, 5
Send, {x}
Sleep, 5
Send, {LControl Up}
Sleep, 5
Send, {Tab 24}
Sleep, 5
Send, {LControl Down}
Sleep, 5
Send, {v}
Sleep, 5
Send, {LControl Up}
Sleep, 5
Send, {Backspace}
Sleep, 5
Send, {Backspace}
Sleep, 5
Send, {LShift Down}
Sleep, 5
Send, {Tab 24}
Sleep, 5
Send, {LShift Up}
Sleep, 5
Send, {End}
Sleep, 5

Thank you :-)
monk Sarana
Posts: 7
Joined: 22 Jun 2017, 17:42

Re: Selecting a particular text inside a line from a specified character until another specified character

23 Jun 2017, 04:24

Here is the file indeed:
TLex screenshot.png
TLex screenshot.png (168.25 KiB) Viewed 1902 times
Fantankerous
Posts: 5
Joined: 16 May 2017, 15:30

Re: Selecting a particular text inside a line from a specified character until another specified character

23 Jun 2017, 10:15

; Give this a shot for selecting from paren to quote. May have to adjust SLEEP for the performance of your computer.
; It assumes your software adheres to the standard of shift-left/right selecting a character.


loop ; move right to (
{
send +{right}
send ^c
send {left}
send {right}
sleep 40
} until (clipboard = "(")
loop ; move left to (
{
send +{left}
send ^c
send {right}
send {left}
sleep 40
} until (clipboard = "(")
sleep 100
send {right}
quoteChar := ("""") ; "in an expression 2 quotes resolve to a single literal quote" e.g. Var := "I am called ""Bob"" by friends."
Loop ; move right to "
{
send +{right}
send ^c
endCharacter := SubStr(Clipboard,StrLen(Clipboard),1)
sleep 40
} until (EndCharacter = quoteChar)
send +{left}
send +{left}
send ^x
Fantankerous
Posts: 5
Joined: 16 May 2017, 15:30

Re: Selecting a particular text inside a line from a specified character until another specified character

23 Jun 2017, 11:28

; you could also copy the whole field into the clipboard, and process the text there instead of with a series of key-selects
send ^a ; select all
send ^c ; copy
x := substr(clipboard,instr(Clipboard,"(")+1,instr(Clipboard,"""")-instr(Clipboard,"(")-1)
stringreplace clipboard,clipboard,%x%,,
send ^v ; paste, now the field has the desired text cut from between ( and "
clipboard := x ; clipboard now contains the substring in question
monk Sarana
Posts: 7
Joined: 22 Jun 2017, 17:42

Re: Selecting a particular text inside a line from a specified character until another specified character

23 Jun 2017, 18:22

Hello,
I don't know what wrong am I doing. See please below three scripts and tell me if there's any remedy for any of them.

I am testing the scripts in an opened Notepad program, where is just one line:
barbecue (asdf, “grilled¹ meatº”)
Script no.1 (gets stuck at the second loop; my text-cursor selects and deselects a character after character in the line, and then starts continuously selecting from a bracket until the end end of the line and stays in that selection. The Pulover's Macro Creator where I create the script shows me that I am stuck on the second loop, and the second loop repeats again and again. No idea why.)

Code: Select all

Loop  ; move right to (
{
    Send, {LShift Down}{Right}{LShift Up}
    ControlSend, Edit1, ^c, A
    Send, {Left}{Right}
    Sleep, 40
}
Until, (clipboard = "(")
Loop  ; move left to (
{
    Send, {LShift Down}{Left}{LShift Up}
    ControlSend, Edit1, ^c, A
    Send, {Right}{Left}
    Sleep, 40
}
Until, (clipboard = "(")
Sleep, 100
Send, {Right}
quoteChar := (""") ; "in an expression 2 quotes resolve to a single literal quote" e.g. Var := "I am called ""Bob"" by friends."
Loop  ; move right to "
{
    Send, {LShift Down}{Right}{LShift Up}
    ControlSend, Edit1, ^c, A
    endCharacter := SubStr(Clipboard,StrLen(Clipboard),1)
    Sleep, 40
}
Until, (EndCharacter = quoteChar)
Send, {LShift Down}{Left}{LShift Up}{LShift Down}{Left}{LShift Up}
ControlSend, Edit1, ^x, A

Script no.2 (selects all text in the line, copies and then pastes it all back, I suppose I have not correctly implemented in the script your line for the "x".)

Code: Select all

ControlSend, Edit1, ^a, A  ; select all
ControlSend, Edit1, ^c, A  ; copy
x := 
(LTrim
substr(clipboard,instr(Clipboard,"(")+1,instr(Clipboard,"""")-instr(Clipboard,"(")-1)
stringreplace clipboard,clipboard,%x%,,
)
ControlSend, Edit1, ^v, A  ; paste, now the field has the desired text cut from between ( and " clipboard := x ; clipboard now contains the substring in question
Script no.3 - I suppose this is perhaps ideal, because I can then easily set the RegEx for any three parts in the line. It has been created by me, so of course it won't work...
Now suppose that I have one line of text in Clipboard, e.g. this one:
barbecue (“grilled¹ meatº”)
I need to make it into three portions, clearly deliminated by RegEx:
$1 /[a-zA-Z0-9]*(/ => barbecue (
$2 /""[a-zA-Z0-9¹º²³]*""/ => "grilled¹ meatº"
$3 /).*/ => )

Then I need to send (by Ctrl-V) $1 and $3 into Notepad, so I will see in Notepad:
barbecue ()
And keep $2 in the Clipboard for Pasting (by Ctr-V) somewhere else.

Code: Select all

WinActivate, tlTerm - [C:\Users\Javana\Documents\TLex\MyanEnCzed Second Edition3.tterm] (*UNSAVED CHANGES) [New Document Object Model] ahk_class wxWindowNR
Sleep, 5
Send, {Control Down}{a}{Control Up}{Control Down}{x}{Control Up}
Send, RegExMatch(clipboard`, "(?<=()(.*)(?=))"`, email)
Clipboard := %Clipboard%
NewText1 := "RegExReplace(text, /[a-zA-Z0-9]*(/, $1)"
NewText2 := "RegExReplace(text, /""[a-zA-Z0-9¹º²³]*""/, $2)"
NewText3 := "RegExReplace(text, /).*/, $3)"
ControlSetText, Edit1, $1$3, ahk_class #32770
Any help will be very much appreciated ...
thank you :-)
monk Sarana
Posts: 7
Joined: 22 Jun 2017, 17:42

Re: Selecting a particular text inside a line from a specified character until another specified character

23 Jun 2017, 18:40

I have made a little improvement in the third script. Now what it does, it just selects and Cuts all of the line. Nothing is outputted in the Notepad program.

WinActivate, tlTerm - [C:\Users\Javana\Documents\TLex\MyanEnCzed Second Edition3.tterm] (*UNSAVED CHANGES) [New Document Object Model] ahk_class wxWindowNR
Sleep, 5
Send, {Control Down}{a}{Control Up}{Control Down}{x}{Control Up}
Clipboard := %Clipboard%
ControlSetText, Edit1, (clipboard`, ".*"`, NewText), A
NewText := "RegExReplace(text, /[a-zA-Z0-9]*(/, $1)"
NewText := "RegExReplace(text, /""[a-zA-Z0-9¹º²³]*""/, $2)"
NewText := "RegExReplace(text, /).*/, $3)"
ControlSetText, Edit1, $1$3, ahk_class #32770

Any help will be very much appreciated ...
Thank you :-)
Fantankerous
Posts: 5
Joined: 16 May 2017, 15:30

Re: Selecting a particular text inside a line from a specified character until another specified character

23 Jun 2017, 22:34

In your line of text: barbecue (“grilled¹ meatº”), you want to stop at the curly quote (“) which is a different character than the straight quote (").
Replace the four quotes in the scripts """" with your quote inside straight quotes "“" and the loop should stop when it gets to that quote character.
No guidance from me on regex. Every time I've tried to use it I've been stumped by the syntax or found errors in the documentation and given up.
monk Sarana
Posts: 7
Joined: 22 Jun 2017, 17:42

Re: Selecting a particular text inside a line from a specified character until another specified character

28 Jun 2017, 04:55

Dear Fantankerous,
I have followed your advice, and now it all works great.

Just what I need help with, is that I get one quote left. What should I do?
First I have this line:
barbecue ("grilled¹ meatº")

then the line becomes:
barbecue (")

and I have in clipboard this:
"grilled¹ meatº

is there any way how to get the quote from in-between brackets to the end of the clipboard text?

Thank you anyway :-)
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Selecting a particular text inside a line from a specified character until another specified character

28 Jun 2017, 05:49

I would like to use AHK [...] to do batch edits for each term in the dictionary. The edits I would like to do are based on detecting certain characters (e.g. brackets - "(", ")" , etc.) and moving the contents of the brackets into a different line. I use a special dictionary-making software, which is based on database creation ...
Correct me if I'm wrong - you're working with TLex, right?

WinActivate, tlTerm - [C:\Users\Javana\Documents\TLex\MyanEnCzed Second Edition3.tterm] (*UNSAVED CHANGES) [New Document Object Model] ahk_class wxWindowNR

Checking its FAQ [here] I'd expect that there's an option to im-/export that data (eg XML)?!
And here we go :arrow: UserGuide (P93-102).

That way you could re-process its (structured) data outside of the application* and once finished import it back to its DB. That way you wouldn't be forced to do any less reliable frontend task. And it should be much faster!
Based on the amount of data you've to process, it looks like it's worth the effort.

Probably some usefull info regarding how to (ab)use TLex with a script, check out [this]

Good luck. 8-)

* [XML Class by maestrith]
monk Sarana
Posts: 7
Joined: 22 Jun 2017, 17:42

Re: Selecting a particular text inside a line from a specified character until another specified character

28 Jun 2017, 07:02

Dear BoBo,
yes, it is TLex. But I feel more comfortable if I see exactly what is happening, one by one. There is a large amount of things like this (not same), and using macro/loops has proved to me to be the best way solving the problems. You know, it worked, so I try it more :-) .

I know pretty much nothing about programming, so taking Lua programming if I have no idea what "function" is etc., and somehow cannot understand it even when reading about it (several times), then I suspect it's not the best solution. Unfortunately, when I contacted the TLex people for help by e-mail, I got no answer at all. So I can't expect they will make me a script.

So far so good, I just have there the quote extra, and if I know how to remove it, perhaps I will understand Fantankerous script better and make customized scripts for other cases like this one.

Thank you :-)
mS
monk Sarana
Posts: 7
Joined: 22 Jun 2017, 17:42

Re: Selecting a particular text inside a line from a specified character until another specified character

28 Jun 2017, 07:26

Oh gosh! I got the answer. After trying different things like you know ca. 3 hours...

At the end of Fantankerous' script, he writes :
send +{left}
send +{left}

These actually should be only one:
send +{left}

This way I get it with both quotes in the clipboard.

Also, Fantankerous suggests that at the third loop I make the quote character: " , he writes """" . But I have found out that with this it doesn't work. The scripts works when instead of """" I actually make ")". this is the end character for the loop.

Now it works seamlessly, Perfect and neat. ... and I also understand the mechanics of the script.

So much thanks to Fantankerous!!

Here's the final script in case if anybody found it useful:
Loop ; move right to (

Code: Select all

{
    Send, {LShift Down}{Right}{LShift Up}{LControl Down}{c}{LControl Up}{Left}{Right}
    Sleep, 40
}
Until, (clipboard = "(")
Loop  ; move left to (
{
    Send, {LShift Down}{Left}{LShift Up}{LControl Down}{c}{LControl Up}{Right}{Left}
    Sleep, 40
}
Until, (clipboard = "(")
Sleep, 100
Send, {Right}
quoteChar := (")") ; "in an expression 2 quotes resolve to a single literal quote" e.g. Var := "I am called ""Bob"" by friends."
Loop  ; move right to "
{
    Send, {LShift Down}{Right}{LShift Up}{LControl Down}{c}{LControl Up}
    endCharacter := SubStr(Clipboard,StrLen(Clipboard),1)
    Sleep, 40
}
Until, (EndCharacter = quoteChar)
Send, {LShift Down}{Left}{LShift Up}
/*{LShift Down}{Left}{LShift Up}
*/{LControl Down}{x}{LControl Up}
Thank you :-)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ThePeter and 217 guests