IT Community - Software Programming, Web Development and Technical Support

Photoshop scripts for centering a layer content!!

This is a discussion on Photoshop scripts for centering a layer content!! within the Web Design Help forums, part of the Web Development category; As the name implies, this script simply centers the active layer within the document, both vertically and horizontally. 1. copy ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Web Design Help

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-13-2007, 12:22 AM
PixelNameVj PixelNameVj is offline
D-Web Trainee
 
Join Date: Jul 2007
Posts: 28
PixelNameVj is on a distinguished road
Exclamation Photoshop scripts for centering a layer content!!

As the name implies, this script simply centers the active layer within the document, both vertically and horizontally.

1. copy the below code in a notepad.
2. For PS CS2 and CS3, save the file with Center Layer 1-0-2.jsx extension. If you are using PS CS then Center Layer 1-0-2.js
3. Copy the file into your "Adobe Photoshop CS#/Presets/Scripts" folder.
4. Restart Photoshop, the script should appear in the File » Scripts menu.


// Author: Trevor Morris (trevor@morris-photographics.com)
// ================================================== ==========================
// Installation:
// 1. Place script in 'C:\Program Files\Adobe\Adobe Photoshop CS2\Presets\Scripts\'
// 2. Restart Photoshop
// 3. Choose File > Scripts > Center Layer
// ================================================== ==========================

// enable double-clicking from Mac Finder or Windows Explorer
#target photoshop // this command only works in Photoshop CS2 and higher

// bring application forward for double-click events
app.bringToFront();

// main function
function centerLayer() {
// ensure that there is at least one document open
if (!documents.length) {
alert('There are no documents open.');
return; // quit
}
// check for background layer, groups, etc.
if (checkInitialConditions()) {
// declare global variables
var docRef = app.activeDocument;
var layerRef = docRef.activeLayer;

// set document resolution to 72ppi (required due to a script bug in Photoshop)
var docRes = activeDocument.resolution;
docRef.resizeImage(undefined, undefined, 72, ResampleMethod.NONE);

var allLocked = activeDocument.activeLayer.allLocked; // remember layer's lock state
var posLocked = activeDocument.activeLayer.positionLocked; // remember layer's lock state

activeDocument.activeLayer.allLocked = false; // unlock layer
activeDocument.activeLayer.positionLocked = false; // unlock layer position

var docWidth = docRef.width;
var docHeight = docRef.height;

// NOTE: both width and height will be off by +2px for shape layers
var layerWidth = layerRef.bounds[2] - layerRef.bounds[0];
var layerHeight = layerRef.bounds[3] - layerRef.bounds[1];

var deltaX = (docWidth - layerWidth)/2 - layerRef.bounds[0];
var deltaY = (docHeight - layerHeight)/2 - layerRef.bounds[1];

layerRef.translate(deltaX, deltaY); // centers the active layer

// restore document's original resolution (required due to a bug)
docRef.resizeImage(undefined, undefined, docRes, ResampleMethod.NONE);

// restore layer's original lock state
layerRef.allLocked = allLocked;
layerRef.positionLocked = posLocked;
}
}

// check background layer, groups, etc.
function checkInitialConditions() {
// check if Background layer is selected
if (activeDocument.activeLayer.isBackgroundLayer) {
alert('The Background layer cannot be centered.\nPlease select another layer and try again.');
return false; // quit
}
// check to see if the current layer contains any artwork
else if (activeDocument.activeLayer.bounds[0] == activeDocument.activeLayer.bounds[2]) {
alert('The current layer contains no artwork.\nPlease select another layer and try again.');
return false; // quit
}
// check to see if a group is currently selected
else if (activeDocument.activeLayer.typename == "LayerSet" && !confirm('Are you sure you want to center the entire group?')) {
return false; // quit
}
// if all above conditions are met, proceed with main function
else {
return true; // continue
}
}

centerLayer();


================================================== ===
__________________
Cheers!!!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Keyboard short cuts of PhotoShop CS2 PixelNameVj Web Design Help 1 07-30-2007 10:34 AM
Photoshop Background's Magix PixelNameVj Web Design Help 0 07-13-2007 12:27 AM
Follow Up:: Introduction to Scripts and Actions Events Manager in Photoshop CS2 PixelNameVj Web Design Help 0 07-11-2007 03:26 AM
Introduction to Scripts and Actions Events Manager in Photoshop CS2 PixelNameVj Web Design Help 0 07-11-2007 03:17 AM
GUI Scripts and VU scripts vigneshgets Software Testing 0 05-22-2007 08:22 AM


All times are GMT -7. The time now is 08:55 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0