Custom Exported File Names
By default Barcode software uses the content (data) of your barcode to name the exported files. This can be changed in settings, but then the rest of the tutorial will not apply.
The format of the generated file name depends on the barcode symbology, the addon presence and so on. The software tries its best to generate a proper name, but sometimes you might want to go beyond that.
Naming Exported Files with JavaScript
The Barcode application lets you provide a JavaScript code that will generate exported file names for your barcodes.
The script gets all the information about the barcode data and parameters and generates a file name that perfectly matches your needs. Although it requires some coding skills, you get a very flexible tool to name the files the way you need.
Making a Basic Script
Right–click any barcode in the list and click Reveal in Finder/Explorer in the popup menu. You will get into the barcodes folder, go to the parent folder from there. That’s the folder where you need to create the script file.
You will need a plain text editor. Editors like MS Word or Apple Pages will not work, as they don’t save plain text files. You will need a code editor like Visual Studio Code, Sublime Text editor, basic Notepad or something like that.
Once you get the text editor installed, create an empty file named “export.js” in that folder.
Open that file in the code editor and put the following code there:
function exportName(params) {
return "My File Name";
}
If the thing above looks scary — simply contact us and explain your needs, so we can make the script for you. This way it will take less time and efforts for both sides.
If the code looks simple, make sure you saved the file and try exporting a barcode from the Barcode application. You should get the exported file named “My File Name” in the saving panel. Note that the file extension is configured in settings and is not affected by the script.
If you don’t see the “My File Name” in the file saving window, make sure you have the Use last exported file name option is off in the Export section of the settings panel. If it is off, make sure you’ve made no errors in the script and saved the file properly. Try checking the log file using Help → Open System Folder from the main menu to access the log. If there’s an error in the script — it will be listed there.
If everything above doesn’t help — contact us, so we can fix it together.
Once it works, you are ready to start doing the real thing :)
Simple Naming Scripts
The exportName()
function has a single parameter named params
. This is a dictionary with some barcode–specific information you can use to name the files. Here’s what you can find there:
- name — default exported file name as if there is no script provided;
- barcode — the barcode name as you see it in the barcodes list;
- data — default “fixed” barcode data;
- use-data-for-naming — the flag that decides if data or barcode field must be used to name the file by default;
- symbology — barcode symbology, as you see it in the popup menu when pressing the plus button in the barcodes list;
- canvas-width, canvas-height — the exported canvas size in mm;
- barcode-width, barcode-height — the barcode size in mm without extra paddings, borders etc.
The last two parameters are optional and are only there if the barcode supports that. For instance you will get both for EAN–13 barcode, just the data-fixed for EAN–8 and none for QR–Code. The main feature of these two data items is that they have the check digit added and have the data properly padded with zeroes, if needed. The Barcode application lets you skip the check digit and adds it automatically to the barcode, but not when you save the file, so if you want to omit the check digit when entering the data, but have it added to the exported file — you need this parameter. Let’s give it a try.
Name Exported Files with Barcode Data
Here is the script to name barcodes after their data. This can be done without the script, but it is a good example of a basic script:
function exportName(params) {
return params["data"];
}
The script above simply returns the data field of the parameters dictionary which contains the fixed version of barcode data.
Custom Prefix and Height
Another common scenario is when you need to put the barcode height in the output file name. We’ll add a custom prefix on top of that to make it a little bit more complex. Here’s the code:
function exportName(params) {
const height_mm = params["barcode-height"]; // get the barcode height
const height_inches = height_mm / 25.4; // convert it to inches
const height_formatted = height_inches.toFixed(2); // round it up to 2 decimal places
const base = params["data"]; // prepare the base name
const prefix = "MyBarcode"; // that's a prefix
// assemble everything up
return prefix + "_" + base + "_" + height_formatted + " in";
}
Now if you try saving a barcode with this script, you should get something like “MyBarcode_1234567890_1.22in” as the suggested file name.
Accessing the Barcode Data
You can go deeper and access the raw barcode data from the script. The same data that you edit in the right panel of the barcode editor. Use the model variable to do so.
Checking the Light Margin Indicator
Let’s make a script that outputs the light margin indicator status to the exported file if the indicator is supported by the symbology.
function exportName(params) {
// prepare the base file name first
let res = params["data"];
// access the light margin parameter if it is there
const lm = model.lightMarginIndicator;
if (lm != undefined) {
const lm_enabled = lm.enabled;
if (lm_enabled) {
res += "_LM"; // add _LM if there is light margin symbol
} else {
res += "_NOLM"; // add _NOLM if there's no light margin symbol
}
}
// return the resulting name
return res;
}
The code checks if the barcode model supports the light margin indicator option and gets its status if it is there. Then it adds a suffix to the output name depending on its status.
Output Barcode Scaling Information
Another common scenario is when you need to have the barcode scale in the output file name. Here’s the code:
function exportName(params) {
// prepare the base file name first
const base = params["data"];
// get the scale information, no need to check it first, it is always there
const scale = model.scale;
let suffix = "";
if (scale == 1.0) {
suffix = "Normal";
} else if (scale < 1.0) {
suffix = "Small";
} else {
suffix = "Large";
}
// format the resulting file name and return it
return `{base}_{suffix}`;
}
The code gets the barcode scaling (1.0 means 100%) and adds a suffix depending on the scale value.
Adding the Customer Information
Let’s add a customer name as a prefix to the exported barcode name.
function exportName(params) {
// prepare the base file name first
let res = params["data"];
// try getting the customer name
const customer = model.notes ? model.notes.customer : "";
// adding the cutomer name as a prefix, if it is specified
if (customer != "") {
res = `{customer}_{res}`;
}
return res;
}
The code checks if there is a notes field in the barcode and if it has a non–empty customer name. If it is — the name is used as a prefix to the exported file name.
Need Something Else?
There’s much more barcode options that you might need to configure the exported file name. Contact us with your requirements, so we can help making a script and adding it here, so everyone else can use it, too.
More Barcode Tutorials
Installation
- Installation — how to install Barcode generator;
- License Activation — how to activate Barcode software with a license key.
Basics
- User Interface — Barcode user interface explained in details;
- Barcode Management — adding, renaming, cloning and deleting barcodes;
- Importing Barcodes — importing barcode images;
- Custom Texts — adding custom text elements to barcodes;
- Marks Panel — configuring border, margins and canvas of barcodes;
- Bar Width Reduction — adjusting barcodes to compensate for ink spread;
- Quiet Zone — making sure the barcodes can be scanned well.
EAN Barcodes
- Making EAN–13 Barcodes — standard point–of–sale barcodes;
- EAN–13 Calculator — how to compute EAN–13 check digits.
UPC Barcodes
- Making UPC–A Barcodes — learn to make UPC–A barcodes;
- Making UPC–E Barcodes — how to create UPC–E barcodes;
- UPC–A Calculator — compute check digits of UPC–A barcodes.
NDC Barcodes
- NDC Barcodes — learn about NDC barcodes and how to make them;
- NDC Barcode Check Digit Calculator — how to compute NDC check digits.
QR Code
- QR Code — how to make and configure the popular 2D barcodes;
- QR Code with Image — adding custom artwork to QR Code.
Other Barcodes
- ISBN Barcode Generator — how to make ISBN barcodes;
- BC412 Barcode — making barcodes for silicon wafers identification;
Export
- Transparent Barcodes — making barcodes with transparent background;
- PNG Barcodes — exporting barcodes to PNG format;
- Vector Barcodes — exporting barcodes to vector formats;
- Export.js — defining custom file names for exported barcodes.
Automation
- Batch Processing — how to batch–convert text data to barcodes;
- Command Line Processing — command line barcode generation.
Other
- Self–Drawing Barcode — drawing QR codes in browser with just an SVG file;
- Fake Barcodes — need a random barcode as a placeholder?