Copy link to clipboard
Copied
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-...
https://community.adobe.com/t5/indesign-discussions/is-it-possible-to-run-a-script-automatically-whe...
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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...
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Oh, thanks, now I see. The problem is indeed in the fact that it gets called from the event handler... Okay, I hope the issue will be fixed soon. Could you also tell me, please, if there a way I could track the progress of the problem, so I would know when (and if) it's fixed?
Copy link to clipboard
Copied
I found an existing bug report for this bug, lodged in March 2023. It has 2 votes only (one mine!). Please add your vote too.
To keep informed, vote on the bug—you will receive updates via email if/when an employee comments, such as to tell us that it is fixed in beta version etc. Don't expect a quick response though. For your current project, you will definitely need to pursue a workaround I think.
- Mark
Copy link to clipboard
Copied
Wow... Yeah, I see that if I even should wait for any response - it'll be anything but quick. But okay, thanks for the info, I voted too! I hope our contribution may speed it up at least a little bit.
Copy link to clipboard
Copied
Hey @reliable_developer6014, I should have thought of this before but I just remembered that I have previously ported a QRCode generator to ExtendScript for use in Illustrator. It wouldn't take much for me to write an adapter for it to work in Indesign. Would that help?
Here are examples for comparison between Indesign's native QRCode and my script:
Let me know if that would be useful and I'll post a link to the github repo.
- Mark
Copy link to clipboard
Copied
Hey, if QR codes can be generated in an event handler using your script - I'd be really grateful if you could share it with me. Thank you very much for the help.
Copy link to clipboard
Copied
That's great! You can download the QRCode.js script file from my GitHub repo and try it out using the code below, which is basically your code, plus my QRCode implementation. You need to put the below script *and* QRCode.js in the same folder (in Indesign's scripts panel folder).
One thing though:I was dismayed at how slow it runs when it's called from the event handler—much slower than a direct call. So if you have many of these calls to do it might be tedious. There is definitely some optimization to be done, so if you wanted to try any modifications please go for it. Maybe writing a raw svg and placing that, rather than actually drawing it in Indesign? It is much faster in Illustrator.
Anyway, let me know what you think.
- Mark
//@targetengine "session"
//@include 'QRCode.js'
/**
* @file start-update-qrcodes.js
*
* Example implementation of QRCode.js to workaround a bug where
* Indesign's native QRCode creation function crash when called
* from an event handler.
*
* @author m1b (modified from sample code by reliable_developer6014)
* @version 2024-12-16
* @discussion https://community.adobe.com/t5/indesign-discussions/function-createplaintextqrcode-crashes-adobe-indesign-without-any-reason-provided/m-p/15036628
*/
(function () {
if ('undefined' === typeof QRCode)
return alert("QRCode.js failed to load. Ensure it is in the same folder as this script.");
var myEventListener = app.eventListeners.item("mAfSave");
if (myEventListener.isValid) {
myEventListener.remove();
return alert('QR Code updater OFF.')
}
myEventListener = app.addEventListener("afterSave", updateQR);
myEventListener.name = "mAfSave";
alert('QR Code updater ON.');
function updateQR() {
if (!app.documents.length)
return;
var doc = app.activeDocument;
var label = 'QRCode',
counter = 0;
for (var i = 0; i < doc.pages.length; i++) {
var page = doc.pages[i];
var qrCodeFrame = getPageItemByLabel(page, label);
if (qrCodeFrame != undefined && qrCodeFrame.isValid) {
// draw a QRCode, replacing the existing
var newQRCode = new QRCode({
text: 'test' + Math.floor(Math.random() * 10), // I've added a random number just for the demo!
doc: doc,
container: qrCodeFrame.parent,
position: [qrCodeFrame.geometricBounds[1], qrCodeFrame.geometricBounds[0]], // [left, top], measured in points
width: qrCodeFrame.geometricBounds[3] - qrCodeFrame.geometricBounds[1], // measured in points
margin: 4, // outer border, measured in "squares"
joinSquares: true,
// correctLevel: QRErrorCorrectLevel.M, // this matches Indesign's native level
// typeNumber: 2, // don't change this unless you know what you're doing
// darkColor: makeColor([0, 0, 0]), // custom dark color
// lightColor: makeColor([255, 255, 255]), // customer light color
});
if (newQRCode) {
newQRCode.label = label;
qrCodeFrame.remove();
counter++;
}
}
}
alert('Updated ' + counter + ' QR Codes.');
};
function getPageItemByLabel(page, label) {
for (var i = 0; i < page.pageItems.length; i++) {
if (page.pageItems[i].label === label) {
return page.pageItems[i];
}
}
};
})();
Copy link to clipboard
Copied
Thanks a lot, I don't know when I'm going to try your solution, but I will reply again to either say it worked fine or maybe ask any more questions.
Copy link to clipboard
Copied
Hi, it's me again, I finally had time to test it out. Thank you very much! It works perfectly fine. And I think you were kinda exaggerating that it takes too long to generate QR code. For my purposes it takes a couple of seconds at worst, which is totally fine, because now my colleagues won't need to keep the script in mind and activate it manually. So it does worth it.
Copy link to clipboard
Copied
Well, that's great to hear it worked out well. 🙂
Find more inspiration, events, and resources on the new Adobe Community
Explore Now