Skip to main content
bala281972
Known Participant
December 14, 2012
Question

delete one guide at a time [2012]

  • December 14, 2012
  • 3 replies
  • 3315 views

Hi

 

any idea on how to delete/remove one guide at a time pl. I've gone through the forum which clears all the guides but couldnt find any help on deleting guides one at a time.

Any possible solution

This topic has been closed for replies.

3 replies

tssee
Inspiring
March 11, 2016

I tried in every way but does not go

Use photoshop cc2014

Participant
May 12, 2024

For anyone looking to do this now almost 10 years in the future, the code would look something like this:

async function deleteAGuide() {
   await executeAsModal(async () => {
      app.activeDocument.guides[0].delete();
   });
}

Good luck

Stephen Marsh
Community Expert
Community Expert
May 12, 2024
quote

For anyone looking to do this now almost 10 years in the future, the code would look something like this:

async function deleteAGuide() {
   await executeAsModal(async () => {
      app.activeDocument.guides[0].delete();
   });
}

Good luck


By @things_and_things


Thanks for sharing! Just to clarify, this is UXP .psjs code, not the more common legacy ExtendScript .jsx code.

cameronmcefee
Inspiring
March 9, 2016

I'm about 3.5 years too late, but here's how to delete a guide by its index:

function removeGuideByIndex(idx) {

   // Build a reference to the guide we want

  var ref = new ActionReference()

  // Guide indexes start at 1 for some reason.

  ref.putIndex(charIDToTypeID('Gd  '), idx+1)

  // Create a descriptor with our reference in it

  var desc = new ActionDescriptor()

  desc.putReference(charIDToTypeID('null'), ref)

  // Execute the delete action on our descriptor.

  executeAction(charIDToTypeID('Dlt '), desc, DialogModes.NO);

}

tssee
Inspiring
March 10, 2016

I tried the script but does not work for me.

cameronmcefee
Inspiring
March 10, 2016

Can you be more specific about what doesn't work? It's working fine for me in CC+

To test:

1. Create a new document.

2. Create a guide on the left side, then one on the right side.

3. Run the function above with removeGuideByIndex(0)

The guide on the left will disappear, as it was the first guide added.

*I'm making the assumption you are familiar with how JavaScript works. If not, the code above is just a function declaration and won't do anything unless you know your guide's index and how to call a function with it.

Inspiring
December 14, 2012

With version greater than CS4 guides are part of the Object Model.

app.activeDocument.guides.length;// number of guides in document

app.activeDocument.guides[0].remove();// remove a guide by index

The index is in the order the guides were created.

You can also determine where a guide is by looking at it's coordinate and direction priperties.

Inspiring
December 14, 2012

And for those mere motals using the Ludditian CS2? I noticed that the script listener doesn't pick up on guide deletion. And since they're not part of the the object model it's gonna be quite difficult to remove them (or even move them to off the canvas)

Inspiring
December 14, 2012

Yes, with older versions you are stuck with using Action Manger to work with guides. And as far as I can tell you can't remove a single guide with AM code.

You can remove all guides and you can create guides. I also have code that will let you get info about the current guides in a document. With that you could get the info about the guides in a document, remove all the guides, then add back the ones you want to keep. Not pretty but it should work. Here is the code.

To get guide info:

#target photoshop

app.bringToFront();

/* the info about guides can be found in the photoshop tag hex 35 42 49 4D 04 08

   the length of the tag is found at offset 11. that value is the length to the next tag

   the number of guides can be found at offset 27. From that point the data repeats

   for each guide. 4 bytes for the location. That value / 32 = pixels

   and one byte for type 0 = vert and 1 = horz

   Note: this info is just a best guess from looking at the data

*/

///////////////////////////////////////////////////////////////////////////////

// Function: getGuideInfo

// Description: Saves the document as a temp jpg file

//                        and extracts the info about the guides

//                        in document if any

// Usage: var guides = getGuideInfo( activeDocument );

// Input: Document

// Return: Object: TotalCount property  = number of guides

//                  HorizontalCount property = number of horizontal guides

//                  VerticalCount property = number of vertical guides

//                  Horizontal = horizontal guides

//                  Vertical property = vertical guides

// Source: Modified from http://ps-scripts.com/bb/viewtopic.php?t=3283

///////////////////////////////////////////////////////////////////////////////

function getGuideInfo( doc ) {

   saveOptions = new JPEGSaveOptions();

   // we don't care about image quality in the temp file

   // we do want the smallest file size

   saveOptions.quality = 0;// we don't care about image quality in the temp file

   var tempFile = new File( Folder.temp + '/' +'guideTemp.jpg' );

   // Dupe the doc, make it savable as JPEG, save it, then close the file

   doc = doc.duplicate();

   if( doc.mode == DocumentMode.BITMAP ) doc.changeMode(ChangeMode.GRAYSCALE);

   doc.bitsPerChannel = BitsPerChannelType.EIGHT;

   doc.changeMode(ChangeMode.RGB);

   doc.saveAs(tempFile, saveOptions, true);

   doc.close(SaveOptions.DONOTSAVECHANGES);

  tempFile.encoding = 'BINARY';

  tempFile.open('r');

  var str = tempFile.read();

  tempFile.close();

  tempFile.remove();

   var guideTag = '8BIM\x04\x08';

   var tagPos = str.search(guideTag);

   var guides = new Object;

   guides.toString = function(){ return 'GuideInfo'; }

   var horGuides = new Array;

   var vertGuides = new Array;

   if( tagPos != -1 ) {

      var tagLength = 12 + str.charCodeAt( tagPos + 11 );

      var guideStr = str.substring( tagPos, tagPos+tagLength );

      guides.count = str.charCodeAt( tagPos + 27 );

      var pointer = tagPos + 28;

      for( var i =0; i < guides.count; i++ ){

       //  var n = ((str[pointer] << 3) + (str[pointer+1] << 2) + (str[pointer+2] << 1) + str[pointer+3])/32;

      //var n = Number( '0x'+getHexString( str, pointer )+getHexString( str, pointer+1 )+getHexString( str, pointer+2 )+getHexString( str, pointer+3 ))/32;

             var n = ((str.charCodeAt(pointer)<< 32) + (str.charCodeAt(pointer + 1)<< 16) +

                    (str.charCodeAt(pointer + 2) << 8) + str.charCodeAt(pointer+3))/32;

if  (str.charCodeAt( pointer + 4 ) == 0) {

          //vertical

          vertGuides = insertValueInOrder(vertGuides, n);

       } else {

          //horizontal

          horGuides = insertValueInOrder(horGuides, n);

       }  

        // guides[ i ] =  [ (str.charCodeAt( pointer + 4 ) ? 'horz':'vert'), n ];    

         pointer = pointer + 5;

      }

     guides.HorizontalCount =horGuides.length;

     guides.VerticalCount =vertGuides.length;

     guides.Horizontal = horGuides;

     guides.Vertical = vertGuides;

   }else{

      guides.TotalCount = 0;

     guides.HorizontalCount =0;

     guides.VerticalCount =0;

   }

   function insertValueInOrder(a , n) {

      var b = new Array;

      var i = 0;

      if (a[0] == undefined)

         b[0] = n;

      else {

         while (a < n && i < a.length) {

               b = a;

               i++;

         }

         b = n;

         while (i < a.length) {

            b[i+1] = a;     

            i++;

         }

      }  

      return b;

   }

   return guides;

}

  function fromCharCode(input) {

      output = eval("String.fromCharCode(" + input + ")");

     return output;

   }

// demo

var masterDocument = app.activeDocument;

var guideInfo = getGuideInfo( masterDocument );

var s = "Vertical Guides: " + guideInfo.Vertical + "\nHorizontal Guides: " + guideInfo.Horizontal;

alert(s);

To remove all guides:

function clearAllGuides() {

     var desc = new ActionDescriptor();

        var ref = new ActionReference();

        ref.putEnumerated( charIDToTypeID( "Gd  " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Al  " ) );

     desc.putReference( charIDToTypeID( "null" ), ref );

     executeAction( charIDToTypeID( "Dlt " ), desc, DialogModes.NO );

};

To add a guide:

function guideLine(position, type,unit) {// from Paul MR

   //units '#Pxl' pixel '#Rlt' =Relative  '#Prc' = percent

   // types: 'Vrtc' & 'Hrzn'

    var desc = new ActionDescriptor();

        var desc2 = new ActionDescriptor();

        desc2.putUnitDouble( charIDToTypeID('Pstn'), charIDToTypeID(unit), position);

        desc2.putEnumerated( charIDToTypeID('Ornt'), charIDToTypeID('Ornt'), charIDToTypeID(type) );

    desc.putObject( charIDToTypeID('Nw  '), charIDToTypeID('Gd  '), desc2 );

    executeAction( charIDToTypeID('Mk  '), desc, DialogModes.NO );

};