// Everything was written in English. // Contac me (Knight) if there's any problem //Autocenter v2.02 //March 20, 2013 //This script centers automatically all the text layers that are inside bubbles in photoshop. //How to install: //Place this script somewhere in your computer. //Open photoshop and create an action that launchs this script. //Assign a Function Key to this action (for example F12). //How to use: //You open the psd and you press F12 (if you chose this Function Key). //It will automatically center all the text layers inside bubbles. #target photoshop function magicWand(x,y,t,a,c,s) { var desc = new ActionDescriptor(); var ref = new ActionReference(); ref.putProperty( charIDToTypeID('Chnl'), charIDToTypeID('fsel') ); desc.putReference( charIDToTypeID('null'), ref ); var positionDesc = new ActionDescriptor(); positionDesc.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Rlt'), x ); positionDesc.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Rlt'), y ); desc.putObject( charIDToTypeID('T '), charIDToTypeID('Pnt '), positionDesc ); desc.putInteger( charIDToTypeID('Tlrn'), t); desc.putBoolean( charIDToTypeID('Mrgd'), s ); if(!c) desc.putBoolean( charIDToTypeID( "Cntg" ), false ); desc.putBoolean( charIDToTypeID('AntA'), a ); executeAction( charIDToTypeID('setd'), desc, DialogModes.NO ); }; function layerPixels2Selection() { var desc = new ActionDescriptor(); var ref = new ActionReference(); ref.putProperty( charIDToTypeID( "Chnl" ), charIDToTypeID( "fsel" ) ); desc.putReference( charIDToTypeID( "null" ), ref ); var ref1 = new ActionReference(); ref1.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Chnl" ), charIDToTypeID( "Trsp" ) ); desc.putReference( charIDToTypeID( "T " ), ref1 ); executeAction( charIDToTypeID( "setd" ), desc, DialogModes.NO ); } function duplifill() { app.activeDocument.artLayers.add() app.activeDocument.selection.fill( whiteColor, ColorBlendMode.VIVIDLIGHT,100, false) } function findPV(h) { for (var i = 0; i <= 255; i++ ) { if (h[i]) { return i; } } return 0; } function boundshape(xt1,yt1,xt2,yt2) { var shapeRef = [[xt1,yt1],[xt1,yt2],[xt2,yt2],[xt2,yt1]] docRef.selection.select(shapeRef) docRef.selection.copy(); var id30 = charIDToTypeID( "CpTL" ); executeAction( id30, undefined, DialogModes.NO ); var tempBounds = docRef.activeLayer.bounds; app.activeDocument.activeLayer.remove() return tempBounds } function getlayercolor(xt3,yt3) { var MyState0 = app.activeDocument.activeHistoryState; app.activeDocument.flatten(); magicWand(xt3,yt3,0,false,true,true) var maxred = findPV(docRef.channels["Red"].histogram); var maxgreen = findPV(docRef.channels["Green"].histogram); var maxblue = findPV(docRef.channels["Blue"].histogram); app.activeDocument.activeHistoryState = MyState0; return Math.min(maxred,maxgreen,maxblue) } function createbubblelayer(xt2,yt2) { magicWand(xt2,yt2, 0 , false, true, true) var selRef = app.activeDocument.selection selRef.contract( 4 ) selRef.expand( 3 ) duplifill() magicWand(xt2,yt2, 0 , false, true, true) duplifill() app.activeDocument.artLayers[1].remove() app.activeDocument.activeLayer=app.activeDocument.artLayers[0] } function textlayerstate() { var emptytext=false layerPixels2Selection(); try { docRef.selection.copy(); } catch(e) { emptytext=true } return emptytext } function bubblebound(xt3,yt3) { magicWand(xt3,yt3,0,false,true,true) var selRef = app.activeDocument.selection duplifill() var tempBounds = docRef.activeLayer.bounds; app.activeDocument.activeLayer.remove() return tempBounds } function findareacenter(yt4,qsizey1) { var theBounds1 = boundshape(0,yt4,px0-1,yt4+1); var theBounds2 = boundshape(0,yt4-qsizey1,px0-1,yt4+1-qsizey1); var theBounds3 = boundshape(0,yt4+qsizey1,px0-1,yt4+1+qsizey1); var posxtext2= (theBounds1[0]+theBounds1[2]+theBounds2[0]+theBounds2[2]+theBounds3[0]+theBounds3[2])/6 var theBounds1 = boundshape(posxtext2,0,posxtext2+1,py0-1); var qsizex1= (theBounds1[2] - theBounds1[0])/4; var theBounds2 = boundshape(posxtext2-qsizex1,0,posxtext2+1-qsizex1,py0-1); var theBounds3 = boundshape(posxtext2+qsizex1,0,posxtext2+1+qsizex1,py0-1); return [Math.round(posxtext2),Math.round((theBounds1[1]+theBounds1[3]+theBounds2[1]+theBounds2[3]+theBounds3[1]+theBounds3[3])/6)] } function findnewpos() { var result = [0,0,0,0,activeDocument.activeLayer.textItem.position[0],activeDocument.activeLayer.textItem.position[1]] if (textlayerstate()==false) { docRef.paste(); var theBounds = docRef.activeLayer.bounds; result[2] = Math.round((theBounds[0]+ theBounds[2])/2); result[3] = Math.round((theBounds[1]+ theBounds[3])/2); docRef.activeLayer.remove(); app.activeDocument.activeLayer.visible = false var theBounds = bubblebound(result[2],result[3]); if (!((theBounds[2] - theBounds[0]<11)||(theBounds[3] - theBounds[1]<11)||(getlayercolor(result[2],result[3])<250))) { createbubblelayer(result[2],result[3]) var theBounds = docRef.activeLayer.bounds; var theWidth = theBounds[2] - theBounds[0]; var theHeight = theBounds[3] - theBounds[1]; if ((theWidth0) { for(j=0;j0)||(layerindnewpos[totalcount2][1]>0)) { var new1posx=layerindnewpos[totalcount2][4]-layerindnewpos[totalcount2][2]+layerindnewpos[totalcount2][0] var new1posy=layerindnewpos[totalcount2][5]-layerindnewpos[totalcount2][3]+layerindnewpos[totalcount2][1] LG.layers[i].textItem.position = Array(new1posx, new1posy); } totalcount2=totalcount2 + 1 }; }; } function movelayerposall(){ movelayerpos(app.activeDocument) jmax=activeDocument.layerSets.length if (jmax>0) { for(j=0;j0) { for(i=0;i0) { layerindnewpos[i][0]=0 layerindnewpos[i][1]=0 } } movelayerposall() } if (acmode=="DocumentMode.GRAYSCALE") app.activeDocument.changeMode(ChangeMode.GRAYSCALE); }