JavaScript:
#target indesign
#targetengine 'Prefix Postfix Relinks'
var doc = app.activeDocument;
function main(){
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
if (app.documents.length != 0){
if (doc.links.length != 0){
myDisplayDialog();
}
else {
alert("В документе нет прилинкованых файлов");
}
}
else{
alert("Нет открытых документов.");
}
}
function myDisplayDialog(){
var myDialog = app.dialogs.add({name:"Replace links"});
with(myDialog){
with(dialogColumns.add()){
var myChangeFolderButtons = radiobuttonGroups.add();
with(myChangeFolderButtons){
radiobuttonControls.add({staticLabel:"Изменить каталог", checkedState:false});
}
with(borderPanels.add()){
var myPrefixButtons = radiobuttonGroups.add();
with(myPrefixButtons){
radiobuttonControls.add({staticLabel:"Убрать", checkedState:true});
radiobuttonControls.add({staticLabel:"Добавить"});
with (dialogColumns.add()){
staticTexts.add({staticLabel:"Префикс:"});
}
with (dialogColumns.add()){
var myPrefixField = textEditboxes.add({editContents:""});
}
}
var myPostfixButtons = radiobuttonGroups.add();
with(myPostfixButtons){
radiobuttonControls.add({staticLabel:"Убрать", checkedState:true});
radiobuttonControls.add({staticLabel:"Добавить"});
with (dialogColumns.add()){
staticTexts.add({staticLabel:"Постфикс:"});
}
with (dialogColumns.add()){
var myPostfixField = textEditboxes.add({editContents:""});
}
}
}
}
var myResult = myDialog.show();
if (myResult == true){
var myPrefixRange = myPrefixButtons.selectedButton;
var myPrefixValue = myPrefixField.editContents.toLocaleLowerCase();
var myPostfixRange = myPostfixButtons.selectedButton;
var myPostfixValue = myPostfixField.editContents.toLocaleLowerCase();
var myChangeFolderRange = myChangeFolderButtons.selectedButton;
var mySelectedFolder = false
if (myChangeFolderRange == 0){
var mySelectedFolder = SelectFolder();
}
if ((myPrefixValue != "") || (myPostfixValue != "") || (mySelectedFolder != false) ){
choiceAction (myPrefixRange, myPrefixValue, myPostfixRange, myPostfixValue, mySelectedFolder)
}
else{
alert("Ничего не выбрано, завершение без изменений");
}
myDialog.destroy();
}
}
}
function choiceAction (myPrefixRange, myPrefixValue, myPostfixRange, myPostfixValue, mySelectedFolder) {
var argRange = myPrefixRange * 2 + myPostfixRange;
switch (argRange) {
case 0:
RemovePrefixPostfix (myPrefixValue, myPostfixValue, mySelectedFolder)
if (wrong.length != 0) {
alert_scroll ("Не найдены:", wrong);
}
break;
case 1:
RemovePrefixAddPostfix (myPrefixValue, myPostfixValue, mySelectedFolder)
if (wrong.length != 0) {
alert_scroll ("Не найдены:", wrong);
}
break;
case 2:
AddPrefixRemovePostfix (myPrefixValue, myPostfixValue, mySelectedFolder);
if (wrong.length != 0) {
alert_scroll ("Не найдены:", wrong);
}
break;
case 3:
AddPrefixAddPostfix (myPrefixValue, myPostfixValue, mySelectedFolder);
if (wrong.length != 0) {
alert_scroll ("Не найдены:", wrong);
}
break;
}
}
function SelectFolder () {
var nFolder = File().parent.selectDlg ('Выберите папку с файлами для замены');
if (!nFolder) {
return false;
}
else {
return nFolder;
}
}
function RemovePrefixPostfix (Prefix, Postfix, mySelectedFolder) {
wrong = [];
for (var i = 0; i < doc.links.length; i++) {
var link = doc.links[i];
var PathFile = File(link.filePath);
var FileNameExt = decodeURI(PathFile.name.toString().toLocaleLowerCase());
var FileName = FileNameExt.substring(0, (FileNameExt.lastIndexOf(".")));
var Ext = FileNameExt.substring(FileNameExt.lastIndexOf("."), FileNameExt.length);
if ((FileName.indexOf(Prefix) == 0) && (Prefix !== "")) {
var FileName = FileName.substring(Prefix.length, FileName.length);
}
if ((FileName.lastIndexOf(Postfix) == FileName.length - Postfix.length ) && (FileName.length >= Postfix.length) && (Postfix !== "")){
var FileName = FileName.substring(0, FileName.length-Postfix.length);
}
var nFileNameExt = FileName + Ext;
if (mySelectedFolder != false) {
var nPathFile = File(mySelectedFolder + '/' + nFileNameExt)
}
else {
var nPathFile = File(link.filePath.toLocaleLowerCase().replace(FileNameExt, (nFileNameExt)));
}
if (nPathFile.exists) {
link.relink(nPathFile);
}
else {
var wrongPathFile = decodeURI(nPathFile)
wrong.push(nPathFile);
}
}
}
function AddPrefixRemovePostfix (Prefix, Postfix, mySelectedFolder) {
wrong = [];
for (var i = 0; i < doc.links.length; i++) {
var link = doc.links[i];
var PathFile = File(link.filePath);
var FileNameExt = decodeURI(PathFile.name.toString().toLocaleLowerCase());
var FileName = FileNameExt.substring(0, (FileNameExt.lastIndexOf(".")));
var Ext = FileNameExt.substring(FileNameExt.lastIndexOf("."), FileNameExt.length);
if ((FileName.lastIndexOf(Postfix) == FileName.length - Postfix.length ) && (FileName.length >= Postfix.length) && (Postfix !== "")){
var FileName = FileName.substring(0, FileName.length-Postfix.length);
}
if (Prefix !== ""){
var FileName = Prefix + FileName;
}
var nFileNameExt = FileName + Ext;
if (mySelectedFolder != false) {
var nPathFile = File(mySelectedFolder + '/' + nFileNameExt)
}
else {
var nPathFile = File(link.filePath.toLocaleLowerCase().replace(FileNameExt, (nFileNameExt)));
}
if (nPathFile.exists) {
link.relink(nPathFile);
}
else {
var wrongPathFile = decodeURI(nPathFile)
wrong.push(wrongPathFile);
}
}
}
function RemovePrefixAddPostfix (Prefix, Postfix, mySelectedFolder) {
wrong = [];
for (var i = 0; i < doc.links.length; i++) {
var link = doc.links[i];
var PathFile = File(link.filePath);
var FileNameExt = decodeURI(PathFile.name.toString().toLocaleLowerCase());
var FileName = FileNameExt.substring(0, (FileNameExt.lastIndexOf(".")));
var Ext = FileNameExt.substring(FileNameExt.lastIndexOf("."), FileNameExt.length);
if ((FileName.indexOf(Prefix) == 0)&&(Prefix !== "")) {
var FileName = FileName.substring(Prefix.length, FileName.length);
}
if (Postfix !== ""){
var FileName = FileName + Postfix;
}
var nFileNameExt = FileName + Ext;
if (mySelectedFolder != false) {
var nPathFile = File(mySelectedFolder + '/' + nFileNameExt)
}
else {
var nPathFile = File(link.filePath.toLocaleLowerCase().replace(FileNameExt, (nFileNameExt)));
}
if (nPathFile.exists) {
link.relink(nPathFile);
}
else {
var wrongPathFile = decodeURI(nPathFile)
wrong.push(wrongPathFile);
}
}
}
function AddPrefixAddPostfix (Prefix, Postfix, mySelectedFolder) {
wrong = [];
for (var i = 0; i < doc.links.length; i++) {
var link = doc.links[i];
var PathFile = File(link.filePath);
var FileNameExt = decodeURI(PathFile.name.toString().toLocaleLowerCase());
var FileName = FileNameExt.substring(0, (FileNameExt.lastIndexOf(".")));
var Ext = FileNameExt.substring(FileNameExt.lastIndexOf("."), FileNameExt.length);
if (Prefix !== ""){
var FileName = Prefix + FileName;
}
if (Postfix !== ""){
var FileName = FileName + Postfix;
}
var nFileNameExt = FileName + Ext;
if (mySelectedFolder != false) {
var nPathFile = File(mySelectedFolder + '/' + nFileNameExt)
}
else {
var nPathFile = File(link.filePath.toLocaleLowerCase().replace(FileNameExt, (nFileNameExt)));
}
if (nPathFile.exists) {
link.relink(nPathFile);
}
else {
var wrongPathFile = decodeURI(nPathFile)
wrong.push(wrongPathFile);
}
}
}
function alert_scroll (title, input){
if (input instanceof Array)
input = input.join ("\r");
var w = new Window ("dialog", title);
var list = w.add ("edittext", undefined, input, {multiline: true, scrolling: true});
list.maximumSize.height = w.maximumSize.height-100;
list.minimumSize.width = 550;
w.add ("button", undefined, "Close", {name: "ok"});
w.show();
}
main();