Skip to main content
Participating Frequently
December 13, 2024
Question

Function 'createPlainTextQRCode' crashes Adobe InDesign without any reason provided

  • December 13, 2024
  • 1 reply
  • 875 views

Hi! I'm trying to write a function, that could update QR code on a page after save. I used code examples from these two discussions in order to solve my problem: 
https://community.adobe.com/t5/indesign-discussions/is-it-possible-to-generate-qr-codes-in-indesign-using-current-page-number/m-p/13749888
https://community.adobe.com/t5/indesign-discussions/is-it-possible-to-run-a-script-automatically-when-a-document-is-saved/m-p/7257249


In the end I got the following code:

#target indesign
#targetengine "session"

var myEventListener = app.eventListeners.item("mAfSave");

if (!myEventListener.isValid) {
    myEventListener = app.addEventListener("afterSave", updateQR);
    myEventListener.name = "mAfSave";
}

function updateQR() {
  if (!app.documents.length) {
      return;
  }
   var doc = app.activeDocument;
   var label = 'QRCode';
  for (var i = 0; i < doc.pages.length; i++) {
     var page = doc.pages[i];
      var qrCodeFrame = getPageItemByLabel(page, label);
      if (qrCodeFrame != undefined && qrCodeFrame.isValid) {
         qrCodeFrame.allGraphics[0].createPlainTextQRCode('test');
      }
    }
    alert('QRCode updated!');
}

function getPageItemByLabel(page, label) {
    for (var i = 0; i < page.pageItems.length; i++) {
        if (page.pageItems[i].label === label) {
            return page.pageItems[i];
        }
    }
}

It works fine, but the app crashes on this line: "qrCodeFrame.allGraphics[0].createPlainTextQRCode('test');"
The QR code isn't undefined and 'allGraphis[0]' has a type 'object EPS'. What could be the reason for this behaviour? Windows event logger tells that the problem is in InDesign's 'EPS PAGE ITEM.RPLN' module, but it doesn't say much. I'd be much obliged for any help with this problem. Maybe there are other ways to update QR code in a document.

This topic has been closed for replies.

1 reply

m1b
Community Expert
Community Expert
December 13, 2024

Hi @reliable_developer6014 , I'm thinking you might have found a bug.

 

I tried your code and it crashed Indesign for me, too. I'm running MacOS so the crash is cross-platform. A couple of quick tests points to the QR Code generation being the point of failure, as you noted.

 

This is the test code I used, based on yours:

//@targetengine "session"

var myEventListener = app.eventListeners.item("mAfSave");

if (myEventListener.isValid)
    myEventListener.remove();

myEventListener = app.addEventListener("afterSave", testQRCodeCrash);
myEventListener.name = "mAfSave";

function testQRCodeCrash() {

    if (!app.documents.length) return;

    app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;

    var doc = app.activeDocument;

    // these cause crash:
    // doc.rectangles.add({ geometricBounds: [0, 0, 40, 40] }).createPlainTextQRCode('test');
    // doc.rectangles.add({ geometricBounds: [0, 0, 100, 100] }).createTextMsgQRCode('1234567890', 'test');
    // doc.rectangles.add({ geometricBounds: [0, 0, 100, 100] }).createEmailQRCode('foo@bar.com', 'test', 'test');
    // doc.rectangles.add({ geometricBounds: [0, 0, 100, 100] }).createHyperlinkQRCode('https://foo-bar.com/test');

    // this doesn't crash:
    // doc.textFrames.add({ geometricBounds: [0, 0, 40, 40], contents: 'Hello world!' });

    alert('Test completed');

};

 

Could you please lodge a bug report? If not, let me know and me (or someone else here) will do it. Post a link to the bug report so we can all vote on it.

- Mark

Participating Frequently
December 13, 2024

Thanks for the answer. I believe you as a Community Expert could write a better bug report than me, so I'd be grateful if you did so. But anyway, does it mean that currently there's no way to generate a QR code via script? I thought if it was the case, then Adobe would already know about that...

m1b
Community Expert
Community Expert
December 13, 2024

No problem. I'll do the bug report.

 

> does it mean that currently there's no way to generate a QR code via script

 

No! sorry I wasn't clear—the bug appears when called from the AfterSave event handler. Making a QRCode in a normal script is no problem:

doc.rectangles.add({ geometricBounds: [0, 0, 40, 40] }).createPlainTextQRCode('test')

 

So if you take out the event handling part at the start, and call the function explicitly, your code works fine, updating the "QRCode" labelled rectangle containing EPS QRCode.

- Mark