Copy link to clipboard
Copied
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>
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 ch
...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);
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Hi Mark, thanks for that, but on downloading the file, Edge (the browser) sees the .txt extension in my downloads list, but in the Windows environment, it only shows as .js so I can't see what I need to delete.
Feeling really dumb here as I'm a dyed in the wool Mac users, so I'm feeling somewhat like a fish out of water.
Somehow I managed to get InDesign to see the script, but when I run it, I get the following message (attached).
Copy link to clipboard
Copied
The error you got was definitely because somewhere along the line something messed with the text. The error had split an != operator into ! = which are two separate operators. Yeah, so it sounds like the OS was hiding the file extension. I'm a dyed in the wool Mac user, too, so I don't know where to see the extension. I think look for the Properties info of the file in the file explorer.
Copy link to clipboard
Copied
I pasted the script from this thread, into the Edit Script function in InDesign and it now runs! Must've been a formatting issue which I must've introduced somewhere. Ok cool, let me test this out and if it works, I'll Paypal you enough for a beer!
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
Hi Robert, thanks for that suggestion. Sorry I wasn't clear - was just after the letter P with no further value to it so I spliced the first interation into the script and it worked great.
Copy link to clipboard
Copied
Hi @Perthdave74, the script shouldn't alter the size of the QRCode, so you make it the size and cropping you want before running the script. Is the QRCode's size changing for you?
As for the variable, eg "P", @Robert at ID-Tasker's answer is right. But we'd need to know where you are getting this from and does it apply to every page? If it applies to the whole document, then adding + "P" as Robert suggests will be fine. If different pages need different letter codes, then we must devise a system for telling the script when to use which code.
- Mark
Copy link to clipboard
Copied
Another idea: the script can ask the user each time which suffix to add? See the updated script above. - Mark
Copy link to clipboard
Copied
Hi Mark, I made the frame fit contents proportionally and executing the script made things work.
I've since sorted the +P provision with Robert so that it appends correctly to the page number label.
A funny thing has happened since this all came together. I timed how long it took for me to: a) duplicate 999 pages, b) run the script and then c) export the PDFs, and it's like double the time it takes for me to do it the original way. Here I was thinking I'd be saving crucial time working smarter, but somehow it's a false economy to do it all within InDesign, which is a shame but at the same time I've learnt a few things.
But I thankful for the time you've both taken to get the result I wanted anyway, so if you both want to message me with your Paypal addresses, I'll happily shout you both funds for a beverage of your choice as a token of my appreciation.
Cheers, David.
Copy link to clipboard
Copied
Thanks for the offer but it doesn't work that way 😉 all advice posted here is free of charge 😉
Copy link to clipboard
Copied
I know it doesn't but I also like to show appreciation as you've given up time to help out. The offer is still there and happy to oblige!
Copy link to clipboard
Copied
Just mark some answers as Correct 😉
Copy link to clipboard
Copied
What was the original way? Generating QR codes outside and import as pictures?
It could be speeded up in two ways:
Copy link to clipboard
Copied
Using an Excel document which I was generating the numbers from, exporting them as a CSV - 5,000 at a time.
Linking the CSV file in InDesign and exporting the PDF.
Doing it this recent way involved delays in a) duplicating the pages -- I couldn't insert new pages based off the master page as it include the new pages when the script was run, b) running the script, and c) exporting the PDFs. Also when I was in another InDesign document and selected the document with the QRs in, there was a significant delay in regaining control of the document (Windows equivalent of 'spinning beachball') which I'd never seen before whilst using InDesign.
Copy link to clipboard
Copied
*** as it DIDN'T include the new pages when the script was run.
-- is there a way to edit posts? lol I always see mistakes after I've posted
Copy link to clipboard
Copied
All good, I'm happy to help and leaving the scripts here can be helpful for others too. So you are doing a Data Merge and making the QR Codes that way? It sort of makes sense that it'd be faster. The scripting API is a lot slower than the native functions. Anyway, all the best!
- Mark
Copy link to clipboard
Copied
So how did you get QR codes in the "original way" ?
Or am I missing something else? 😉
Copy link to clipboard
Copied
My original way was via an Excel document which I was generating the numbers from, exporting them as a CSV - 5,000 at a time. Then linking the CSV file in InDesign and exporting the PDF.
Copy link to clipboard
Copied
And where the QR codes came from?
Copy link to clipboard
Copied
@Robert at ID-Tasker, Data Merge can generate QR codes. Here's an article about it.
Copy link to clipboard
Copied
Then it can be done from the start in Excel? No need for script - just formula in Excel?