# Mark Scripting
# Example Script Marks
Below you'll find some example scripts and snippets to help you understand how scripting works, as well as help with some common scripting needs
# Find a product's custom property
This script looks through all items in a layout, and finds products in the layout. Then it finds the products location and index property, and draws the index on top of the product. This script is delivered as is to help share scripting concepts and is not designed to be used in production.
/*
* Tilia Labs Inc.
* Copyright (c) 2012-*, All Rights Reserved
* Script author: david@tilialabs.com
*
* Product Index Mark - 2021-02-05
*
* This script looks through all items in a layout, and finds
* products in the layout. Then it finds the products location
* and index property, and draws the index on top of the product.
* This script is delivered as is to help share scripting
* concepts and is not designed to be used in production.
*
*/
// Create array to store layout items
var items = [];
function run(context){
// find items on the layout
var products = findProducts(context,context.root)
for (var i=0;i<items.length;i++) {
var product = items[i]
// Set product name
var productName = product.name
// Find product location on sheet
var productX = product.globalRect.left;
var productY = product.globalRect.bottom;
// Find product width and height
var productHeight = getProductHeight(context, productName)
var productWidth = getProductWidth(context, productName)
// Find product property
var productIndex = getProductIndex(context, productName);
// Create a drawable painter from the mark context
var painter = new Painter(context.data);
// Create color to be used in mark
var ink = new Color(100, 0, 100, 0);
// Clear the pen so there will be no stroke
painter.clearPen();
painter.clearBrush();
// Set brush (fill) color
painter.setBrush(ink);
// Set font and style
var titleFont = new Font("Times New Roman");
titleFont.style = "Bold";
// Set title text and font
var title = new Text(productIndex);
title.font = titleFont;
title.size = 72;
// Set title location
title.point = new Point(
productX,
productY
);
painter.draw(title);
}
return true;
}
function findProducts(context,item) {
// if item is a product, store it in array
if (item.type === Type.Product) {
context.log("Found***")
items.push(item)
}
// continue to look through additional items and loop
// back in to see if there are more product items
var children = item.children;
for (var index = 0; index < children.size(); index++) {
var child = findProducts(children.get(index));
if (child !== undefined) {
return;
}
}
return;
}
function getProductHeight(context, productName) {
// take product item and see if its name matches
// a job product. If so, return height
for(var i=0;i<context.job.products.size();i++) {
var product = context.job.products.get(i);
if (product.name == productName) {
return product.height;
}
}
}
function getProductWidth(context, productName) {
// take product item and see if its name matches
// a job product. If so, return width
for(var i=0;i<context.job.products.size();i++) {
var product = context.job.products.get(i);
if (product.name == productName) {
return product.width;
}
}
}
function getProductIndex(context, productName) {
// take product item and see if its name matches
// a job product. If so, return product index.
// this function can be modified to get any product
// property
for(var i=0;i<context.job.products.size();i++) {
var product = context.job.products.get(i);
if (product.name == productName) {
return product.index;
}
}
}