EAN-14 creation script
Hello guys,
Let's share my very first real scripting project. Hope it can help someone, at least the shy fellas that think sripting is out of range. The code may not be brilliant yet, but it's a first step, anyway.
What does the script do ?
- It generates a EAN-14 code
- Provides 2 links to help you get to your usual web pages where you can paste the codes and get your jpg/png/AI pictures for your books/ boxes that are meant to be shipped and scanned.
What do you need to use it ?
- A EAN-13/ ISBN
- Distributor reference table for generating the Key digit (the 1st digit of the EAN-14)
What does it look like ?

What's the code ?
create_EAN14();
// Interface --------------------------------------------------------------------------------------------------------
function create_EAN14 () {
var w = new Window ('dialog {text: "EAN-14 creation", orientation: "column", alignChildren: "top"}');
// Main Panel --------------------------------------------------------------------------------------------------------
main = w.add ('panel {alignChildren: "fill", orientation: "column"}');
//ISBN panel
ean13 = main.add ('panel {text: "Packing data", alignChildren: "center"}');
var myEan13 = ean13.add ('group {_: StaticText {text: "EAN-13 / ISBN :"}}');
var myEan13Data = ean13.add ("edittext", [0, 0, 140, 25],"", {multiline:false});
myEan13Data.text = "";
myEan13Data.active = true;
// Quantity of Parcels Panel
var myColis = ean13.add ('group {_: StaticText {text: "Number of items in box"}}');
var myPackingData = myColis.add ("edittext", [0, 0, 40, 25],"", {multiline:false});
myPackingData.text = "";
myPackingData.active = true;
// Digital key - Yes/ No
var useCle = ean13.add ("checkbox", undefined, "Digital key ?");
useCle.value = true;
// Second Panel --------------------------------------------------------------------------------------------------------
bbeData = main.add ('panel {text: "", alignChildren: "center"}');
// Calculation of EAN14
var myCalc = bbeData.add ('group {orientation: "column"}');
var calcButton = myCalc.add ("button", undefined, "Calculation of EAN-14");
// Panel for Outsourced company
var bbeDataSub = main.add ('panel {alignChildren: "center", orientation: "column", text: "Distributor data"}');
// Key Digit
var bbe1 = bbeDataSub.add ('group {_: StaticText {text: "Digital key"}}');
var myKeyData = bbeDataSub.add ("edittext", [0, 0, 40, 25],"", {multiline:false});
myKeyData.text = "";
myKeyData.active = false;
//Check Digit
var bbe2 = bbeDataSub.add ('group {_: StaticText {text: "Check Digit"}}');
var myDigitData = bbeDataSub.add ("edittext", [0, 0, 40, 25],"", {multiline:false});
myDigitData.text = "";
myDigitData.active = false;
// EAN14
var ean14 = main.add ('panel {text: "Generated EAN-14", alignChildren: "fill"}');
var myEan = ean14.add ('group {_: StaticText {text: ""}}');
var myEan14Data = ean14.add ("edittext", [0, 0, 140, 25],"", {multiline:false});
myEan14Data.active = false;
// First link
var myIsbn = w.add ("group");
myIsbn.add ("statictext", undefined, "https://www.free-barcode-generator.net/isbn/");
myIsbn.graphics.foregroundColor = myIsbn.graphics.newPen(myIsbn.graphics.PenType.SOLID_COLOR, [0, 0, 1], 1);
// Second link
var myEan14Site = w.add ("group");
myEan14Site.add ("statictext", undefined, "https://www.free-barcode-generator.net/ean-14/");
myEan14Site.graphics.foregroundColor = myEan14Site.graphics.newPen(myEan14Site.graphics.PenType.SOLID_COLOR, [0, 0, 1], 1);
// OnClick for the links
myIsbn.addEventListener("click", JumpToLink);
myEan14Site.addEventListener("click", JumpToLink2);
// Usual Exit button
var buttons = w.add ('group {orientation: "column"}');
w.add ("button", undefined, "Fermer", {name: "cancel"});
// End of Interface --------------------------------------------------------------------------------------------------------
//Button OnClic Calc
calcButton.addEventListener("click", calculatate_EAN14);
function calculatate_EAN14 () {
// Update of ISBN (No Hyphens)
var myNewEAN13 = myEan13Data.text.replace(/-/g,"");
myEan13Data.text = myNewEAN13;
var myShortEan14 = myEan13Data.text.slice(0,-1);
// Company Custom Key Table -- Fill in the array according your Company logistic pattern
var my1Array = [1,2];
var my2Array = [3,4];
var my3Array = [5,6];
var my4Array = [7,8];
var my5Array = [9,10];
var my6Array = [11,12,];
var my7Array = [13,14];
var my8Array = [15,16];
// Function that returns the correct key digit and put in accurate Edittext
function include(arr, obj) {
for (var i = 0; i < arr.length; i++) {if (arr[i] == obj) return true; }
}
if (include(my1Array, myPackingData.text) == true) { myKeyData.text = "1"; } else
if (include(my2Array, myPackingData.text) == true) { myKeyData.text = "2"; } else
if (include(my3Array, myPackingData.text) == true) { myKeyData.text = "3"; } else
if (include(my4Array, myPackingData.text) == true) { myKeyData.text = "4"; } else
if (include(my5Array, myPackingData.text) == true) { myKeyData.text = "5"; } else
if (include(my6Array, myPackingData.text) == true) { myKeyData.text = "6"; } else
if (include(my7Array, myPackingData.text) == true) { myKeyData.text = "7"; } else
if (include(my8Array, myPackingData.text) == true) { myKeyData.text = "8"; } else myKeyData.text = "1";
if (useCle.value == false) {myKeyData.text = "9"};
// Start of creation of the Last digit
myDigitData.text = "";
// Assigning the values to prepare calculation
var d1 = +myKeyData.text;
var d2 = +myEan13Data.text.slice(0,1);
var d3 = +myEan13Data.text.slice(1,2);
var d4 = +myEan13Data.text.slice(2,3);
var d5 = +myEan13Data.text.slice(3,4);
var d6 = +myEan13Data.text.slice(4,5);
var d7 = +myEan13Data.text.slice(5,6);
var d8 = +myEan13Data.text.slice(6,7);
var d9 = +myEan13Data.text.slice(7,8);
var d10 = +myEan13Data.text.slice(8,9);
var d11 = +myEan13Data.text.slice(9,10);
var d12 = +myEan13Data.text.slice(10,11);
var d13 = +myEan13Data.text.slice(11,12);
// Calculation
var digitNumStep1 = d1*3 + d2 + d3*3 + d4 + d5*3 + d6 + d7*3 + d8 + d9*3 + d10 + d11*3 + d12 + d13*3;
// Last Digit (Kind help from Marc Autret)
myDigitData.text = 10-(digitNumStep1%10||10);
/* Last Digit (kind help from M1B)
function getEANCheckDigit(num) {
var str = String(num).split(''),
sum = 0,
d;
while (d = str.shift()) {
d = Number(d);
if (d !== d) // NaN
return;
multiplier = (str.length - 1) % 2 ? 3 : 1;
sum += d * multiplier;
}
return (Math.ceil(sum / 10) * 10) - sum;
}
myDigitData.text = getEANCheckDigit(digitNumStep1);
*/
// Creation of EAN14 and put it in accurate Edittext
myEan14Data.text = myKeyData.text + myShortEan14 + myDigitData.text;
}
w.show ();
}
// Function to create links for Edittexts
function JumpToLink() {
var scriptName = "isbn";
var tmpFileName = scriptName.replace(/\-\s\d+\.\d+$/, "").replace(/\s+/g, "_").toLowerCase();
var tmpFolder = new Folder(Folder.temp.absoluteURI + "/InDesignScripts");
tmpFolder.create();
var linkJumper = File(tmpFolder.absoluteURI + "/" + tmpFileName + "_temp.html");
if (!linkJumper.exists) {
linkJumper.open("w");
var linkBody = '<html><head><META HTTP-EQUIV=Refresh CONTENT="0; URL=https://www.free-barcode-generator.net/isbn/"></head><body> <p></body></html>'
linkJumper.write(linkBody);
linkJumper.close();
}
linkJumper.execute();
}
function JumpToLink2() {
var scriptName = "ean14";
var tmpFileName = scriptName.replace(/\-\s\d+\.\d+$/, "").replace(/\s+/g, "_").toLowerCase();
var tmpFolder = new Folder(Folder.temp.absoluteURI + "/InDesignScripts");
tmpFolder.create();
var linkJumper = File(tmpFolder.absoluteURI + "/" + tmpFileName + "_temp.html");
if (!linkJumper.exists) {
linkJumper.open("w");
var linkBody = '<html><head><META HTTP-EQUIV=Refresh CONTENT="0; URL=https://www.free-barcode-generator.net/ean-14/"></head><body> <p></body></html>'
linkJumper.write(linkBody);
linkJumper.close();
}
linkJumper.execute();
}
