Copy link to clipboard
Copied
I had a script that worked in photoshop 23 where I could copy and paste guides from one document to another, but it is not working in the new version of Photoshop. Does anyone have an updated script or know how to fix this problem?
Sorry, it’s reported on line 53. Here are lines 52 and 53 for context from the previous code posted:
var desc1 = app.getCustomOptions('7a301ec0-afde-11e1-afa6-0800200c9a66');
var layerGuides = eval(desc1.getString(0));
Copy link to clipboard
Copied
I can confirm that it is now broken.
Copy link to clipboard
Copied
Here is the script which works to copy guides in 2021:
/*
https://www.reflections-ibs.com/blog/articles/how-to-copy-guides-from-one-photoshop-document-to-another
https://www.reflections-ibs.com/media/1241/guides-copy.jsx
*/
#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;
setGuides();
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(0, 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(0));
}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 );
};
Here is the script to paste:
/*
https://www.reflections-ibs.com/blog/articles/how-to-copy-guides-from-one-photoshop-document-to-another
https://www.reflections-ibs.com/media/1242/guides-paste.jsx
*/
#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;
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(0, 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(0));
}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 );
};
While this version is combined, run the script with SHIFT held down to copy, run the same script again on the new doc to paste:
/*
https://gist.github.com/codyjlandstrom/5fcb2779b59eb97d862311a2b2815791
https://youtu.be/ERHbxtyOJEg
NOTE: Hold down the shift key when copying guides
*/
#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();
app.beep();
} 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(0, 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(0));
} 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);
};
Copy link to clipboard
Copied
I am a little out of my depth...
I believe that the script is working correctly to copy the guide coordinates, as I get the expected result when I alert the following block of code from:
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+=','
}
}
To:
for( var g = 0; g < guides.length; g++ ){
if(guides[g].direction.toString() == 'Direction.HORIZONTAL'){
alert(gH+=(parseInt(guides[g].coordinate.value)));
gH+=',';
}else{
alert(gV+=(parseInt(guides[g].coordinate.value)));
gV+=','
}
}
Copy link to clipboard
Copied
This block of code looks like an Action Manager method to set a Photoshop environment "session" variable:
gH = gH.replace(/,$/, '');
gV = gV.replace(/,$/, '');
currentGuides = 'Layer Guides' + "¬" + gH + "¬" + gV;
var desc2 = new ActionDescriptor();
desc2.putString(0, currentGuides.toSource());
app.putCustomOptions('7a301ec0-afde-11e1-afa6-0800200c9a66', desc2, true);
While this looks like the corresponding code to get the variable:
var desc1 = app.getCustomOptions('7a301ec0-afde-11e1-afa6-0800200c9a66');
var layerGuides = eval(desc1.getString(0));
Putting an alert into the try/catch for getting the session variable results in an error "The requested property does not exist"...
So the issue appears to be that v2024 has changed how AM environment variables are handled!
EDIT:
I am having issues replacing the AM code with DOM code for $.setenv() and $.getenv() to verify if this fixes the issue. Can somebody with more knowledge and/or experience please step in and help? Environment variables have always been problematic for me, I usually write to/read from a text file as a "bullet-proof" alternative.
Copy link to clipboard
Copied
Putting an alert into the try/catch for getting the session variable results in an error "The requested property does not exist"...
By @Stephen_A_Marsh
Copy link to clipboard
Copied
@r-bin – thank you for taking an interest.
Error: The requested property does not exist. 53
Sorry, it’s reported on line 53. Here are lines 52 and 53 for context from the previous code posted:
var desc1 = app.getCustomOptions('7a301ec0-afde-11e1-afa6-0800200c9a66');
var layerGuides = eval(desc1.getString(0));
This is why I believe the AM code for an environment variable is the issue in 2024. Not knowing about AM environment variables, I thought that just using a DOM environment variable would be a quick fix, but I have only had intermittent success using them.
There is another error generated:
Error 21: undefined is not an object.
Line: 56
-> var ar1 = layerGuides.toString().split('¬');
Copy link to clipboard
Copied
I have code someplace that uses env variables.
Copy link to clipboard
Copied
That's great, I have always had spotty results with them, writing to a text file and reading from the text file has always worked better for me, in addition to being persistent between Photoshop sessions.
Copy link to clipboard
Copied
Sorry, it’s reported on line 53. Here are lines 52 and 53 for context from the previous code posted:
var desc1 = app.getCustomOptions('7a301ec0-afde-11e1-afa6-0800200c9a66');
var layerGuides = eval(desc1.getString(0));
Copy link to clipboard
Copied
@r-bin = genius – changing 0 to 1 worked!
desc2.putString(1, currentGuides.toSource());
var layerGuides = eval(desc1.getString(1));
Copy link to clipboard
Copied
The following script from @c.pfaffenbichler works in 2024:
And another option here, which uses a text file rather than an environment variable:
https://www.reddit.com/r/graphic_design/comments/32ub8x/tip_copying_guides_from_one_psd_to_another/
http://www.mediafire.com/download/zz7j7t3fl824p31/ImportandExportGuides.zip
Copy link to clipboard
Copied
This is a sample prototype, you would need to loop through the document.guides object and parse each entry. Document.guides is an object, not an array, so starting index is one, not zero. Run with your source file in the foreground (active) and your target file in the background. This works in PS 25.7.
#target photoshop
try{
var doc = app.activeDocument;
var g = doc.guides;
var d = g[1].direction;
var c = g[1].coordinate;
var doc2 = app.documents[1];
app.activeDocument = doc2;
doc2.guides.add(d, c);
}
catch(e){
Window.alert(e + e.line);
}
Copy link to clipboard
Copied
@Lumigraphics – Thanks for the reply, can you help with swapping AM environment variables into DOM? I believe that this is the issue with the original code that I posted earlier.
Copy link to clipboard
Copied
I can confirm that it's not working with the newest update, version 2024.
Copy link to clipboard
Copied
Not working on from the 2-3 updates back. Photoshop 2024. It's now 4th of May 2024.
This is the script I was using and it's NOT working. It's the same as you posted as working.
COPY:
#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;
setGuides();
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(0, 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(0));
}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 );
};
PASTE:
#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;
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(0, 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(0));
}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 );
};
Copy link to clipboard
Copied
Thanks for confirming, other scripted options still work (see my previous post), so at least there are alternatives.
Copy link to clipboard
Copied
@Stephen_A_Marsh Can you point me at one that works with the recent PS2024 version? I'll test it right away.
Copy link to clipboard
Copied
They both work in 2024 as they are not leveraging an environment variable. This one copies the guides to all open docs in a single step:
While this one is the equivalent of the broken one:
Export Guides - Photoshop.jsx
if (documents.length == 0) {
alert("There are no documents open.");
}
else {
var docRef = activeDocument;
if ($.os.search(/windows/i) != -1) {
fileLineFeed = "Windows"
} else {
fileLineFeed = "Macintosh"
}
fileOut = new File("~/Desktop/Guides.txt")
fileOut.lineFeed = fileLineFeed
fileOut.open("w", "TEXT", "????")
for(var i=0; i<docRef.guides.length; ++i) {
fileOut.write(docRef.guides[i].direction + "," + docRef.guides[i].coordinate + "\n")
}
fileOut.close();
alert("Guides exported!");
}
Import Guides - Photoshop.jsx
if (documents.length == 0) {
alert("There are no documents open.");
}
else {
var docRef = activeDocument;
if ($.os.search(/windows/i) != -1) {
fileLineFeed = "Windows"
} else {
fileLineFeed = "Macintosh"
}
fileOut = new File("~/Desktop/Guides.txt")
fileOut.lineFeed = fileLineFeed
fileOut.open("r", "TEXT", "????")
var line = "";
while((line = fileOut.readln()) !== "")
{
var dir = line.split(",")[0];
var amt = line.split(",")[1].split(" ")[0];
var unit = line.split(",")[1].split(" ")[1];
var cmd = "docRef.guides.add("+dir+",UnitValue("+amt+","+"\""+unit+"\"))";
eval(cmd);
}
fileOut.close();
alert("Guides imported!");
}
fileOut.remove(); // Remove the text file from the desktop
I added an extra line at the end to delete the text file after importing, simply remove that last line if you wish to import the same guides to multiple documents.
The two separate scripts could be combined into a single script where the SHIFT or OPT/ALT key is used to copy.
Copy link to clipboard
Copied
Thanks, It works in the newest PS2024!
I just deleted alert cause that's one click more then I need. And I renemaed Export script to COPY and Import script to PASTE, it kinda makes more sence to me.
Thank you very much!
Copy link to clipboard
Copied
Thanks, It works in the newest PS2024!
I just deleted alert cause that's one click more then I need.
By @filips97187540
You're welcome!
I usually use an app.beep(); if I want quick feedback that something worked without having to OK an alert.
P.S. @r-bin has provided info on fixing the original script and it now works for me in 2024 and when tested in 2021 it still worked with the same fix!
Copy link to clipboard
Copied
Can you post the fixed old script?
Copy link to clipboard
Copied
I'm on a mobile phone at the moment, so no. But it's as I previously wrote, just find the following two bits in the script changing 0 to 1:
desc2.putString(1, currentGuides.toSource());
var layerGuides = eval(desc1.getString(1));
Copy link to clipboard
Copied
Works here to, BUT it added one more guide at the top of the format, horizontal one.... Not sure why. I will rather stick with the new script for PS2024. Just to be sure.