I'm trying basic scripting exercices with indesign. I created a script doing basic functions like inserting a textframe, a table, a smiley etc.. It's working
Now I want to create a dialog box to launch each function individually.
But I don't understand why my insertTextFrame function works when launch from main() but doesn't work when launch from the dialog box.
Here is my code :
function insertTextFrame(doc) {
alert("function Insert Text Frame is ON.");
var textFrame = doc.pages[0].textFrames.add();
alert("textFrame added.");
textFrame.geometricBounds = [0, 0, 100, 100];
textFrame.contents = "Hello World";
textFrame.contents += "\r\r";
textFrame.contents += "#waza";
}
function insertTable(doc) {
var numRows = 3;
var numColumns = 4;
var cellWidth = 50;
var cellHeight = 20;
var tableFrame = doc.pages[0].textFrames.add();
tableFrame.geometricBounds = [50, 50, 50 + numRows * cellHeight, 50 + numColumns * cellWidth];
var table = tableFrame.insertionPoints[0].tables.add({
numRows: numRows,
numColumns: numColumns
});
for (var row = 0; row < numRows; row++) {
for (var col = 0; col < numColumns; col++) {
var cell = table.rows[row].cells[col];
cell.contents = "Ligne " + (row + 1) + ", Colonne " + (col + 1);
}
}
}
function insertSmiley(doc) {
var docWidth = doc.documentPreferences.pageWidth;
var docHeight = doc.documentPreferences.pageHeight;
var head = doc.ovals.add({
geometricBounds: [docHeight / 2 - 50, docWidth / 2 - 50, docHeight / 2 + 50, docWidth / 2 + 50],
fillColor: "Yellow",
strokeColor: "Black",
strokeWeight: 1
});
var leftEye = doc.ovals.add({
geometricBounds: [docHeight / 2 - 20, docWidth / 2 - 20, docHeight / 2 + 20, docWidth / 2],
fillColor: "Black",
strokeColor: "Black",
strokeWeight: 1
});
var rightEye = doc.ovals.add({
geometricBounds: [docHeight / 2 - 20, docWidth / 2, docHeight / 2 + 20, docWidth / 2 + 20],
fillColor: "Black",
strokeColor: "Black",
strokeWeight: 1
});
var mouth = doc.graphicLines.add();
mouth.paths[0].entirePath = [
[docWidth / 2 - 20, docHeight / 2 + 40],
[docWidth / 2, docHeight / 2 + 50],
[docWidth / 2 + 20, docHeight / 2 + 40]
];
mouth.strokeColor = "Black";
mouth.strokeWeight = 1;
var smileyGroup = doc.groups.add([head, leftEye, rightEye, mouth]);
smileyGroup.name = "Smiley";
}
function findAndReplace(doc, findText, replaceText) {
// Loop through all stories in the document
for (var i = 0; i < doc.stories.length; i++) {
var story = doc.stories[i];
// Check if the story contains the text to find
if (story.contents.indexOf(findText) !== -1) {
// Create a find/change object
var findChangeObject = app.findTextPreferences;
findChangeObject.findWhat = findText;
// Create a change object
var changeObject = app.changeTextPreferences;
changeObject.changeTo = replaceText;
// Search and replace text in the story
story.changeText();
alert("Text replaced successfully.");
}
}
}
function exportToPdf(doc) {
var pdfPath = "~/Desktop/HelloWorld.pdf";
var pdfFile = new File(pdfPath);
doc.exportFile(ExportFormat.pdfType, pdfFile);
}
function dialogBox(doc) {
// Create a dialog box
var dialog = new Window("dialog", "Script Menu");
// Create buttons for various actions
var btnExit = dialog.add("button", undefined, "Exit");
var btnInsertTextFrame = dialog.add("button", undefined, "Insert Text Frame");
var btnInsertTable = dialog.add("button", undefined, "Insert Table");
var btnInsertSmiley = dialog.add("button", undefined, "Insert Smiley");
var btnFindAndReplace = dialog.add("button", undefined, "Find and Replace");
var btnExportToPdf = dialog.add("button", undefined, "Export to PDF");
// Function to handle button clicks
function buttonClickHandler(buttonName) {
if (buttonName === "Exit") {
dialog.close();
} else if (buttonName === "Insert Text Frame") {
insertTextFrame(doc);
dialog.close();
} else if (buttonName === "Insert Table") {
insertTable(app.activeDocument);
} else if (buttonName === "Insert Smiley") {
insertSmiley(app.activeDocument);
} else if (buttonName === "Find and Replace") {
var findText = "#waza";
var replaceText = "Hello World";
findAndReplace(app.activeDocument, findText, replaceText);
} else if (buttonName === "Export to PDF") {
exportToPdf(app.activeDocument);
}
}
// Add event listeners for button clicks
btnExit.onClick = function () {
buttonClickHandler("Exit");
};
btnInsertTextFrame.onClick = function () {
buttonClickHandler("Insert Text Frame");
};
btnInsertTable.onClick = function () {
buttonClickHandler("Insert Table");
};
btnInsertSmiley.onClick = function () {
buttonClickHandler("Insert Smiley");
};
btnFindAndReplace.onClick = function () {
buttonClickHandler("Find and Replace");
};
btnExportToPdf.onClick = function () {
buttonClickHandler("Export to PDF");
};
// Show the dialog box
dialog.show();
}
function main() {
// Check if InDesign is open
if (app.documents.length > 0) {
var doc = app.activeDocument;
// Show the dialog box
dialogBox(doc);
// Insert a text frame
insertTextFrame(doc);
// Insert a table
insertTable(doc);
// Insert a smiley
insertSmiley(doc);
// Find and replace text
var findText = "#waza";
var replaceText = "Hello World";
findAndReplace(doc, findText, replaceText);
// Export to PDF
//exportToPdf(doc);
alert("Operations completed successfully.");
} else {
alert("No open document. Please open a document first.");
}
}
// Appel de la fonction principale
main();
There are two types of interactive windows:
They are very different beasts with different logic of work.
As far as I can tell from your code you mean to make a palette. To accomplice this you have to make these changes:
//@target "indesign"
//@targetengine "session"
var dialog = new Window("dialog", "Script Menu");
with:
var dialog = new Window("palette", "Script Menu");
function main() {
// Check if InDesign is open
if (app.documents.length > 0) {
var doc = app.activeDocument;
// Show the dialog box
dialogBox(doc);
} else {
alert("No open document. Please open a document first.");
}
}
After that it should work as intended.