Copy link to clipboard
Copied
Hi Manan,
Since the previous Post was successfully closed, I have created a new Post to address a new problem.
The script given by Manan, is working fine, but I noticed that if the InDesign file has Vector graphics like .eps or .pdf files, then it is giving an error regarding the event handler, see snapshot.
Can you please check if this can be fixed.
Thanks,
Masood
//DESCRIPTION: Event Listener to check if image is scaled <80% or >120%
#targetengine "save"
app.addEventListener("beforeSave", function(){
for(var i = 0; i < app.documents[0].allGraphics.length; i++)
{
var img = app.documents[0].allGraphics[i]
if((img.absoluteHorizontalScale < 80 || img.absoluteHorizontalScale > 120) || (img.absoluteVerticalScale < 80 || img.absoluteVerticalScale > 120))
{
alert("Scaling of Image(s) is below 80% and/or above 120%. Please Fix it!")
break;
}
}
})
Works fine for me with your document as well, see the screengrab of me testing your document
I tested on CC2020, but I am not sure why it would break on CC2019. Are you sure you have no other script installed that registers event handler for beforeSave event? If yes then try the following version with try catch, it will silently ignore the graphic that is crashing and move on to check the others
#targetengine "save"
app.addEventListener("beforeSave", function(){
for(var i = 0; i < app.documents
...
Copy link to clipboard
Copied
Can you share a small document that has this issue?
-Manan
Copy link to clipboard
Copied
Hi Manan,
Here's the InDesign file for your review. Page one has vector graphics (resolution free) and page two has the raster image. You can test with or without the raster image.
https://www.dropbox.com/s/v4b5vfrk5cwlwdm/ScaledImage.zip?dl=0
Thanks,
Masood
PS: I'm on InDesign CC 2019.
Copy link to clipboard
Copied
Works fine for me with your document as well, see the screengrab of me testing your document
I tested on CC2020, but I am not sure why it would break on CC2019. Are you sure you have no other script installed that registers event handler for beforeSave event? If yes then try the following version with try catch, it will silently ignore the graphic that is crashing and move on to check the others
#targetengine "save"
app.addEventListener("beforeSave", function(){
for(var i = 0; i < app.documents[0].allGraphics.length; i++)
{
try
{
var img = app.documents[0].allGraphics[i]
if((img.absoluteHorizontalScale < 80 || img.absoluteHorizontalScale > 120) || (img.absoluteVerticalScale < 80 || img.absoluteVerticalScale > 120))
{
alert("Scaling of Image(s) is below 80% and/or above 120%. Please Fix it!")
break;
}
}catch(e){}
}
})
-Manan
Copy link to clipboard
Copied
Try & Catch did the magic. Thanks, Manan for your timely support.
Copy link to clipboard
Copied
Hi,
Is this the right code to alert Embedded/Pasted images in a document. An Add-On to the code, above.
if (img.itemLink.status == null) {
alert("Document has Embedded/Pasted Image(s). Please Fix it!")
}
Copy link to clipboard
Copied
For the embedded link, it should be the following
LinkStatus.LINK_EMBEDDED
for the pasted image null check is fine
-Manan
Copy link to clipboard
Copied
Thanks, it worked. Here's my complete script:
//DESCRIPTION: Alert for Scaled, Low-Res, Embedded and Pasted Images
#targetengine "save"
app.addEventListener("beforeSave", function () {
for (var i = 0; i < app.documents[0].allGraphics.length; i++) {
try {
var img = app.documents[0].allGraphics[i]
if (img.absoluteHorizontalScale > 110 || img.absoluteVerticalScale > 110) {
alert("Scaling of Image(s) is above 110%. Please Fix it!")
}
if (img.effectivePpi[0] < 300 || img.effectivePpi[1] < 300) {
alert("Effective Resolution of the Image(s) is below 300 dpi. Please Fix it!")
break;
}
} catch (e) { }
}
})
app.addEventListener("beforeSave", function () {
for (var i = 0; i < app.documents[0].allGraphics.length; i++) {
try {
var img = app.documents[0].allGraphics[i]
if (img.itemLink == null) {
alert("Document has Pasted Image(s). Please Fix it!")
break;
}
} catch (e) { }
}
})
app.addEventListener("beforeSave", function () {
for (var i = 0; i < app.documents[0].allGraphics.length; i++) {
try {
var img = app.documents[0].allGraphics[i]
if (img.itemLink.status == LinkStatus.LINK_EMBEDDED) {
alert("Document has Embedded Image(s). Please Fix it!")
break;
}
} catch (e) { }
}
})
Copy link to clipboard
Copied
Hi Masood,
testing eventlisteners on object app can be a bit tricky.
Especially if you execute a modified script the second or third time without removing the listener first.
If you want to get a clean situation do a restart of InDesign before running the script again.
It's also a good idea to give the eventlistener a "unique" name and test if it is already running.
Warning! Do NEVER do something like this:
app.eventListeners.everyItem().remove();
You will be able to wreck other running scripts from other developers that are started at runtime or the user is currently using with a ScriptUI palette kind of window. Also note, that InDesign itself is using some scripts at start-up that are pre-installed.
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Hi Uwe,
I always restart my InDesign after any changes to the script. So it's a teadious task, but I do it.
Thanks for your suggestions.
Masood