Copy link to clipboard
Copied
Hi, I am trying to place an image onto a rectangle object [rectangle frame tool] inside an event. I do not recieve any error but image is not placed. I am stopping the event propogation and event defaults after placing as i do not want the original intented operation to take place, just make use of the event that is called to cater to my needs . I tired by putting the sleep call as well to see if it was getting placed and then getting removed but that is also not the case. I am not sure what i am doing wrong or missing here.
this is my code
app.addEventListener('beforeImport',function(theEvent){
try{
var doc = theEvent.parent;
if ( !( doc instanceof Document ) ) {
alert('Not instance of document');
return;
}
var rect = theEvent.currentTarget.selection[0];
var placedImagePath = decodeURI(theEvent.fullName.toString());
var placedImageName= placedImagePath.substring(placedImagePath.lastIndexOf("/")+1);
if(validateString(placedImageName)==false){
var pathToPlaceNewImageForSpecialChar = doc.extractLabel("pathToPlaceNewImageForSpecialChar");
if(pathToPlaceNewImageForSpecialChar==""){
alert('Error');
}
else{
callMainDialog(placedImageName,placedImagePath,pathToPlaceNewImageForSpecialChar,rect);
//$.sleep("5000");
theEvent.stopPropagation();
theEvent.preventDefault();
}
}
}
catch(ex){
alert(ex.message);
}
});
function callMainDialog(offendingFileName,originalFilePath,fileDownloadPath,rect){
var w = new Window ("dialog", "Rename");
w.group = w.add ('group');
w.group.add ('statictext {text: "Please enter a new name for the file. "}');
w.input = w.group.add ('edittext {characters: 30, active: true,text: "'+ offendingFileName+'"}');
var offendingTextLabel = w.add ('statictext {text: "offending characters : '+ getOffendingText(offendingFileName) +'"}');
offendingTextLabel.graphics.foregroundColor = offendingTextLabel.graphics.newPen (offendingTextLabel.graphics.PenType.SOLID_COLOR, [1, 0,0], 1);
offendingTextLabel.alignment ="left";
var CreateCopy = w.add ("checkbox", undefined, "Create a copy of the file");
CreateCopy.alignment="left";
var pathLabel=w.add ("statictext", undefined, "Path: " + fileDownloadPath);
pathLabel.alignment="left";
pathLabel.graphics.font = "dialog:9";
w.buttons = w.add ('group {alignment: "right"}');
w.buttons.add ('button {text: "Cancel"}');
var okButton = w.buttons.add ('button {text: "OK", enabled: false}');
w.input.onChanging = function(){
var str= w.input.text;
okButton.enabled = validateString(str);
offendingTextLabel.text= "offending characters : " + getOffendingText(str);
}
okButton.onClick=function(e){
doOperation(CreateCopy.value,w.input.text,originalFilePath,fileDownloadPath,rect);
//e.stopPropagation();
//e.preventDefault();
w.close();
};
w.show();
}
function doOperation(isChecked,newFileName,originalFilePath,fileDownloadPath,rect){
if(isChecked==true){
CopyMyFile(originalFilePath,fileDownloadPath);
placeFile(fileDownloadPath + newFileName,rect);
}
else{
RenameMyFile( originalFilePath,newFileName);
var newPath = originalFilePath.substring(0,originalFilePath.lastIndexOf("/")+1) + newFileName;
placeFile(newPath,rect);
}
}
function placeFile(imagePath,rect){
var newFile = new File(imagePath);
rect.place(newFile,false,null);
}
Copy link to clipboard
Copied
Is the rectangle undefined content or image content?
If the rectanle is text content (or any rectangle where the text tool has converted it to a text frame), placing an image will fail.
Copy link to clipboard
Copied
Rectangle here is the empty rectangle frame tool already present on the document which is selected by the user to place a new image upon it. I am checking if the image name is valid, if not then giving user the option to either rename or create a copy of the image and then placing the renamed or copied image over the rectangle and stopping the default image from being placed.
Copy link to clipboard
Copied
Hi @Ujjwal_u2 , I don’t think you can stop the place, but the listener function could get the event parent and relink a new image. Also I think you’ll need #targetengine "session"
#targetengine "session";
var el = app.eventListeners.add("beforePlace", placeImage);
/**
* Function to run before a place command
* @ param the event
*/
function placeImage(e){
alert("About to place on a selected " + e.parent.constructor.name + ". Relink code here")
}
Copy link to clipboard
Copied
Hi Rob, i have resolved the issue. target engine i had already specified in my code, forgot to paste here. Issue was with the placement of file while the dialog was open. Intrestingly enough i was not receving any error from Indesign but when i put the place function inside try catch block then it told me that dialog is open and no operation can be perform, so i changed the dialog to the palette and it worked. 🙂 thanks for the reply.
Copy link to clipboard
Copied
Ok, we cross posted
Copy link to clipboard
Copied
You might have to listen for the afterPlace event and relink. Something like this?:
#targetengine "session";
var el = app.eventListeners.add("afterPlace", relinkImage);
/**
* Function to run after a place command
* @ param the event
*/
function relinkImage(e){
alert("New placed link " + e.parent.itemLink.name + ". Relink code here")
//e.parent.itemLink.relink(//filepath here);
}