• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

onSave Event Listener to prompt scaled images – Part 2

Explorer ,
Sep 08, 2020 Sep 08, 2020

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.

https://community.adobe.com/t5/indesign/onsave-event-listener-to-prompt-scaled-images/m-p/11408530?p...

 

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

 

Screenshot 2020-09-08 at 21.37.45.png

 

 

//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;
		}
	}
})

 

  

TOPICS
Scripting

Views

291

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 08, 2020 Sep 08, 2020

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
...

Votes

Translate

Translate
Community Expert ,
Sep 08, 2020 Sep 08, 2020

Copy link to clipboard

Copied

Can you share a small document that has this issue?

-Manan

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 08, 2020 Sep 08, 2020

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 08, 2020 Sep 08, 2020

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 09, 2020 Sep 09, 2020

Copy link to clipboard

Copied

Try & Catch did the magic. Thanks, Manan for your timely support.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 09, 2020 Sep 09, 2020

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!")
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 10, 2020 Sep 10, 2020

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

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 10, 2020 Sep 10, 2020

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) { }
    }
})

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 10, 2020 Sep 10, 2020

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 )

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 11, 2020 Sep 11, 2020

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines