Skip to main content
ameri91165842
Inspiring
August 25, 2018
Answered

Replace tilde in dictionary by headword

  • August 25, 2018
  • 1 reply
  • 1173 views

Hello everybody

I'm very new in scripting but this is what I want:

<ar><k>заглушать</k>

, заглушить (вн.) 1. (звук) deaden (smth.) , muffle (smth.) , smother (smth.) ; (более громким звуком) drown (smth.) ; шум ветра заглушил его слова the noise of the wind drowned his words; 2. (ослаблять какое-л. чувство, ощущение) deaden (smth.) ; ~ боль deaden pain; 3. (растения) choke (smth.) ; 4. (подавлять) stifle (smth.) ; ~ голос совести stifle the voice of conscience.</ar>

<ar><k>загляденье</k>

с. разг. lovely sight; feast for the eyes идиом. ; она просто ~! she`s simply lovely!</ar>

<ar><k>заглядывать</k>

, заглянуть 1. (смотреть) look; (незаметно) peep, peer; заглянуть кому-л. в лицо peer/look into smb.`s face; ~ в словарь look in a dictionary; 2. (бегло прочитывать) take* a look/glance (at), glance through; 3. ~ в душу человека seek* an insight into a person`s heart, try to penetrate a person`s innermost feelings; 4. разг. (заходить куда-л.) look in (at), drop in (at); ~ вперёд look ahead; ~ в будущее get* a glimpse of the future.</ar>

<ar><k>загораживать</k>

загоражив|ать - , загородить (вн.) 1. (обносить оградой) fence in (smth.) ; ~ двор забором enclose a yard, fence in a yard; 2. (преграждать) block (smth.) , obstruct (smth.) , bar (smth.) ; ~ кому-л. дорогу block smb.`s way; 3. (заслонять) shield (smth.) ; ~ кому-л. свет be* in smb.`s light; не ~айте мне свет would you mind keeping out of my light; ~аться, загородиться 4. fence one self off, shut* one self off; 5. (заслоняться) screen one self, shield one self.</ar>

I want replace tilde every time by its corresponding headword

some tildes have spaces from 2 sides this will take the full word between <ar><k> and </k>

and text from beginning to this sign " | " will replace tilde without space after : for example ~аться will become загораживаться

I tried something very simple and very weak but it is not working:

-----------------------------------------------------------------------------------------------------------------------------------

// Search the document for the string "first word in paragraph"

    app.findGrepPreferences.findWhat = "<ar><k>.*?</k>";

    var mySel = app.selection[1];

    app.copy;

//Search the document for the string " ~ " and replace it by "the first word in paragraph".

    app.findTextPreferences.findWhat = " ~ ";

    app.changeTextPreferences.changeTo = app.findGrepPreferences.findWhat = app.paste;

    app.activeDocument.changeText();

// Search the document for the string "any letters from beginning of the line until | symbol "

    app.findGrepPreferences.findWhat = "^.*?|";

    var mySel = app.selection[1];

    app.copy;

    app.findTextPreferences.findWhat = " ~";

    app.changeTextPreferences.changeTo = app.findGrepPreferences.findWhat = app.paste;

    app.activeDocument.changeText();

-----------------------------------------------------------------------------------------------------------------------------------

Can anyone help me how it will be the best script form can be used without mistakes in this script??

thanks a lot for all your answers

This topic has been closed for replies.
Correct answer ameri91165842

To make this change:

Before:

<ar><k>загородный доставка</k>

загородн|ый - out-of-town attr. , country attr. ;  загородный  дом house in the country;  ~ая прогулка trip to the country, trip out of town.</ar>

After:

<ar><k>загородный доставка</k>

загородн|ый - out-of-town attr. , country attr. ;  загородный  дом house in the country;  загородная прогулка trip to the country, trip out of town.</ar>

Use this script:

app.findGrepPreferences = null;

app.findGrepPreferences.findWhat = '^.+?(?=\\|)';

found = app.documents[0].findGrep();

app.findGrepPreferences.findWhat = ' ~';

for (i = found.length-1; i >= 0; i--) {

  app.changeGrepPreferences.changeTo = found.contents;

  found.paragraphs[0].changeGrep();

}

No need to change | to something else. | has a special meaning in Grep ('or'), so to look for the | character, escape it by adding \ before it. And because \ is a special character, it, too, must be escaped.

P.


I want thank you so much for your help

I did it yesterday the same by this one and it worked like magic

I will check yours when I will come back home as I'm at work now

Full apreciation for your first helpful script

This is the final one I did:

app.findTextPreferences.findWhat = "|";
app.changeTextPreferences.changeTo = "###";
app.activeDocument.changeText();

app.findGrepPreferences = app.changeGrepPreferences = null; 

app.findGrepPreferences.findWhat = "^.*?###"; 

app.changeGrepPreferences.changeTo = "<en><q>$0</q>";

app.activeDocument.changeGrep();

app.findGrepPreferences = app.changeGrepPreferences = null; 

app.findGrepPreferences.findWhat = '<ar><k>\\K.+?(?=</k>)' 

headwords = app.documents[0].findGrep(); 

 

app.findGrepPreferences.findWhat = ' ~ '; 

for (i = headwords.length-1; i >= 0; i--) { 

  app.changeGrepPreferences.changeTo = headwords.contents; 

  nextpar = headwords.paragraphs[0].insertionPoints[-1].paragraphs[0]; 

  nextpar.changeGrep(); 

}

app.findTextPreferences.findWhat = " ~";
app.changeTextPreferences.changeTo = "  ~";
app.activeDocument.changeText();

app.findGrepPreferences = app.changeGrepPreferences = null; 

app.findGrepPreferences.findWhat = '<en><q>\\K.+?(?=</q>)' 

subwords = app.documents[0].findGrep();  

 

app.findGrepPreferences.findWhat = ' ~'; 

for (i = subwords.length-1; i >= 0; i--) { 

  app.changeGrepPreferences.changeTo = subwords.contents; 

  samepar = subwords.insertionPoints[-1].paragraphs[0]; 

  samepar.changeGrep(); 

}

Replace ("###", "")
Replace ("</q>", "|")
Replace ("<en><q>", "")

//Replace("hobs707", "\x0dhobs007") just copy and paste and edit the contents as your wish 
function Replace(input, output) 
app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing; 
app.findTextPreferences.findWhat = input; 
app.changeTextPreferences.changeTo = output; 
app.activeDocument.changeText(); 
}
app.findTextPreferences.findWhat = " ~ ";
app.changeTextPreferences.changeTo = "  ~  ";
app.activeDocument.changeText();

1 reply

Peter Kahrel
Community Expert
Community Expert
August 26, 2018

You first find all the head words. Then you take each head word and you replace, in the next paragraph only, ~ with that headword. Along these lines:

app.findGrepPreferences = app.changeGrepPreferences = null;

app.findGrepPreferences.findWhat = '<ar><k>\\K.+?(?=</k>)'

headwords = app.documents[0].findGrep();

app.findGrepPreferences.findWhat = '~';

for (i = headwords.length-1; i >= 0; i--) {

  app.changeGrepPreferences.changeTo = headwords.contents;

  nextpar = headwords.paragraphs[0].insertionPoints[-1].paragraphs[0];

  nextpar.changeGrep();

}

You'll undoubtedly need to add things, but this is something to get you started.

Peter

ameri91165842
Inspiring
August 26, 2018

I really want thank you so much for the missing lines you provided me

inspite it copied only one word and repeat it in all the other paragraphs

I think the best way is to search grep by groups within same paragrah

in order to avoid select useless words which don't have tilde in the same paragraph

all parapraphs start by <ar> and ends by </ar>

some of them has tildes and some no

so I think if we need to focus search only on the paragraphs that contains both headword and tilde

in this case we will achieve with no mistake at all I think

grep: ^(.*?)(.*?)/r

change: $1 copy beside $2

this copy beside which is missing for me really

app.findGrepPreferences.findWhat = '^<ar>(<k>.*?</k>)|( ~ )</ar>'

app.changeGrepPreferences.changeTo = $1 copy beside $2

Again thanks a lot for your quick response and useful information really

I wish you can guide me through searching two groups of grep only not any text between <ar><k>\\K.+?(?=</k>)

and copy found $1 beside found $2 without touching any other text in between

ameri91165842
ameri91165842AuthorCorrect answer
Inspiring
August 28, 2018

To make this change:

Before:

<ar><k>загородный доставка</k>

загородн|ый - out-of-town attr. , country attr. ;  загородный  дом house in the country;  ~ая прогулка trip to the country, trip out of town.</ar>

After:

<ar><k>загородный доставка</k>

загородн|ый - out-of-town attr. , country attr. ;  загородный  дом house in the country;  загородная прогулка trip to the country, trip out of town.</ar>

Use this script:

app.findGrepPreferences = null;

app.findGrepPreferences.findWhat = '^.+?(?=\\|)';

found = app.documents[0].findGrep();

app.findGrepPreferences.findWhat = ' ~';

for (i = found.length-1; i >= 0; i--) {

  app.changeGrepPreferences.changeTo = found.contents;

  found.paragraphs[0].changeGrep();

}

No need to change | to something else. | has a special meaning in Grep ('or'), so to look for the | character, escape it by adding \ before it. And because \ is a special character, it, too, must be escaped.

P.


I want thank you so much for your help

I did it yesterday the same by this one and it worked like magic

I will check yours when I will come back home as I'm at work now

Full apreciation for your first helpful script

This is the final one I did:

app.findTextPreferences.findWhat = "|";
app.changeTextPreferences.changeTo = "###";
app.activeDocument.changeText();

app.findGrepPreferences = app.changeGrepPreferences = null; 

app.findGrepPreferences.findWhat = "^.*?###"; 

app.changeGrepPreferences.changeTo = "<en><q>$0</q>";

app.activeDocument.changeGrep();

app.findGrepPreferences = app.changeGrepPreferences = null; 

app.findGrepPreferences.findWhat = '<ar><k>\\K.+?(?=</k>)' 

headwords = app.documents[0].findGrep(); 

 

app.findGrepPreferences.findWhat = ' ~ '; 

for (i = headwords.length-1; i >= 0; i--) { 

  app.changeGrepPreferences.changeTo = headwords.contents; 

  nextpar = headwords.paragraphs[0].insertionPoints[-1].paragraphs[0]; 

  nextpar.changeGrep(); 

}

app.findTextPreferences.findWhat = " ~";
app.changeTextPreferences.changeTo = "  ~";
app.activeDocument.changeText();

app.findGrepPreferences = app.changeGrepPreferences = null; 

app.findGrepPreferences.findWhat = '<en><q>\\K.+?(?=</q>)' 

subwords = app.documents[0].findGrep();  

 

app.findGrepPreferences.findWhat = ' ~'; 

for (i = subwords.length-1; i >= 0; i--) { 

  app.changeGrepPreferences.changeTo = subwords.contents; 

  samepar = subwords.insertionPoints[-1].paragraphs[0]; 

  samepar.changeGrep(); 

}

Replace ("###", "")
Replace ("</q>", "|")
Replace ("<en><q>", "")

//Replace("hobs707", "\x0dhobs007") just copy and paste and edit the contents as your wish 
function Replace(input, output) 
app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing; 
app.findTextPreferences.findWhat = input; 
app.changeTextPreferences.changeTo = output; 
app.activeDocument.changeText(); 
}
app.findTextPreferences.findWhat = " ~ ";
app.changeTextPreferences.changeTo = "  ~  ";
app.activeDocument.changeText();