Reply To: Script to change the language of all text on document

#61198

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].

This article was last modified on December 1, 2011

Comments (0)

Loading comments...