I had access on my phone, here is some of my code hopefully it helps you:
var moduleMain=function(ticket) {
for each(var inputFile in ticket.getInputFiles()) {
const fileWidth = inputFile.getProperty('MetaDataExtractor/NormalizedPageFormat/Width', PropertyType.FLOAT).value;
const fileHeight = inputFile.getProperty('MetaDataExtractor/NormalizedPageFormat/Height', PropertyType.FLOAT).value;
ticket.job.setFloatProperty('SUMMA', 'barWidth', fileWidth); // Set to the Width of the File
ticket.job.setFloatProperty('SUMMA', 'barHeight', 8.50394); // Force to 3mm
ticket.job.setFloatProperty('SUMMA', 'barHeightPlus', 35.4375); // Force to 3mm
ticket.job.setFloatProperty('SUMMA', 'pageWidth', fileWidth + 72); // Set to the Width of the File + Mark Spacing
ticket.job.setFloatProperty('SUMMA', 'pageHeight', fileHeight + 99.225); // Set the Height of the File + Barcode Spacing
const barcodes = generateUniqueCodes();
const stdCode = barcodes[0];
const rotCode = barcodes[1];
const cutFilename = `${barcodes[2]}.pdf`;
const markPositions = calculatePrintMarks(fileWidth);
const railMarkSpacing = calculateRailMarkPositions(fileHeight);
let markFile = ticket.storage.rootResourceFolder.createSub('PrintMark/SUMMA_Bar.pdf');
var outMarkFile = ticket.getOutputRunList('BAR').copyFile(markFile, FileState.OK);
outMarkFile.setIntegerProperty('SUMMA', 'stdCode', stdCode);
outMarkFile.setIntegerProperty('SUMMA', 'rotCode', rotCode);
outMarkFile.setStringProperty('SUMMA', 'cutFilename', cutFilename);
ticket.job.setIntegerProperty('SUMMA', 'stdCode', stdCode);
ticket.job.setIntegerProperty('SUMMA', 'rotCode', rotCode);
ticket.job.setStringProperty('SUMMA', 'cutFilename', cutFilename);
ticket.job.setStringProperty('SUMMA', 'summaMark1', markPositions[0]);
ticket.job.setFloatProperty('SUMMA', 'summaMark1Position', markPositions[1]);
ticket.job.setStringProperty('SUMMA', 'summaMark2', markPositions[2]);
ticket.job.setFloatProperty('SUMMA', 'summaMark2Position', markPositions[3]);
ticket.job.setFloatProperty('SUMMA', 'railMarkSpacing', railMarkSpacing);
ticket.OKOutputRunList.addFile(inputFile);
}
};
function generateUniqueCodes() {
// Get the current date
const now = new Date();
// Format month, day, and year to be 2 digits each using leftPad
const month = leftPad(now.getMonth() + 1, 2); // +1 because months are 0-indexed
const day = leftPad(now.getDate(), 2);
const year = leftPad(now.getFullYear(), 4).slice(-2); // Get last 2 digits of year
// Generate 4 random digits
const randomDigits = Math.floor(1000 + Math.random() * 9000); // Ensures a 4-digit number
// Concatenate everything and add the final digits
const baseCode = month + day + year + randomDigits;
const codeWithZero = baseCode + '0';
const codeWithFive = baseCode + '5';
//ticket.log.info(`STD Code: ${codeWithZero}, ROT Code: ${codeWithFive}`);
return [codeWithZero, codeWithFive, baseCode];
}
function leftPad(number, targetLength) {
/*
* Adds leading zeros to a number until it reaches the specified length.
* @param {number|string} number - The number to pad.
* @param {number} targetLength - The desired length of the output string.
* @returns {string} The padded number as a string.
*/
// Convert the number to a string
var output = number + '';
// Add zeros to the front of the string until the desired length is reached
while (output.length < targetLength) {
output = '0' + output;
}
// Return the padded string
return output;
}
/**
* Calculates the center position(s) based on the width following the specified rules.
* @param {number} width - The width in millimeters.
* @returns {Array} An array of center position(s).
*/
function calculatePrintMarks(width) {
let summaMark1 = 'DrillHole None.pdf';
let summaMark2 = 'DrillHole None.pdf';
let summaMark1Position = 0;
let summaMark2Position = 0;
if (width <= 1417.5) {
// Do nothing for widths of 500mm or less
return [summaMark1, summaMark1Position, summaMark2, summaMark2Position];
} else if (width <= 2126.25) {
// Find center for widths between 500mm and 750mm
summaMark1 = 'MC_Regmarks.pdf';
summaMark1Position = (width / 2) - 18;
return [summaMark1, summaMark1Position, summaMark2, summaMark2Position];
} else {
// For widths greater than 750mm, divide into three segments
// and find the two division points
var segmentLength = width / 3;
summaMark1 = 'MC_Regmarks.pdf';
summaMark1Position = segmentLength - 18;
summaMark2 = 'MC_Regmarks.pdf';
summaMark2Position = (segmentLength * 2) - 36;
return [summaMark1, summaMark1Position, summaMark2, summaMark2Position];
}
}
/**
* Calculates the ideal spacing of marks along the side of the page, trying to be as close to the standard spacing as possible.
* @param {number} fileHeight - Total length of the image in DTP points.
* @returns {number} Ideal spacing in DTP points.
*/
function calculateRailMarkPositions(fileHeight) {
var maxBedSize = 8640; // Maximum bed working size in DTP (120in)
var minSpacing = 432; // Minimum spacing in DTP (6in)
var standardSpacing = 1417.5; // Standard spacing in DTP (19.685in, 500mm)
// Calculate the number of full beds and the length of the final bed
var fullBeds = Math.floor(fileHeight / maxBedSize);
var finalBedLength = fileHeight % maxBedSize;
// If there is only one bed, use standard spacing
if (fullBeds === 0 || (fullBeds === 1 && finalBedLength === 0)) {
return standardSpacing;
}
// Start with the standard spacing
var idealSpacing = standardSpacing;
// Adjust the spacing for the final bed if necessary
if (finalBedLength > 0 && finalBedLength < standardSpacing * 2) {
while (idealSpacing > finalBedLength / 2 && idealSpacing > minSpacing) {
idealSpacing /= 2; // Halve the spacing until it fits within the final bed
}
}
// Ensure the spacing is within the acceptable range
return Math.min(Math.max(idealSpacing, minSpacing), standardSpacing);
}