• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Rulers and scripting

Community Beginner ,
Nov 17, 2022 Nov 17, 2022

Copy link to clipboard

Copied

As we have scripts for placing rulers to the center of the canvas, could there be a script made to make coordinate 0.0 in the center of the canvas, instead of dragging it manually from the corner?

TOPICS
Actions and scripting

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 17, 2022 Nov 17, 2022

@aleksanderk54154416 – Ask and you shall receive!

 

/*
Set Ruler Origin to Canvas Center.jsx
Version 1.0, 17th November 2022
Stephen Marsh

Based on:
https://community.adobe.com/t5/photoshop/feature-request-set-origin-point/td-p/12037399
Feature Request: Set Origin Point
*/

#target photoshop

if (app.documents.length > 0) {

    (function () {

        // Save the current ruler units and set to pixels
        var savedRuler = app.preferences.rulerUnits;
        app.preferences.rulerUnits = Unit
...

Votes

Translate

Translate
Adobe
Community Expert ,
Nov 17, 2022 Nov 17, 2022

Copy link to clipboard

Copied

@aleksanderk54154416 – Ask and you shall receive!

 

/*
Set Ruler Origin to Canvas Center.jsx
Version 1.0, 17th November 2022
Stephen Marsh

Based on:
https://community.adobe.com/t5/photoshop/feature-request-set-origin-point/td-p/12037399
Feature Request: Set Origin Point
*/

#target photoshop

if (app.documents.length > 0) {

    (function () {

        // Save the current ruler units and set to pixels
        var savedRuler = app.preferences.rulerUnits;
        app.preferences.rulerUnits = Units.PIXELS;

        var hCenter = activeDocument.width.value / 2;
        $.writeln(hCenter);

        var vCenter = activeDocument.height.value / 2;
        $.writeln(vCenter);

        /*
        // Add horizontal center guide
        activeDocument.guides.add(Direction.HORIZONTAL, vCenter);
        
        // Add vertical center guide
        activeDocument.guides.add(Direction.VERTICAL, hCenter);
        */

        ////////////////////////////// tom_ruark @ adobe //////////////////////////////

        /* https://feedback.photoshop.com/conversations/photoshop/photoshop-ability-to-ruler-origin-by-script/5f5f45bb4b561a3d425c7b32 */

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

        function GetRulerOrigin() {
            var ro = {};

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

            return ro;
        }

        function SetRulerOrigin_Horiz(horiz) {
            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);
        }

        function SetRulerOrigin_Vert(vert) {
            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;
        }

        // Reset ruler origin to zero for a known start point
        SetRulerOrigin_Horiz(0);
        SetRulerOrigin_Vert(0);

        ////////////////////////////// tom_ruark @ adobe //////////////////////////////

        // Set the ruler origin to the center
        SetRulerOrigin_Horiz(hCenter);
        SetRulerOrigin_Vert(vCenter);

        // Restore the ruler units
        app.preferences.rulerUnits = savedRuler;
    })
        ();
}

else {
    alert('A document must be open to use this script!');
}

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 17, 2022 Nov 17, 2022

Copy link to clipboard

Copied

Man! You did it again! 

Thanks!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 17, 2022 Nov 17, 2022

Copy link to clipboard

Copied

LATEST

@aleksanderk54154416 
You're welcome. Please mark my previous reply as the correct answer if it is an accepted solution.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines