Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How include external file in a script?

Engaged ,
Aug 17, 2018 Aug 17, 2018

Hi,

Please, do you know how can I include an external file in a multiples scripts?

I would like to place all my words translations from English to Spanish in one file, and each time I need some word simple get the word from the main file called translation.jsx

EXAMPLE STRUCTURE:

translation.jsx

MyScript1.jsx

MyScript2.jsx

MyScript3.jsx

EXAMPLE FILE translation.jsx

$english1="hello"; $spanish1="hola"; 

$english2="bye"; $spanish2="adios"; 

$english3="car"; $spanish3="carro"; 

etc...... 

EXAMPLE FILE MyScript1.jsx, MyScript2.jsx, MyScript3.jsx

(function () {   

    var doc = app.activeDocument,   

        indexIdx = doc.indexes.length - 1;  

 

 

    function removeNewEntriesFromIndex(index) {   

        var idx = index.topics.length - 1;     

        for (; idx >= 0; idx -= 1) {   

            $.writeln(idx); 

            index.topics[idx].name = index.topics[idx].name.replace(/ *^$english1$/, '$spanish1'); 

            index.topics[idx].name = index.topics[idx].name.replace(/ *^$english2$/, '$spanish2'); 

        }   

    }   

       

    for (; indexIdx >= 0; indexIdx -= 1) {   

        removeNewEntriesFromIndex(doc.indexes[indexIdx]);   

    }   

}()); 

Thanks so much in advance!

TOPICS
Scripting
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 17, 2018 Aug 17, 2018

> But can we use a trick like?

> var en1 = $english1;

No: you'll see that the result is '$english1 is undefined'.

> ....replace(/ *^en1$/, 'es1');

doesn't work because you can't use a variable like that inside a regular expression. Use variables like this:

var english1 = "hello";

var spanish1 = "hola";

index.topics[idx].name = index.topics[idx].name.replace (RegExp ('^' + english1 + '$'), spanish1);

But that's expensive because the regexes are compiled at every iteration. Better to compile everything on

...
Translate
Community Expert ,
Aug 17, 2018 Aug 17, 2018

Use the #include compiler directive (JavaScript isn't compiled, but that's what it's called in other languages). Like this:

#include MyScript1.jsx;

#include MyScript2.jsx;

#include MyScript3.jsx;

P.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 17, 2018 Aug 17, 2018

Hi Peter!

I two things , but is not working

translate.jsx

#include MyScript1.jsx;

#include MyScript2.jsx;

#include MyScript3.jsx;

$english1="hello"; $spanish1="hola";  

$english2="bye"; $spanish2="adios";  

$english3="car"; $spanish3="carro";

and also:

MyScript1.jsx;

#include translate.jsx

(function () {     

    var doc = app.activeDocument,     

        indexIdx = doc.indexes.length - 1;    

   

   

    function removeNewEntriesFromIndex(index) {     

        var idx = index.topics.length - 1;       

        for (; idx >= 0; idx -= 1) {     

            $.writeln(idx);   

            index.topics[idx].name = index.topics[idx].name.replace(/ *^$english1$/, '$spanish1');   

            index.topics[idx].name = index.topics[idx].name.replace(/ *^$english2$/, '$spanish2');   

        }     

    }     

         

    for (; indexIdx >= 0; indexIdx -= 1) {     

        removeNewEntriesFromIndex(doc.indexes[indexIdx]);     

    }     

}());

thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 17, 2018 Aug 17, 2018

sorry I didn't know what is #include compiler directive

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 17, 2018 Aug 17, 2018

I think the includes have to go inside the anonymous function:

(function () {

  #include . . .

  // Rest of the code

}());

P.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 17, 2018 Aug 17, 2018

good, looks work if I do a:

$.writeln($english1);

I can get the text "hello" in my Javascript console. But unfortunate is not working with the code here:

.....name.replace(/ *^$english1$/, '$spanish1');

Also I try:

(/ *\$english1$/,

(/ *$english1$/,

(/ *'$english1'$/,

....

Please, do you know the right way to do?

Thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 17, 2018 Aug 17, 2018

> But unfortunate is not working with the code here:

> .....name.replace(/ *^$english1$/, '$spanish1');

That's because $ has a special meaning in regular expressions (end of input). Try using a different character, e.g. # (#English instead of $English).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 17, 2018 Aug 17, 2018

I try to use:

var english = "hello";

and document.getElementById

....

but nothing work.

But can we use a trick like?

....

var en1 = $english1;

var es1 =$spanish1;

....

....

index.topics[idx].name = index.topics[idx].name.replace(/ *^en1$/, 'es1');

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 17, 2018 Aug 17, 2018

> But can we use a trick like?

> var en1 = $english1;

No: you'll see that the result is '$english1 is undefined'.

> ....replace(/ *^en1$/, 'es1');

doesn't work because you can't use a variable like that inside a regular expression. Use variables like this:

var english1 = "hello";

var spanish1 = "hola";

index.topics[idx].name = index.topics[idx].name.replace (RegExp ('^' + english1 + '$'), spanish1);

But that's expensive because the regexes are compiled at every iteration. Better to compile everything once:

var english1 = /^ *hello$/;

var spanish1 = "hola";

index.topics[idx].name = index.topics[idx].name.replace (english1, spanish1);

and it would probably be a good idea to make one pass through the topic names to remove any leading spaces, and remove that space remover from the translate strings:

var english1 = /^hello$/;

P.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 18, 2018 Aug 18, 2018

I follow your instructions, but didn`t work.

Do you see something wrong?

MyScritp1.jsx

(function () {

    #include translate.jsx;

    var doc = app.activeDocument,    

        indexIdx = doc.indexes.length - 1;  

  

  

    function removeNewEntriesFromIndex(index) {

          

        var idx = index.topics.length - 1;      

        for (; idx >= 0; idx -= 1) {    

            $.writeln(idx);  

            index.topics[idx].name = index.topics[idx].name.replace(english1, spanish1);  

        }    

    }    

        

    for (; indexIdx >= 0; indexIdx -= 1) {    

        removeNewEntriesFromIndex(doc.indexes[indexIdx]);    

    }    

}());

translate.jsx

var english1 = /^hello$/;

var spanish1 = "hola"; 

Thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 18, 2018 Aug 18, 2018

Works fine over here. In what way did it not work at your end?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 18, 2018 Aug 18, 2018
LATEST

Thanks so much, Petter for your patience.

is working perfectly.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines