You must be logged in to post Login

Search Forums:


 






Script to change the language of all text on document

UserPost

9:03 pm
October 18, 2011


Cooch

Community Member

posts 5

Hi all,


I was wondering if anyone has or knows of a script that will do the same thing as…


Find/Change

Find Format – Language (USA)

Change Format – Language (UK)


I've built a custom dictionary for our specific type of work, but to use it on existing documents I need to do this find/change first.

To run a script over a document when it's first opening would be a huge saving in time considering I get between 20 and 30 jobs through here per day.


Any help would be greatly appreciated

Thanks in advance

Graeme

2:26 am
October 19, 2011


Kasyan Servetsky

Kiev, Ukraine

Member

posts 65

Main();

function Main() {
    if (app.documents.length == 0) {
        alert("Please open a document and try again.");
        exit();
    }
    var doc = app.activeDocument;
    app.findTextPreferences = app.changeTextPreferences = null;
    app.findTextPreferences.appliedLanguage = "English: USA";
    app.changeTextPreferences.appliedLanguage = "English: UK";
    doc.changeText();
    app.findTextPreferences = app.changeTextPreferences = null;
}

2:55 am
October 19, 2011


Cooch

Community Member

posts 5

That's awesome Kasyan, thank you so much.


I've just saved and installed it and it works a treat, really wish I knew how to write a script.


Thanks again.

Graeme.

1:42 am
October 20, 2011


Kasyan Servetsky

Kiev, Ukraine

Member

posts 65

Here I posted some links for first-time scripters. From my own experience (I'm not a programmer by background), I can tell you that EVERYBODY can write scripts — the devil is not so terrible as he is painted!

The most difficult is to make the first step. But when you make progress in it, another problem appears: you can't stop — scripting is very absorbing.

On this site, I find a lot of information on how to use InDesign — both for beginners and advanced users. There are many "ready to use" scripts here as well. But I've never seen a scripting tutorial here and remember reading only one scripting trick lately.

I think this is a big omission for indesignsecrets and suggest that a series of scripting lessons, tutorials, articles, tips & tricks, etc. should be started here on regular basis.

9:02 am
October 20, 2011


David Blatner

Admin

posts 823

Kasyan, I think you have a great idea. We will certainly think about how we can include some beginner scripting articles here, trying not to scare away designers and other users. :)


co-host, InDesignSecrets.com

2:08 am
December 1, 2011


TheoDuijkers

Community Member

posts 3

would be even better if I had a simple dialog window where I could choose source and target language. anyone?

2:58 am
December 1, 2011


Jongware

Member

posts 764

Sure, no problem. May Be Handy: if you have some text selected, it will be the initial language in the Find box.


Main();
function Main() {
if (app.documents.length == 0) {
alert("Please open a document and try again.");
exit();
}
langList = app.languagesWithVendors.everyItem().name.sort();
langList.splice (0,0, langList[langList.length-1]);
langList.pop();

preselected = 0;
if (app.selection.length == 1 && app.selection[0].hasOwnProperty("appliedLanguage"))
{
for (i=0; i<langList.length; i++)
{
if (langList[i] == app.selection[0].appliedLanguage.name)
{
preselected = i;
break;
}
}
}

var langDialog = app.dialogs.add({name:"Find/Replace Languages", canCancel:true});
with (langDialog)
{
with(dialogColumns.add())
{
with(dialogRows.add())
{
staticTexts.add({staticLabel:"&Find", minWidth:40});
findDropDown = dropdowns.add ({stringList:langList, selectedIndex:preselected});
}
with(dialogRows.add())
{
staticTexts.add({staticLabel:"&Change", minWidth:40});
chngDropDown = dropdowns.add ({stringList:langList, selectedIndex:0});
}
}
}

if (langDialog.show() == true)
{
var doc = app.activeDocument;
app.findTextPreferences = app.changeTextPreferences = null;
app.findTextPreferences.appliedLanguage = langList[findDropDown.selectedIndex];
app.changeTextPreferences.appliedLanguage = langList[chngDropDown.selectedIndex];
doc.changeText();
app.findTextPreferences = app.changeTextPreferences = null;
}
}

The convulated stuff right after retrieving the language list is because the languages don't come in their usual sorted order. It's easy to apply a sort, but then [No Language] ends up at the bottom. Rather than writing a custom sort function, I merely move the last item up to the top. I'm only assuming that'll actually always be [No Language].

1:59 am
December 2, 2011


TheoDuijkers

Community Member

posts 3

amazing, works like a charm. exactly what I was looking for. tnx

10:47 am
December 2, 2011


David W. Goodrich

Community Member

posts 18

I don't mean to look a gift horse in the mouth, but this very handy script seems not to work for languages that don't come with ID, such as Chinese and Japanese (it took me a while to realize my ID grabbed those languages from imported files).  Poking around, I found an older script Jongware made available over on Adobe's ID scripting forum that does find all the languages in a document, though so thorough a check takes time.  I often get files with all kinds of odd languages, and I can't search for them unless I know they're there.  So I really want to thank Jongware for both scripts.

David

2:26 pm
December 2, 2011


Jongware

Member

posts 764

Well, David, you are correct in your observation of the behavior of these two scripts. The one above inquires InDesign about the list of supported languages, and the one you mention reads them from a document — which, annoyingly, may include text in a language that's actually not supported! So you can't use this script to remove the non-supported languages.

Oh, the conundrum.

I'll take a peek to see if it's possible to combine the two (and make the list of not-supported languages appear in a section of their own, in full ID Font List style. Also, if possible.)

6:38 pm
December 2, 2011


Jongware

Member

posts 764

Okay so this is a tricky one. It appears you cannot easily use "find" with a language that is not in InDesign's internal list.

I've put up a query on Adobe's scripting forum for this, as I got everything working just fine — it's just this one little thing that stumps me!

11:29 pm
January 6, 2012


Eric

Member

posts 33

How exactly do I install this script in ID (I have CS4 and CS5.5 (Mac OS 10.6.8)? And is it Java or AppleScript? I found this explanation of how to install a script (http://indesignsecrets.com/how…..design.php), but since what Jongware provided is the text of the script and not a script file, I don't know what to save it as (.js, .jsx, .as, .scpt).

I went to Apple Script Editor and tried to save it as .scpt, since that's what came up. I got a syntax error: "Unknown token can't go after this ")". It was referring to the ); at the end of Main();. So I'm assuming this was a Java script. Next I went to TextEdit and pasted in Jongware's script saving it as Language_Change_Throughout.jsx. I put it in the Scripts Panel folder, and it now shows up in ID CS5.5. And it works beautifully! Thank you, Jongware!

I am posting this simply for other newbies who might not know how to install the script text above.

1:35 am
January 7, 2012


collywolly

Adelaide, Australia

Member

posts 83

@Eric:


Jongware's script is javascript, not java, nor applescript.


go back to Jongware's post and hit the "select code" button. copy this text to your clipboard.


next, open textedit on your mac (or textwrangler if you have it) and paste the text into a new text file.


from here, save the file with whatever name you like, SO LONG AS the suffix is "jsx".


then, put this into your scripts panel folder of indesign like the indesignsecrets.com page suggests.

Keep an open mind, but not so open that your brain falls out – Richard Dawkins

8:11 am
January 7, 2012


Jongware

Member

posts 764

Eric, sorry — you are correct, I re-read my post and indeed forgot to mention it's Javascript.


InDesign supports three different scripting languages out-of-the-box, and so it would be wise to always state what language is used :-) There are a few small clues, though, hidden inthe "syntax":

AppleScript uses a syntax based on English:

   tell application "InDesign" to copy item 3 of page 1 … — this is a comment

etc. (I'm making this one up as I don't speak AppleScript).

Visual Basic leans heavily on ye olde Basic and has Initial Caps for Everything:

   Sub CopyItem (item) ' a comment after a ' sign

     x = Rectangles.Add()

Javascript looks like … the above :D Lots of curly braces, a semicolon to end each command.