Skip to main content
Pedro Cortez Marques
Legend
August 24, 2016
Released

P: Ability to "Ruler origin" by script

  • August 24, 2016
  • 19 replies
  • 5790 views

I want to make sure a user has its ruler origin reseted to document top/left.
But I can't reset it by script.

19 replies

annalopezz
Participating Frequently
February 24, 2026

I recommend using the Long Path Tool to resolve this issue.

Stephen Marsh
Community Expert
Community Expert
May 16, 2021
Pedro Cortez Marques
Legend
November 18, 2016
Thank you Tom!
Tom Ruark
Inspiring
November 18, 2016

// Version 2016.11.18
// Show how to get and set the ruler origin point for the current document
// Values are in pixels shifted 16 bits

// some constants to make it more readable
const classProperty                = app.stringIDToTypeID("property");
const krulerOriginHStr             = app.stringIDToTypeID("rulerOriginH");
const krulerOriginVStr             = app.stringIDToTypeID("rulerOriginV");
const classDocument                = app.stringIDToTypeID("document");
const typeOrdinal                  = app.stringIDToTypeID("ordinal");
const enumTarget                   = app.stringIDToTypeID("targetEnum");
const typeNULL                     = app.stringIDToTypeID("null");
const keyTo                        = app.stringIDToTypeID("to");
const eventSet                     = app.stringIDToTypeID("set");



// get the current values
GetRulerOrigin().toSource();

// set them to zero, negative numbers work as well
SetRulerOrigin(0, 0);



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


function GetRulerOrigin() {


    var ro = {};

    ro.horizontal = GetInfo(classDocument, krulerOriginHStr) >> 16;
    ro.vertical = GetInfo(classDocument, krulerOriginVStr) >> 16;
    
    return ro;
}

function SetRulerOrigin(horiz, vert) {


    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putProperty(classProperty, krulerOriginHStr);
    ref.putEnumerated(classDocument, typeOrdinal, enumTarget);
    desc.putReference(typeNULL, ref);
    desc.putInteger(keyTo, horiz << 16);
    executeAction(eventSet, desc, DialogModes.NO);

    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putProperty(classProperty, krulerOriginVStr);
    ref.putEnumerated(classDocument, typeOrdinal, enumTarget);
    desc.putReference(typeNULL, ref);
    desc.putInteger(keyTo, vert << 16);
    executeAction(eventSet, desc, DialogModes.NO);
}



///////////////////////////////////////////////////////////////////////////////
// Function: GetInfo
// Usage: Get information from Photoshop
// Input: desiredClass, classApplication, classLayer, etc.
//        desiredKey, optional specific key to get instead of everything
//                    this is recommended as all keys is an expensive call
// Return: ActionDescriptor or single value depending on what is asked for
///////////////////////////////////////////////////////////////////////////////
function GetInfo(desiredClass, desiredKey) {


    var reference = new ActionReference();
    if (typeof desiredKey != "undefined") {


        reference.putProperty(stringIDToTypeID("property"), desiredKey);
    }
    reference.putEnumerated(desiredClass, stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum")); 
    var desc = executeActionGet(reference);
    if (typeof desiredKey != "undefined") {


        return GetItemFromDescriptor(desc, desiredKey);
    }
    return desc;
}

///////////////////////////////////////////////////////////////////////////////
// Function: GetItemFromDescriptor
// Usage: Get a specific key from an ActionDescriptor
// Input: desc (ActionDescriptor), valid ActionDescriptor to pull info from
//        desiredKey (Number), key in question, use charIDToTypeID() or
//                             stringIDToTypeID()
// Return: ActionDescriptor or single value depending on what is asked for
///////////////////////////////////////////////////////////////////////////////
function GetItemFromDescriptor(desc, desiredKey) {


    if (desc.hasKey(desiredKey)) {


        var typeID = desc.getType(desiredKey);
        switch (typeID) {


            case DescValueType.BOOLEANTYPE:
return desc.getBoolean(desiredKey);
break;
case DescValueType.STRINGTYPE:
return desc.getString(desiredKey);
break;
case DescValueType.DOUBLETYPE:
return desc.getDouble(desiredKey);
break;
case DescValueType.INTEGERTYPE:
                return desc.getInteger(desiredKey);
                break;
            case DescValueType.LARGEINTEGERTYPE:
            return desc.getLargeInteger(desiredKey);
            break;
case DescValueType.OBJECTTYPE:
                return desc.getObjectValue(desiredKey);
                break;
case DescValueType.UNITDOUBLE:
                var newT = desc.getUnitDoubleType(desiredKey);
                var newV = desc.getUnitDoubleValue(desiredKey);
                return new UnitValue(newV, newT);
                break;
case DescValueType.ENUMERATEDTYPE:
                return desc.getEnumerationValue(desiredKey);
                break;
case DescValueType.CLASSTYPE:
                return desc.getClass(desiredKey);
                break;
case DescValueType.ALIASTYPE:
                return desc.getPath(desiredKey);
                break;
case DescValueType.RAWTYPE:
                var tempStr = desc.getData(desiredKey);
                var rawData = new Array();
                for (var tempi = 0; tempi < tempStr.length; tempi++) { 
                    rawData[tempi] = tempStr.charCodeAt(tempi); 
                }
                return rawData;
                break;
case DescValueType.REFERENCETYPE:
                return desc.getReference(desiredKey);
                break;
case DescValueType.LISTTYPE:
                return desc.getList(desiredKey);
                break;
default:
return;
        }
    }
return;
}

Inspiring
September 5, 2016
I used to have similar problems too, but after using
"long path tool" You can use to solve this problem.
Pedro Cortez Marques
Legend
August 26, 2016
Yes, that's the one I'm using. Thanks,
Participating Frequently
August 26, 2016
The offset solution is the only other suggestion I have, here is the code I use:

var ref = new ActionReference();  // figure out where the ruler origin is - we can't change it, but we can offset it
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );  
var desc = executeActionGet(ref);  
var xRulerRawOffSet = desc.getInteger(stringIDToTypeID('rulerOriginH'));  
var yRulerRawOffSet = desc.getInteger(stringIDToTypeID('rulerOriginV'));  
var xRuler = xRulerRawOffSet/65536 // these are stored as two byte numbers so they need to be divided
var yRuler = yRulerRawOffSet/65536
 I hope it helps!
Pedro Cortez Marques
Legend
August 26, 2016
Each retoucher open/saves 500 images/day on average.
My team has 6500 images/day on average.
Can't do that.
It's easier to assume de ruler origin has a variable on team scripts.
Participating Frequently
August 26, 2016
I thought you could do it within the script by setting a variable to the current file path/filename and then close the file with save changes. Open the file using that variable and the origin will be reset to 0,0.

The files I work with are huge and would take too long to save and re-open. If you are dealing with small files, you wouldn't notice much delay.

I had to use offsets like you described above, but it makes my code more complex.
Pedro Cortez Marques
Legend
August 24, 2016
This was the answer from Michael L Hale:
Re: how to change ruler's Origin

by Mike Hale » Mon Oct 25, 2010 8:56 pm

A couple of thoughts. Scriptlistener doesn't record changing the ruler offset because that is done with the mouse and scriptlistener doesn't record most actions done with the mouse. And I don't see any other way to change it in the GUI using menus or keyboard shortcuts.
RulerOriginH and RulerOriginV are two of a document's action descriptor keys. You can use those IDs to get the current offsets. There are some things that you can get but not set and this may be one of those. When I try to construct an executeAction to set the offset the best I can do is set it to 0. That is to say no matter what value I use in the executeAction, the ruler resets to 0.
I have never spent a lot of time trying to get setting the offset to work so it may be possible but I suspect that it needs access to the ruler class and action manager does not have that access.