Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Photoshop script for adding measurements to your mockups, based on Pixel Measure v0.04 by Nikolaj Selvik (https://code.google.com/p/pixelmeasure/)
/*
Pixel Measure v0.04 - Photoshop script for adding pixel measurements to your mockups
Copyright (C) 2009 Nikolaj Selvik
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
//=====================================================
// ADDED: DIMENSION MEASUREMENT UNITS
// by @alingham
// Adding Measurement options into the script. Still produces the dimension line in pixels, but changes the label to be in
// Inches or Millimeters or Pixels (default) depending on the original unit of measurement set on the ruler in Photoshop.
//=====================================================
var originalUnit = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
app.displayDialogs = DialogModes.NO;
if(validateState())
{
app.activeDocument.suspendHistory("Pixel Measure", "createMeasure();");
}
function validateState()
{
if (app.documents.length == 0)
{
alert("No document open");
return false;
}
if(!hasSelection(app.activeDocument))
{
alert("Please make a selection to measure");
return false;
}
return true;
}
function createMeasure()
{
var docRef = app.activeDocument;
var selRef = docRef.selection;
var mainLayerSet;
// =======================================================
// Create "Pixel Measures" LayerSet if it doesn't already exist
// =======================================================
try
{
mainLayerSet = docRef.layerSets.getByName("Pixel Measures");
}
catch(error)
{
mainLayerSet = docRef.layerSets.add();
mainLayerSet.name = "Dimensions";
}
// =======================================================
// Create Measurement LayerSet
// =======================================================
var layerSetRef = mainLayerSet.layerSets.add()
var linesLayerRef = layerSetRef.artLayers.add();
// =======================================================
// Draw Lines
// =======================================================
var x1 = selRef.bounds[0].value;
var y1 = selRef.bounds[1].value;
var x2 = selRef.bounds[2].value;
var y2 = selRef.bounds[3].value;
selRef.deselect();
var width = x2 - x1;
var height = y2 - y1;
var horizontal = width > height;
if(horizontal)
{
drawLine(x1,y1,x1,y1+10);
drawLine(x2-1,y1,x2-1,y1+10);
drawLine(x1,y1+5,x2-1,y1+5);
}
else
{
drawLine(x1,y1,x1+10,y1);
drawLine(x1,y2-1,x1+10,y2-1);
drawLine(x1+5,y1,x1+5,y2-1);
}
// =======================================================
// Draw Text
// =======================================================
var textLayerRef = layerSetRef.artLayers.add();
textLayerRef.kind = LayerKind.TEXT;
var textItemRef = textLayerRef.textItem;
//Get Measurement from Ruler, and Resolution from Document
if (document === undefined) {
var document = app.activeDocument;
}
var resolution = document.resolution;
if (originalUnit == "Units.INCHES") {
var dimensionwidth = width/resolution;
var dimensionheight = height/resolution;
var unit = "in";
var dp = 2;
}
else if (originalUnit == "Units.MM") {
var dimensionwidth = (width/resolution)*25.4;
var dimensionheight = (height/resolution)*25.4;
var unit = "mm";
var dp = 0;
}
else if (originalUnit == "Units.CM") {
var dimensionwidth = (width/resolution)*2.54;
var dimensionheight = (height/resolution)*2.54;
var unit = "cm";
var dp = 1;
}
else {
//default pixels (PX)
var dimensionwidth = width;
var dimensionheight = height;
var unit = "px";
var dp = 0;
}
if(horizontal)
{
textItemRef.contents = dimensionwidth.toFixed(dp) + " " + unit;
textItemRef.justification = Justification.CENTER;
textItemRef.position = Array(Math.floor(x1 + (width/2)),y1+21);
}
else
{
textItemRef.contents = dimensionheight.toFixed(dp) + " " + unit;
textItemRef.position = Array(x1+15,Math.floor(y1 + 4 + (height/2)));
}
layerSetRef.name = textItemRef.contents;
textItemRef.color = app.foregroundColor;
textItemRef.font = "ArialMT";
// =======================================================
// Reset
// =======================================================
app.preferences.rulerUnits = originalUnit;
}
function drawLine(x1,y1,x2,y2)
{
var pointArray = new Array();
var pointA = new PathPointInfo();
pointA.kind = PointKind.CORNERPOINT;
pointA.anchor = Array(x1, y1);
pointA.leftDirection = pointA.anchor;
pointA.rightDirection = pointA.anchor;
pointArray.push(pointA);
var pointB = new PathPointInfo();
pointB.kind = PointKind.CORNERPOINT;
pointB.anchor = Array(x2, y2);
pointB.leftDirection = pointB.anchor;
pointB.rightDirection = pointB.anchor;
pointArray.push(pointB);
var line = new SubPathInfo();
line.operation = ShapeOperation.SHAPEXOR;
line.closed = false;
line.entireSubPath = pointArray;
var lineSubPathArray = new Array();
lineSubPathArray.push(line);
var linePath = app.activeDocument.pathItems.add("TempPath", lineSubPathArray);
linePath.strokePath(ToolType.PENCIL, false);
app.activeDocument.pathItems.removeAll();
}
function hasSelection(doc)
{
var res = false;
var as = doc.activeHistoryState;
doc.selection.deselect();
if (as != doc.activeHistoryState)
{
res = true;
doc.activeHistoryState = as;
}
return res;
}
Owner

//=====================================================
// ADDED: DIMENSION MEASUREMENT UNITS
// by @alingham
// Adding Measurement options into the script. Still produces the dimension line in pixels, but changes the label to be in
// Inches or Millimeters or Pixels (default) depending on the original unit of measurement set on the ruler in Photoshop.
//=====================================================

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment