Skip to content

Instantly share code, notes, and snippets.

@codyjlandstrom
Last active June 12, 2024 00:17
Show Gist options
  • Select an option

  • Save codyjlandstrom/5fcb2779b59eb97d862311a2b2815791 to your computer and use it in GitHub Desktop.

Select an option

Save codyjlandstrom/5fcb2779b59eb97d862311a2b2815791 to your computer and use it in GitHub Desktop.
Script to copy and paste guides in Photoshop
#target photoshop
main();
function main(){
if(Number(app.version.match(/\d+/)) <12) return;
if(!documents.length) return;
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
if(ScriptUI.environment.keyboardState.shiftKey ){
setGuides();
}else{
displayGuides();
}
app.preferences.rulerUnits = startRulerUnits;
function setGuides(){
var guides = app.activeDocument.guides;
if(guides.length == 0){
alert("No guides exist");
return;
}
var gH = '';
var gV = '';
for( var g = 0; g < guides.length; g++ ){
if(guides[g].direction.toString() == 'Direction.HORIZONTAL'){
gH+=(parseInt(guides[g].coordinate.value));
gH+=',';
}else{
gV+=(parseInt(guides[g].coordinate.value));
gV+=','
}
}
gH=gH.replace(/,$/,'');
gV=gV.replace(/,$/,'');
currentGuides = 'Layer Guides' + "¬" + gH + "¬" + gV;
var desc2 = new ActionDescriptor();
desc2.putString(1, currentGuides.toSource());
app.putCustomOptions('7a301ec0-afde-11e1-afa6-0800200c9a66', desc2, true );
}
function displayGuides(){
try{
var desc1 = app.getCustomOptions('7a301ec0-afde-11e1-afa6-0800200c9a66');
var layerGuides = eval(desc1.getString(1));
}catch(e){return;}
clearGuides();
var ar1 = layerGuides.toString().split('¬');
var Hor = ar1[1].toString().split(',');
var Ver = ar1[2].toString().split(',');
for(var H in Hor){
activeDocument.guides.add(Direction.HORIZONTAL,new UnitValue(Number(Hor[H]),'px'));
}
for(var V in Ver){
activeDocument.guides.add(Direction.VERTICAL,new UnitValue(Number(Ver[V]),'px'));
}
}
}
function clearGuides() {
var id556 = charIDToTypeID( "Dlt " );
var desc102 = new ActionDescriptor();
var id557 = charIDToTypeID( "null" );
var ref70 = new ActionReference();
var id558 = charIDToTypeID( "Gd " );
var id559 = charIDToTypeID( "Ordn" );
var id560 = charIDToTypeID( "Al " );
ref70.putEnumerated( id558, id559, id560 );
desc102.putReference( id557, ref70 );
executeAction( id556, desc102, DialogModes.NO );
};
@kiwisa1986
Copy link

Thanks for the script. The thing is I am only getting Copy option so I can't paste them to another image :)

@MarshySwamp
Copy link

MarshySwamp commented May 2, 2024

I have both the copy and paste scripts, however Photoshop 2024 appears to have broken the scripts.

The copy appears to work, the guide co-ordinates are correctly reported in an alert.

No guides are pasted with the paste script though.

@MarshySwamp
Copy link

@MarshySwamp
Copy link

r-bin @ the Adobe forums has fixed the issue for 2024:

Changing the value from 0 to 1:

desc2.putString(1, currentGuides.toSource());

var layerGuides = eval(desc1.getString(1));

@codyjlandstrom
Copy link
Author

Thanks for posting the fix! I'll update the guide

@MarshySwamp
Copy link

Thanks for posting the fix! I'll update the guide

@codyjlandstrom – please check the Adobe forum link and test, I think that somebody reported unexpected behaviour with the code change. Action Manager isn't easy for me, so I hope that you can work out what's going on in later versions of Photoshop.

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