Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

script needed to remove background colour

Return to Member Forum

  • Author
    Posts
    • #81119
      Paul White
      Member

      anyone know of a script to remove background colour from image picture boxes without deleting the image.

      I use coloured boxes as image placeholders but could do with removing the colour automatically

    • #81121
      Keith Gilbert
      Participant

      This script will remove the background color from any rectangular picture box that contains an image:

      var myDoc = app.activeDocument;
      var myRectangleArray = myDoc.rectangles.everyItem().getElements();
      for (var j = myRectangleArray.length – 1; j >= 0; j–) {
      var myItem = myRectangleArray[j];
      if ((myItem.graphics.length >0 ) || (myItem.images.length > 0)) {
      myItem.fillColor = myDoc.swatches.item(“None”);
      }
      }

    • #81149
      Paul White
      Member

      I thanks for your reply Keith.

      I keep getting a Syntax error. Error number 8 on line 3
      offending text: –

      I know nothing about scripts so totally in your hands

    • #81156
      Peter Kahrel
      Participant

      Paul,

      The forum software played a trick on Keith’s script. In this line:

      for (var j = myRectangleArray.length – 1; j >= 0; j–) {

      the two minuses in j-- were replaced by an en-rule. Line 3 should be

      for (var j = myRectangleArray.length – 1; j >= 0; j--) {

      Peter

    • #81157
      David Blatner
      Keymaster

      Yes, as Peter learned, code on our forum needs to be inside pre or code html tags. :-)
      Thanks to Keith and Peter for jumping in and helping!

    • #81179
      Paul White
      Member

      still getting the same error :(

      var myDoc = app.activeDocument;
      var myRectangleArray = myDoc.rectangles.everyItem().getElements();
      for (var j = myRectangleArray.length – 1; j >= 0; j--) {
      var myItem = myRectangleArray[j];
      if ((myItem.graphics.length >0 ) || (myItem.images.length > 0)) {
      myItem.fillColor = myDoc.swatches.item(“None”);
      }
      }
      

      can anyone have a quick look please?

    • #81180
      Peter Kahrel
      Participant

      There was another instance where the forum software tried to be ‘clever’. Change the curly quotes at None to straight quotes.

    • #81182
      Paul White
      Member

      fixed the curly quotes but still getting syntax error on line 3.
      it was the space between -1;

      var myDoc = app.activeDocument;
      var myRectangleArray = myDoc.rectangles.everyItem().getElements();
      for (var j = myRectangleArray.length -1; j >= 0; j--) {
      var myItem = myRectangleArray[j];
      if ((myItem.graphics.length >0 ) || (myItem.images.length > 0)) {
      myItem.fillColor = myDoc.swatches.item("None");
      }
      }
      

      This works great! The above code works if anyone else needs it and thank you for everyone input.
      Now do I tell it to look for all these in a book not just a single document?

    • #81185
      Peter Kahrel
      Participant

      It was and it wasn’t the space between the – and the 1: the forum software replaced [space]-[space] to [space]en-rule[space].

      To run a script against all book documents, you could use the method described here:
      https://forums.adobe.com/thread/2069523
      See the first post by Vamitul.

    • #81200
      Keith Gilbert
      Participant

      Gah! I was afraid that pasting the code in might chew it up! David, where do I insert the tags? I don’t see anything in the interface. Or do I just put the tags in like this?

    • #81202
      Keith Gilbert
      Participant

      Ah, I see now! I just answered my own question with the test above. I’ll use the tags next time!

      Peter, thanks for intercepting this and helping Paul.
    • #81204
      Paul White
      Member

      Thanks Peter and Keith

      I have used the new script on this project and works great. I put in filled image placeholders to represent size and shape of a product until the images are ready to import and usually forget to remove the background, so this script is a great timing saving check before going to print.

      Is there away to modified this to script a book not just a document?

    • #81206
      Peter Kahrel
      Participant

      Here’s a wrapper to deal with books. Make sure there’s an open book, then run the script. The script opens, processes, and closes the book documents one by one.

      (Hope you don’t feel I’ve hijacked your script, Keith.)

      (function () {
      
        function removeBackgrounds (myDoc) {
          var myRectangleArray = myDoc.rectangles.everyItem().getElements();
          for (var j = myRectangleArray.length-1; j >= 0; j--) {
            var myItem = myRectangleArray[j];
            if ((myItem.graphics.length > 0 ) || (myItem.images.length > 0)) {
              myItem.fillColor = myDoc.swatches.item("None");
            }
          }
        }
        
        var chapters = app.activeBook.bookContents.everyItem().getElements();
        for (var i = 0; i < chapters.length; i++){
          app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract;
          app.open (chapters[i].fullName);
          app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
          removeBackgrounds (app.activeDocument);
          app.activeDocument.close (SaveOptions.YES);
        }
      
      }());
    • #81208
      Paul White
      Member

      tested it and runs a treat.

      nice one.

    • #89106

      Hi,

      Can not get this script to work, can someone please upload the script file here rather than just the text.
      Script for the single document not the book version.

      Sorry this is the first time looking at scripts, and I wonder if I am doing something wrong.

      Cheers
      Phil

    • #89371
      Brian Pifer
      Participant

      Hi Philip, Paul’s code from Jan. 28 should work. If you need help with how to set up a script with a snippet you found here, check out this post: https://creativepro.com/how-to-install-a-script-in-indesign-that-you-found-in-a-forum-or-blog-post.php

Viewing 15 reply threads
  • You must be logged in to reply to this topic.
Forum Ads