Hi,
my Indesign book has severall bookmarks. Now I have to make a french version of the book. The Indesign documents are ready, I just have to rename the bookmarks. Lazy as I am, I want to do it by script (I know there will be follow 10 mare translations.. pff)
I thought to make an excell with in the first column the original bookmarks and in the second column the translations.
this is what I get on this moment, but it won't work..
tell application "Microsoft Excel"
activate
tell active workbook
tell active sheet
repeat with i from 1 to 21
set originalValue to "formula of cell ("A" & i)
set replacementValue to formula of cell ("B" & i)
end repeat
end tell
end tell
end tell
tell application "Adobe InDesign CS5"
activate
tell active document
set allBookmark to every bookmark
repeat with thisBookmark in allBookmark
try
if (name of thisBookmark) as string is equal to originalValue as string then
set name of thisboormark to replacementValue
end if
end try
end repeat
end tell
end tell
I don't script Excel ( you should take a look at FMP btw )… Anyhows this is not going to work… You loop a workbook but only and end up with 2 variables ( the last ones ) Then you loop ID bookmarks its only ever going to change the last ones… You say it don't work but NOT why or the result of running the script… You can do one of two things loop twice as you have but create and pass lists from one app to the other or loop Excel and call a sub-routine/handler to make the ID change… Here is where your result is ( see bottom of panel if you are using AppleScript Editor )…
Oh… The other thing is you have try end try blocks that have NO on error so this will always fail silently by… Remove or handle while debugging your script… ![]()
G'day
Sorry I don't have time to completely review where the problem may be in your script...
..but if you're having trouble with the Excel part you may find the info I wrote at MacGrunt useful.
that script uses Excel to generate two lists for changing filenames — so, somewhat similar to what you are trying to do.
There's a brief explanation of the vagaries of getting data from excel.
Getting the complete set of data from each column right from the start (as I do in that script) will probably be quicker than looping repeatedly through Excel.
Good luck with it.
m.
Hello,
I could make a script to change the name of the bookmarks. Now he changes the bookmarks but he doesn't change the sub bookmarks (I thinks they are called Children). How can I add the sub bookmarks to this scrript?
tell application "Adobe InDesign CS5"
tell active document
set allBookmarks to every bookmark
repeat with x from 1 to count of allBookmarks
log name of item x of allBookmarks as list
set name of item x of allBookmarks to my getTextInExcel(x)
end repeat
end tell
end tell
on getTextInExcel(x)
tell application "Microsoft Excel"
tell active workbook
tell active sheet
set thisVal to formula of cell ("A" & x)
log thisVal
end tell
end tell
end tell
return thisVal
end getTextInExcel
Here is the script:
//======================================================================================
const gScriptName = "Rename InDesign bookmarks";
const gScriptVersion = "2.0";
var gCsvArr, doc, gDocName;
var gCounter = 0;
var gAllBookmarks = [];
Main();
//===================================== FUNCTIONS ======================================
function Main() {
var bookmark;
if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);
var doc = app.activeDocument;
gDocName = GetFileName(doc.name);
var csvFile = File.openDialog("Locate a CSV file", "CSV Files:*.CSV");
if (csvFile == null) exit();
gCsvArr = ReadCsvData(csvFile);
var bookmarks = doc.bookmarks;
for (var i = 0; i < bookmarks.length; i++) {
bookmark = bookmarks[i];
GetAllBookmarks(bookmark);
}
if (gAllBookmarks.length == 0) ErrorExit("There are no bookmarks in this document.", true);
ReplaceFunnyCharsInBookmarkNames();
ProcessBookmarks();
GetNotRenamedBookmarks(csvFile.name);
alert("Finished. " + gCounter + " out of " + gAllBookmarks.length + " bookmarks were renamed.", gScriptName + " - " + gScriptVersion);
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
function ReplaceFunnyCharsInBookmarkNames() {
for (var i = 0; i < gAllBookmarks.length; i++) {
bookmark = gAllBookmarks[i];
bookmark.name = bookmark.name.replace(/(\r|\n)/g, "");
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
function GetAllBookmarks(bookmark) {
bookmark.label = "";
gAllBookmarks.push(bookmark);
var bookmarks = bookmark.bookmarks;
for (var i = 0; i < bookmarks.length; i++) {
GetAllBookmarks(bookmarks[i]);
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function GetNotRenamedBookmarks(csvFileName) {
var bookmark;
var notRenamedBookmarks = [];
for (var i = 0; i < gAllBookmarks.length; i++) {
bookmark = gAllBookmarks[i];
if (bookmark.label != "ok") notRenamedBookmarks.push(bookmark);
}
if (notRenamedBookmarks.length > 0) {
WriteToFile(GetDate());
WriteToFile("InDesign file: " + gDocName);
WriteToFile("CSV-file: " + csvFileName + "\r");
for (var k = 0; k < notRenamedBookmarks.length; k++) {
WriteToFile(notRenamedBookmarks[k].name);
}
WriteToFile("\r");
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function ReadCsvData(csvFile) {
var line, temp, csvRecord, tempArr;
var csvData = [];
csvFile.open("r");
while (!csvFile.eof) {
line = csvFile.readln();
if (line.match(/^Volume/) != null) continue; // skip the header
if (/;{2,}/g.test(line)) continue; // skip empty lines with two or more semicolumns
tempArr = line.split(";");
csvData.push(tempArr);
}
csvFile.close();
return csvData;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function ProcessBookmarks() {
var bookmark, findWhat, changeTo, columnC;
for (var i = 0; i < gAllBookmarks.length; i++) {
bookmark = gAllBookmarks[i];
for (var j = 0; j < gCsvArr.length; j++) {
findWhat = gCsvArr[j][1].replace(/(^"|"$)/g, ""); // column B
changeTo = gCsvArr[j][3].replace(/(^"|"$)/g, ""); // column C (0 - 1st, 1 - 2nd, 2 - 3rd and so on)
if (findWhat == bookmark.name) {
bookmark.name = changeTo;
bookmark.label = "ok";
gCounter++;
}
} // for j
} // for i
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function ErrorExit(error, icon) {
alert(error, gScriptName + " - " + gScriptVersion, icon);
exit();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function WriteToFile(text) {
var file = new File("~/Desktop/" + gDocName + ".txt");
if (file.exists) {
file.open("e");
file.seek(0, 2);
}
else {
file.open("w");
}
file.write(text + "\r");
file.close();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function GetFileName(fileName) {
var str = "";
var res = fileName.lastIndexOf(".");
if (res == -1) {
str = fileName;
}
else {
str = fileName.substr(0, res);
}
return str;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function GetDate() {
var date = new Date();
if ((date.getYear() - 100) < 10) {
var year = "0" + new String((date.getYear() - 100));
} else {
var year = new String ((date.getYear() - 100));
}
var dateString = (date.getMonth() + 1) + "/" + date.getDate() + "/" + year + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
return dateString;
}
You have to edit ReadCsvData and ProcessBookmarks functions (see comments)
For example, in my sheet, the header contains word "Volume" (cell A:1) so I use this line to skip it:
if (line.match(/^Volume/) != null) continue;
In findWhat and changeTo variables you have to set number of columns you want to use (I use B and C)
This script was written for csv-files exported from Excel 2011 (Mac). BTW, my version of Excel 2007 (PC) exports csv-file a little differently.
Hope this helps.
Kas
Here is a newer version of the script (I forgot about it yesterday) which works with csv-files exported from Excel 2011 for Mac, the previous one is for CSVs exported from Excel 2007 (PC).
const gScriptName = "Rename InDesign bookmarks";
const gScriptVersion = "2.3";
var gCsvArr, gDocName;
var gCounter = 0;
var gAllBookmarks = [];
Main();
//===================================== FUNCTIONS ======================================
function Main() {
var bookmark;
if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);
var doc = app.activeDocument;
gDocName = GetFileName(doc.name);
var csvFile = File.openDialog("Locate a CSV file", "CSV Files:*.CSV");
if (csvFile == null) exit();
gCsvArr = ReadCsvData(csvFile);
var bookmarks = doc.bookmarks;
for (var i = 0; i < bookmarks.length; i++) {
bookmark = bookmarks[i];
GetAllBookmarks(bookmark);
}
if (gAllBookmarks.length == 0) ErrorExit("There are no bookmarks in this document.", true);
ReplaceFunnyCharsInBookmarkNames();
ProcessBookmarks();
GetNotRenamedBookmarks(csvFile.name);
alert("Finished. " + gCounter + " out of " + gAllBookmarks.length + " bookmarks were renamed.", gScriptName + " - " + gScriptVersion);
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
function ReplaceFunnyCharsInBookmarkNames() {
for (var i = 0; i < gAllBookmarks.length; i++) {
bookmark = gAllBookmarks[i];
bookmark.name = bookmark.name.replace(/(\r|\n)/g, "");
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
function GetAllBookmarks(bookmark) {
bookmark.label = "";
gAllBookmarks.push(bookmark);
var bookmarks = bookmark.bookmarks;
for (var i = 0; i < bookmarks.length; i++) {
GetAllBookmarks(bookmarks[i]);
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function GetNotRenamedBookmarks(csvFileName) {
var bookmark;
var notRenamedBookmarks = [];
for (var i = 0; i < gAllBookmarks.length; i++) {
bookmark = gAllBookmarks[i];
if (bookmark.label != "ok") notRenamedBookmarks.push(bookmark);
}
if (notRenamedBookmarks.length > 0) {
WriteToFile(GetDate());
WriteToFile("InDesign file: " + gDocName);
WriteToFile("CSV-file: " + csvFileName + "\r");
for (var k = 0; k < notRenamedBookmarks.length; k++) {
WriteToFile(notRenamedBookmarks[k].name);
}
WriteToFile("\r");
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function ReadCsvData(csvFile) {
var line, temp, csvRecord, tempArr;
var csvData = [];
csvFile.open("r");
while (!csvFile.eof) {
line = csvFile.readln();
if (line.match(/Volume/) != null) continue; // skip the header
if (line == "") continue; // skip empty lines
if (/;{2,}/g.test(line)) continue; // skip empty lines with two or more semicolons
if (/,{2,}/g.test(line)) continue; // skip empty lines with two or more commas
tempArr = line.split('""","""');
csvData.push(tempArr);
}
csvFile.close();
return csvData;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function ProcessBookmarks() {
var bookmark, findWhat, changeTo;
for (var i = 0; i < gAllBookmarks.length; i++) {
bookmark = gAllBookmarks[i];
for (var j = 0; j < gCsvArr.length; j++) {
findWhat = gCsvArr[j][1].replace(/(^"+|"+$)/g, "");
changeTo = gCsvArr[j][3].replace(/(^"+|"+$)/g, "");
if (findWhat == "" || changeTo == "") continue; // skip if there is a missing value from the column B or D (the search and replace columns)
if (findWhat == bookmark.name) {
bookmark.name = changeTo;
bookmark.label = "ok";
gCounter++;
}
} // for j
} // for i
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function ErrorExit(error, icon) {
alert(error, gScriptName + " - " + gScriptVersion, icon);
exit();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function WriteToFile(text) {
var file = new File("~/Desktop/" + gDocName + ".txt");
if (file.exists) {
file.open("e");
file.seek(0, 2);
}
else {
file.open("w");
}
file.write(text + "\r");
file.close();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function GetFileName(fileName) {
var str = "";
var res = fileName.lastIndexOf(".");
if (res == -1) {
str = fileName;
}
else {
str = fileName.substr(0, res);
}
return str;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function GetDate() {
var date = new Date();
if ((date.getYear() - 100) < 10) {
var year = "0" + new String((date.getYear() - 100));
} else {
var year = new String ((date.getYear() - 100));
}
var dateString = (date.getMonth() + 1) + "/" + date.getDate() + "/" + year + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
return dateString;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
Could you send me an example of you csv file?
I can't give you the "real" csv-file because it was given me by my client and I keep such materials confidential.
I created samples myself and posted them here. I don't have Excel 2011 for Mac so they were created on PC -- some hidden characters may be different.
Here is how it looks in Excel:
A csv exported from Excel 2011 (Mac) looks like so:
And here is a sample of csv-file exported from Excel 2007 (PC):
PC version -- in "save as" dialog box, there is an option: CSV separated by commas (I have Russian version of Excel -- it's my own translation), but in fact the exported file is semicolon separated.
MAC version -- my client told me that he could export only comma separated files. However, commas are used in text too. That's why cells are enclosed by three pairs of quotes.
This is the main difference between Mac and PC versions of csv-files.
Kas
North America
Europe, Middle East and Africa
Asia Pacific