Skip to main content
Perthdave74
Known Participant
April 25, 2023
Answered

Is it possible to generate QR codes in InDesign using Current Page Number?

  • April 25, 2023
  • 1 reply
  • 5725 views

Rather than generate numbers in Excel and export them as CSV files, I wanted to see if I could remove a step of the process for creating QR codes out of InDesign.

 

I was hoping to re-number pages and add prefixes to the page numbers, so for example, I could have "AAA080501". I would setup a text page on the master page with a marker for 'Current Page Number'. I would then add 999 pages to my document so that the page numbers would span from AAA080501 to AAA08150. But how can I get the in-built QR code generator to read from the Current Page Number marker on the master page to generate a code?

 

So I tried another method: using the inbuilt QR code generator under the Object menu, but there is no option to place in a special chatacter marker for current page number.

 

I asked ChatGPT if it was possible, and it gave me comprehensive instructions on how to do it, but it added fictional options which aren't available in InDesign, although the AI does understand what it is that I'm trying to achieve.

 

Is it possible?

 

<Title renamed by moderator>

This topic has been closed for replies.
Correct answer Robert at ID-Tasker

Hi Mark, when the QR generates, the size of the QR code is smaller than the box I've placed it in - I was anticipating that the code would fill the frame. Can this be changed?

Also, I've just been told I need to append a variable (in this case, a P) after the last digit of the code, which of course needs to encoded into the QR code. Is this possible?

 

Thank you once again


Modify line:

 

qrCodeFrame.allGraphics[0].createPlainTextQRCode(page.name);

 

 

By adding:

 

qrCodeFrame.allGraphics[0].createPlainTextQRCode(page.name + 'P' );

 

 

Or do you mean contents of the variable P - not the letter 'P' ?

 

Then you would have to declare it, set some value and:

 

var P = 'my extra code';
qrCodeFrame.allGraphics[0].createPlainTextQRCode(page.name + P);

 

 

1 reply

m1b
Community Expert
Community Expert
April 25, 2023

Hi @Perthdave74, here is one approach:

1. Set up a document (see example document attached) with a section suffix of AAA, and numbering style of 01,02,03, and page starting at 80150. Make sure that the prefix is included in the name (checkbox).

2. Put a QRCode (generate a plain-text QRCode in Indesign and use it as a dummy)

3. Put a Script Label on the QRCode's frame (see Window > Utility > Script Label menu). The label must match the label in the script—I used "PageNumberQRCode", but you can change it.

4. Put this QRCode on every page where you want it.

5. Run the script I wrote for you (see below).

- Mark

 

/**
 * Updates any *labelled* QRCode graphic with the page name.
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/generating-qr-codes-in-indesign-using-current-page-number-is-it-possible/m-p/13749888
 */
function main() {

    var doc = app.activeDocument,
        label = 'PageNumberQRCode',
        suffix = 'P',
        counter = 0,
        suffix = prompt('What is the suffix?', suffix);

    if (suffix == null)
        return;

    for (var i = 0; i < doc.pages.length; i++) {

        var page = doc.pages[i],
            qrCodeFrame = getPageItemByLabel(page, label);

        if (
            qrCodeFrame != undefined
            && qrCodeFrame.isValid
        ) {
            qrCodeFrame.allGraphics[0].createPlainTextQRCode(page.name + suffix);
            counter++;
        }

    }

    alert('Updated ' + counter + ' QR Codes.');

};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Update QR-Codes');


/**
 * Returns page item on page by matching label.
 * @param {Page} page
 * @param {String} label
 * @param {PageItem}
 */
function getPageItemByLabel(page, label) {

    for (var i = 0; i < page.pageItems.length; i++)
        if (page.pageItems[i].label === label)
            return page.pageItems[i];

};

 

Edit 2023-04-25: added prompt for a suffix. Comment out the line starting "suffix = ..." if you don't want it.

Perthdave74
Known Participant
April 25, 2023

Thank you so much, Mark, for taking the time to do this for me. Everything looks very promising. The only thing I can't get to work is the script.

 

I've cut and pasted the script (except the opening notes above 'function'), pasted it into a text document, saved it with a .jsx file extension, dropped into the Scripts folder under the 'User' folder -- and InDesign sees it -- but it's greyed out and says 'the file is not executable by any supported script language'. 

 

Could it be the encoding (defaulted at UTF-8) that Notepad (in Windows) applies to the resultant save?

m1b
Community Expert
Community Expert
April 25, 2023

Okay, sorry I should have mentioned. You have to save the script as plain text. The encoding shouldn't matter in this case unless you are changing the label to a unicode string. Also you could try downloading the attached script file. Remove the .txt extension. - Mark