Copy link to clipboard
Copied
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!
> 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
...Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
sorry I didn't know what is #include compiler directive
Copy link to clipboard
Copied
I think the includes have to go inside the anonymous function:
(function () {
#include . . .
// Rest of the code
}());
P.
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
> 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).
Copy link to clipboard
Copied
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');
Copy link to clipboard
Copied
> 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.
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
Works fine over here. In what way did it not work at your end?
Copy link to clipboard
Copied
Thanks so much, Petter for your patience.
is working perfectly.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now