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

Help with editing script

  • This topic has 4 replies, 2 voices, and was last updated 10 years ago by Anonymous.

Return to Member Forum

  • Author
    Posts
    • #80053
      Anonymous
      Inactive

      Hello!
      I’m pretty new to working with InDesign scripts (just working my way up from coding VBA for Microsoft office) and I’m still doing a lot of learning. I have the below script that I found and altered slightly, but I’m not sure how to make it do what I’d like.

      Currently the script randomizes the font and color of the characters in a text frame. What I’d like to do, if it’s possible, is make it so that it only uses the first font listed – “font[0] = “GF3Medium” one time and then randomly selects from font 1 + (the total number of fonts will vary) for the rest of the characters.

      Any help anyone can provide would be greatly appreciated. Thanks in advance!

      // Check selection
      if (app.documents.length == 0 ||
      app.selection.length == 0)
      {
      alert (‘Please make sure to have some text frames selected first!’)
      exit();
      }
      var font = [];
      var swatch = [];
      font[0] = “GF3Medium”;
      font[1] = “GF2Medium”;
      //font[2] = “GFMedium”;
      // font[3] = “etc. etc.”

      swatch[0] = “C=0 M=0 Y=0 K=100”;
      swatch[1] = “C=0 M=0 Y=0 K=98”;
      swatch[2] = “C=0 M=0 Y=0 K=96”;
      swatch[3] = “C=0 M=0 Y=0 K=95”;
      swatch[4] = “Fade”;
      // Check fonts
      for (i=0; i<font.length; i++)
      {
      try {
      x = app.fonts.item(font[i]).fullName;
      } catch (_)
      {
      alert (‘The font name “‘+font[i]+'” cannot be found — please check spelling etc.’);
      }
      }
      // Check swatches
      for (i=0; i<swatch.length; i++)
      {
      try {
      x = app.activeDocument.swatches.item(swatch[i]).name;
      } catch (_)
      {
      alert (‘The swatch name “‘+swatch[i]+'” cannot be found — please check spelling etc.’);
      }
      }
      // Apply random colors and fonts!
      for (sel=0; sel<app.selection.length; sel++)
      {
      // Is this a text frame? (It better be!)
      if (app.selection[sel] instanceof TextFrame)
      {
      // Ah right. Go over each of its text lines ..
      for (ch=0; ch<app.selection[sel].characters.length; ch++)
      {
      // .. and apply random font and swatch
      app.selection[sel].characters[ch].appliedFont = font[Math.floor(Math.random()*font.length)];
      app.selection[sel].characters[ch].fillColor = swatch[Math.floor(Math.random()*swatch.length)];
      }
      }
      }

    • #80087
      Mike Dean
      Member

      Do you mean you want the very first character in the frame to use font[0] and swatch[0]? If that’s the case, you can adjust the for loop at the end of the script to start at the second character. Just change ch=0 to ch=1:
      for (ch = 1; ch < app.selection[sel].characters.length; ch++) {

      Then right above that loop, add a couple of lines to style the first character:
      app.selection[sel].characters[0].appliedFont = font[0];
      app.selection[sel].characters[0].fillColor = swatch[0];

      And finally, to prevent the first font/swatch from being reused, you can add a couple of statements to remove them from the array:
      font = font.slice(1);
      swatch = swatch.slice(1);

      Out of curiosity, what’s the utility of a script like this? I’m just having trouble thinking of how I would use this (other than to play an evil joke on someone… “what happened to all the text in your InDesign file??”).

    • #80088
      Anonymous
      Inactive

      Hi Mike,
      Thanks for the response!
      I was actually hoping to still use font 0 randomly, so that the character it is used on will change each time it is run. So I’d like it to randomly assign font 0 to a character and then exclude it from the random assignment after it’s used.. does that make sense?

      What I’m doing is creating a handwritten campaign. We have it functioning with this script so that it will randomly assign one of 4 or 5 handwritten fonts. The reason I want font 0 to end after one character application is because we are thinking of developing an error font to make the writing seem even more realistic. If the same errored letter appears more than once, though, then it kind of defeats the purpose.

    • #80090
      Mike Dean
      Member

      I see. Thanks for the explanation!

      I think this might be fairly simple (at least, simpler than I was originally making it). I was trying to think of was of being able to mark fonts as “used” and have the script skip them. But I think an even easier answer is to use script you originally posted with just two minor tweaks.

      First, somewhere around the font array declaration (var font = [];), add a variable with the font that you only want to use once:
      var uniqueFont = “Comic Sans MSBold”;

      Second, at the very end of the script (after all the curly braces), add:
      app.selection[0].characters[Math.floor(Math.random() * app.selection[0].characters.length)].appliedFont = uniqueFont;

      The idea here is to let the original script work as intended. It will randomly apply a font to every character. Then, when that’s finished, it will apply your uniqueFont to a single, random character. That should keep things nice and simple!

    • #80091
      Anonymous
      Inactive

      Thanks Mike! I’m excited to try this and see how it works.

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