Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

EAN-14 creation script

Contributor ,
Aug 06, 2022 Aug 06, 2022

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 ?

Capture d’écran 2022-08-06 à 14.49.19.jpgexpand image

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();
}

 

TOPICS
Scripting
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 08, 2022 Aug 08, 2022

Hi @Fred.L, do you realise that the function I wrote for you earlier—getEANCheckDigit—does all the same calculations your script is doing, somewhat laboriously, with all the slicing? You may as well use it. Can you see how it works? It converts the number into a string and splits it into characters, converts each character to a number and does the digitNumStep1 calculations on it.

- Mark

 

You can probably us it like this:

getEANCheckDigit(myKeyData.text + myEan13Data.text);

 

Translate
Contributor ,
Aug 06, 2022 Aug 06, 2022

Following my own post,

let's talk about what can be improved, or added.

 

- Using palette rather than panels to be able to keep working on the ID document while generating EAN-14 codes. (will certainly give it a go)

- Adding static texts that inform about the lenght of the ISBN/ EAN-14 codes, to avoid pasting incorrect numbers.

- Adding a function OnClick to the checkBox (Digital key) that regenerates the EAN-14 according to the new Digital Key number.

- Last and not the least, Clean the bloody code with proper scripting ^^ (guys, feel free to propose something, I believe that's the best way for us to understand what's wrong and to improve) 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 06, 2022 Aug 06, 2022

I'm not a guru in JS but I'm pretty sure you can use text as an array to avoid using slice().

 

And am I missing something but what is the purpose of your scrip? ISBN / EAN codes are provided by GS1 organisation - so they already have all the digits - including the last one.

 

No one should be "generating" codes - they should be used as provided. 

 

It would have more sense if your script was creating actual barcode - instead of linking to external websites. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Aug 06, 2022 Aug 06, 2022

> I'm not a guru in JS but I'm pretty sure you can use text as an array to avoid using slice().

Possibly. I thought about creating a loop for this part.  I'll have a look. Thanks for the advice.

 

> And am I missing something but what is the purpose of your scrip? ISBN / EAN 

Partly true. ISBN codes are provided. But not EAN-14.

These are meant to be used by our Distributor and they are put on the boxes, not on the items within the boxes. Every single wholesaler has his own way to deal with parcels. Bar codes are following Companies' own process. That's why I need to generate myself a EAN-14 code out of the ISBN code + other datas I get from my Distributor's chart to match their process.

 

> It would have more sense if your script was creating actual barcode - instead of linking to external websites.

It's more complicated than that. I would tend to agree if it was only about creating ISBN barcode. And among other scripts, Marc Autret has done what I see the best in that matter. No possible debate about that. (https://www.indiscripts.com/category/projects/BookBarcode).

The other reason is trying to be consistent with the process, and the human ressources we've got. As far as I know, there is no cheap way (I saw plugins that do that but cost an Adobe monthly plan, just for a bloody barcode) to get a built-in EAN-14 code. Meaning, I have to rely on the websites provided. Besides, we have outsourced layout specialists. It would be a pain in the a## for all of us if we had to deal with different patterns. This script helps me being consistant from ISBN to EAN-14. A single tool for everyone. We prefer not to let different types of generated barcodes coexist.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 08, 2022 Aug 08, 2022

Hi @Fred.L, do you realise that the function I wrote for you earlier—getEANCheckDigit—does all the same calculations your script is doing, somewhat laboriously, with all the slicing? You may as well use it. Can you see how it works? It converts the number into a string and splits it into characters, converts each character to a number and does the digitNumStep1 calculations on it.

- Mark

 

You can probably us it like this:

getEANCheckDigit(myKeyData.text + myEan13Data.text);

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Aug 09, 2022 Aug 09, 2022
LATEST

I wish I had realised it ^^

Sounds like a nicer way to do it. I'll definitely have a look.

Thank you very much for the time spent to look at it. This is exactly the kind of help I need, something that helps me see things through.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines