Skip to content

Instantly share code, notes, and snippets.

@mihai-vlc
Last active August 29, 2015 14:02
Show Gist options
  • Save mihai-vlc/cd8b5401a6edf0a9cc1e to your computer and use it in GitHub Desktop.
Save mihai-vlc/cd8b5401a6edf0a9cc1e to your computer and use it in GitHub Desktop.
/**
* Script shows position and size of selection or layer if selection is not set
* @author: Mihai Ionut Vilcu
* Jul 2014
*/
/******** CONFIGURATION **********/
var ID = '#home-tab-item-'; // the css selector
var PATH = '~/Desktop/test.css';
var L_OFFSET = 1; // left offset, ribbon_left
var T_OFFSET = 3013; // top offset, ribbon_top
/******** END CONFIGURATION **********/
// Enables double-click launching from the Mac Finder or Windows Explorer
#target photoshop
init();
function init() {
// Bring application forward
app.bringToFront();
// Define pixels as unit of measurement
var defaultRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
try {
var info = getSizeInfo();
} catch (e) {
alert(e);
//restore units
preferences.rulerUnits = defaultRulerUnits;
return;
}
// ask the user what type of output he wants
var type = getType();
if(type === 5) {
buildRibbon(info);
} else {
// format the info
var css = formatStyles(info, type);
// add the generated info in the clipboard
copyTextToClipboard(css);
}
//restore units
preferences.rulerUnits = defaultRulerUnits;
}
/**
* Formates the data based on the type
* @param {Object} obj The object with the selection data
* @param {integer} type The type of the format.
* @return {string} The generated css
*/
function formatStyles(obj, type) {
var o;
switch(type) {
case 1:
o = {
height: obj.height,
width: obj.width
};
break;
case 2:
o = {
'background-position': obj['background-position']
};
break;
case 3:
o = {
left: obj.left,
top: obj.top
};
break;
case 4:
o = {
'background-position': obj['background-position'],
width: obj.width,
height: obj.height
};
break;
default:
o = obj;
break;
}
return makeCss(o);
}
/**
* Builds the css for a ribbon element
* @param {Object} obj The object with the selection data
* @return {[type]} [description]
*/
function buildRibbon(obj) {
var ind = parseInt(prompt("Index:", 1), 10)
// append the index
ID += ind;
var f = new File(PATH);
f.open('a+');
f.write(makeRibbonElem(obj));
f.close();
}
/**
* Prompts the user for the type
* @return {integer} The type inserted
*/
function getType() {
var promptText = [
'Select type:',
'0 = all ' +
'1 = w and h ' +
'2 = bg-pos ' +
'3 = top and left ',
'4 = bg-pos, w and h ' +
'5 = ribbon'
].join('\n');
return parseInt(prompt(promptText, 0), 10);
}
/**
* Returns the requested data about the currect selection
* @return {Object} And object containing the generated info
*/
function getSizeInfo() {
var selectionRef;
try {
var tmp = app.activeDocument.selection.bounds.length;
selectionRef = app.activeDocument.selection;
} catch(err) {
selectionRef = app.activeDocument.activeLayer;
}
var xtop = selectionRef.bounds[0].value;
var ytop = selectionRef.bounds[1].value;
var xbot = selectionRef.bounds[2].value;
var ybot = selectionRef.bounds[3].value;
return {
'background-position': val(xtop, ytop),
'height': val(ybot - ytop),
'left': val(xtop),
'top': val(ytop),
'width': val(xbot - xtop)
};
}
/**
* Generates a ribbon element based on the obj info
* @param {Object} obj Object containing the info about the selection
* @return {string} The generated ribbon element.
*/
function makeRibbonElem(obj) {
var css = [
'',
ID + ' {',
makeCss({
height: obj.height,
left: val(parseInt(obj.left, 10) - L_OFFSET),
top: val(parseInt(obj.top, 10) - T_OFFSET),
width: obj.width
}, 1) +
'}',
ID + ':hover {',
makeCss({
'background-position': obj['background-position']
}, 1) +
'}',
''
].join('\n');
return css;
}
/**
* Formats the number value of a css property
* by adding px at the end.
* @param {number} a The value of the first property
* @param {number} b The value of the sencond property for bgpos
* @return {string | number} Returs the modified string.
*/
function val(a, b) {
if(typeof b != 'undefined') {
// background position
return (a ? -a + 'px' : 0) + ' ' + (b ? -b + 'px' : 0);
}
return a ? a + 'px' : 0;
}
/**
* Converts an object to the css form
* @param {Object} obj The object to be converted
* @param {bool} indent If true it will indent the css rules with 4 spaces
* @return {string} The generated css
*/
function makeCss(obj, indent) {
var css = '';
indent = indent ? ' ' : '';
for( p in obj ) {
if(obj.hasOwnProperty(p) && (obj[p] !== false)) {
css += indent + p + ': ' + obj[p] + ';\n';
}
}
return css;
}
/**
* Adds the text to the clipboard
* @param {string} text The text to be added to the clipboard
*/
function copyTextToClipboard(text) {
var folderForTempFiles = Folder.temp.fsName;
// create a new textfile and put the text into it
var clipTxtFile = new File(folderForTempFiles + "/ClipBoard.txt");
clipTxtFile.open('w');
clipTxtFile.write(text);
clipTxtFile.close();
// use the clip.exe to copy the contents of the textfile to the windows clipboard
var clipBatFile = new File(folderForTempFiles + "/ClipBoard.bat");
clipBatFile.open('w');
clipBatFile.writeln("cat \"" + folderForTempFiles + "/ClipBoard.txt\"|clip");
clipBatFile.close();
clipBatFile.execute();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment