Skip to main content
Participant
May 1, 2024
Answered

Copy and Paste Guides - Photoshop 2024

  • May 1, 2024
  • 7 replies
  • 6875 views

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? 

Correct answer r-bin

@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('¬');

 


quote

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));

 

 

 


By @Stephen Marsh

 

 
Could you use a key value other than 0 when using the putString and getString function?
It seems that the getCustomOptions function still returns the found descriptor and does not generate an error.
Many built-in scripts use the putCustomOptions and getCustomOptions functions.
If the reason is their incorrect operation, then they may also stop working correctly.
These include Fit Image.jsx, Export Layers To Files.jsx and many others.
If getCustomOptions does not work in these scripts, then this is an obvious bug that needs to be fixed.
 
ADD.
Also try using putCustomOptions without "true" as the third argument,
i.e. use only the first two - name and descriptor.
 
 
 

7 replies

filips97187540
Known Participant
May 3, 2024

I can confirm that it's not working with the newest update, version 2024.  

filips97187540
Known Participant
May 4, 2024

@Stephen Marsh 

 

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 );
};

 

Stephen Marsh
Community Expert
Community Expert
May 4, 2024

@filips97187540 

 

Thanks for confirming, other scripted options still work (see my previous post), so at least there are alternatives.

Legend
May 2, 2024

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);
    }

 

 

Stephen Marsh
Community Expert
Community Expert
May 2, 2024

@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.

Stephen Marsh
Community Expert
Community Expert
May 2, 2024
Stephen Marsh
Community Expert
Community Expert
May 2, 2024

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.

 

Legend
May 3, 2024
quote

Putting an alert into the try/catch for getting the session variable results in an error "The requested property does not exist"...


By @Stephen Marsh
 
What exact code (line) causes this error?
Stephen Marsh
Community Expert
Community Expert
May 3, 2024

@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('¬');

 

Stephen Marsh
Community Expert
Community Expert
May 2, 2024

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+=','
            }
}

 

Stephen Marsh
Community Expert
Community Expert
May 2, 2024

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);
};

 

MarvinHeston
Inspiring
August 4, 2025

Thank you.  I presume you run the combined script without the shift key on the Paste function.

Marv

 

Stephen Marsh
Community Expert
Community Expert
August 4, 2025
quote

Thank you.  I presume you run the combined script without the shift key on the Paste function.

Marv

 


By @MarvinHeston

 

That is correct, the modifier key is to copy.

 

The shift modifier could be changed to alt/opt if that is more convenient or makes more sense to you.

 

Or just use the two separate scripts if you need it in an action or custom keyboard shortcut.

Stephen Marsh
Community Expert
Community Expert
May 2, 2024
MarvinHeston
Inspiring
August 3, 2025

The Bit.ly link is blocked.  Anywhere else to get these?

Stephen Marsh
Community Expert
Community Expert
August 4, 2025

@MarvinHeston 

 

Scroll up, there is new code posted from May 2024. The original code and instructions for updating it are posted later in the topic. In all cases the code is 'inline' and not an attachment or a link to another site.