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

Use a script to obtain the text layer bounds, where x and y are inconsistent with the x and y in UI

Participant ,
Sep 28, 2024 Sep 28, 2024

The x and y of the text layer below are inconsistent

li27112570w0z3_0-1727512912071.png

 

The value of the text layer below is correct, x and y are consistent

li27112570w0z3_1-1727512925904.png

The value of the "TRAVEL northwest" layer is also wrong, and x and y are inconsistent. One thing these two layers with wrong values ​​have in common is that the text styles are repeated.

The picture below is the text style obtained through the script, which is repeated.

 

li27112570w0z3_2-1727512937549.png

甘肃旅游海报(10).mov  is a zip file, modify the suffix and decompress it.

 

Below is the script to get the text style

 

 

 

function get_layer_transform_yy() {
    // 创建了一个 ActionReference 对象,是 Photoshop 脚本用来引用特定项目的对象
    var ref = new ActionReference();
    // 将当前选中的图层作为目标图层放入引用中。
    ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    // 执行获取操作,获取当前图层的描述对象
    var desc = executeActionGet(ref).getObjectValue(stringIDToTypeID('textKey'))
    // 描述对象中存在transform
    if (desc.hasKey(stringIDToTypeID('transform'))) {
        //获取 transform 对象值
        desc = desc.getObjectValue(stringIDToTypeID('transform'))
        //获取 yy 值
        var yy = desc.getDouble(stringIDToTypeID('yy'));

        return yy;
    }
    return "";
}

//ps的脚本很古老,不支持json模块,手工拼接json字符串
function formatResult(obj, indent) {
    if (typeof indent === 'undefined') {
        indent = '';
    }
    var result = '';
    var indentStep = '  ';

    if (obj instanceof Array) {
        result += '[';
        for (var i = 0; i < obj.length; i++) {
            result += indent + indentStep + formatResult(obj[i], indent + indentStep) + (i < obj.length - 1 ? ',' : '');
        }
        result += indent + ']';
    } else if (typeof obj === 'object' && obj !== null) {
        result += '{';
        var keys = [];
        for (var key in obj) {
            if (obj.hasOwnProperty(key)) {
                keys.push(key);
            }
        }
        for (var j = 0; j < keys.length; j++) {
            var key = keys[j];
            result += indent + indentStep + '"' + key + '": ' + formatResult(obj[key], indent + indentStep) + (j < keys.length - 1 ? ',' : '');
        }
        result += indent + '}';
    } else if (typeof obj === 'string') {
        result += '"' + obj + '"';
    } else {
        result += obj;
    }

    return result;
}

//结果去重
function distinct(textInfoList) {
    //去重后textInfoList
    var uniqueTextInfoList = [];

    //遍历textInfoList
    for (var i = 0; i < textInfoList.length; i++) {
        //初始标记不重复
        var isDuplicate = false;
        //当前textInfo
        var textInfo = textInfoList[i];

        //遍历去重后textInfoList
        for (var j = 0; j < uniqueTextInfoList.length; j++) {
            //初始标记相同
            var isSame = true;

            // 遍历textInfo的key
            for (var key in textInfo) {
                //如果不是自定义属性key,则跳过
                if (!textInfo.hasOwnProperty(key)) {
                    continue;
                }

                // 如果两个对象在相同的属性上值不同,则标记这2个对象不同
                if (textInfo[key] !== uniqueTextInfoList[j][key]) {
                    isSame = false;
                    break;
                }
            }
            
            //如果相同,则标记重复,跳出循环
            if (isSame) {
                isDuplicate = true;
                break;
            }
        }
        
        //不重复,才添加到去重后textInfoList
        if (!isDuplicate) {
            uniqueTextInfoList.push(textInfo);
        }
    }

    return uniqueTextInfoList;

}

// 映射 fontCaps 枚举值到可读字符串
var capsMap = {
    2654: 'AllCaps', // 全部大写
    2653: 'SmallCaps' // 小型大写
};

// 映射 baseline 枚举值到可读字符串
var baselineMap = {
    2656: 'Superscript', // 上标
    2657: 'Subscript' // 下标
};

// 映射下划线和删除线枚举值
var underlineMap = {
    2701: 'Underline' // 下划线
};

var strikethroughMap = {
    2697: 'Strikethrough' // 删除线
};


//获取文本的段落信息
//https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-get-color-info-of-text-in-text-layer/m-p/3342131
//https://www.ps-scripts.com/viewtopic.php?t=7736
function get_layer_text_info() {
    // 创建了一个 ActionReference 对象,是 Photoshop 脚本用来引用特定项目的对象
    var ref = new ActionReference();
    // 将当前选中的图层作为目标图层放入引用中。
    ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    // 执行获取操作,获取当前图层的描述对象
    var layerDesc = executeActionGet(ref);
    var textDesc = layerDesc.getObjectValue(stringIDToTypeID('textKey'));
    // 从描述对象中获取 'textStyleRange' 列表
    // textStyleRange 代表的是文本图层中不同样式范围的集合。即多个段落
    var layerStyles = textDesc.getList(stringIDToTypeID('textStyleRange'));
    //整个文本内容
    var textContents = textDesc.getString(stringIDToTypeID('textKey'));
    var textInfoList = [];

    //获取层变换矩阵中的yy值
    var yy = get_layer_transform_yy();

    alert("layerStyles.count:" + layerStyles.count)

    //遍历段落
    for (var i = 0; i < layerStyles.count; i++) {
        //获取段落
        var textStyleRange = layerStyles.getObjectValue(i);
        //该段落在整个文本内容中的起点
        var from = textStyleRange.getInteger(stringIDToTypeID('from'));
        //该段落在整个文本内容中的终点
        var to = textStyleRange.getInteger(stringIDToTypeID('to'));

        // //获取段落样式
        var textStyle = textStyleRange.getObjectValue(stringIDToTypeID('textStyle'));

        //获取段落字体名
        var fontName = textStyle.hasKey(stringIDToTypeID("fontName")) ? textStyle.getString(stringIDToTypeID('fontName')) : null;

        //获取字符间距
        var tracking = textStyle.hasKey(stringIDToTypeID("tracking")) ? textStyle.getInteger(stringIDToTypeID("tracking")) : null;

        //获取字体大小
        var fontSize = textStyle.getDouble(stringIDToTypeID('size'));
        //获取行间距
        var leading = textStyle.hasKey(stringIDToTypeID('leading')) ? textStyle.getDouble(stringIDToTypeID('leading')) : null;
        //获取段落文本内容
        var textSegment = textContents.substring(from, to);


        //获取颜色
        var color;
        if (textStyle.hasKey(stringIDToTypeID("color"))) {
            var colorDesc = textStyle.getObjectValue(stringIDToTypeID('color'));
            var red = colorDesc.getDouble(stringIDToTypeID('red'));
            var green = colorDesc.getDouble(stringIDToTypeID('green'));
            var blue = colorDesc.getDouble(stringIDToTypeID('blue'));
            color = red + "," + green + "," + blue;
        } else {
            color = null;
        }


        // 获取全部大写、小型大写
        var caps = textStyle.hasKey(stringIDToTypeID("fontCaps")) ? textStyle.getEnumerationValue(stringIDToTypeID("fontCaps")) : null;
        // 获取下划线
        var underline = textStyle.hasKey(stringIDToTypeID("underline")) ? textStyle.getEnumerationValue(stringIDToTypeID("underline")) : null;
        // 获取删除线
        var strikethrough = textStyle.hasKey(stringIDToTypeID("strikethrough")) ? textStyle.getEnumerationValue(stringIDToTypeID("strikethrough")) : null;
        // 获取上标、下标
        var baseline = textStyle.hasKey(stringIDToTypeID("baseline")) ? textStyle.getEnumerationValue(stringIDToTypeID("baseline")) : null;

        // alert(textStyle.hasKey(stringIDToTypeID("baseline")))


        //放到数组
        //如果yy值存在,则需要变换计算
        textInfoList.push({
            text: textSegment
            , fontName: fontName
            , fontSize: yy === "" ? fontSize : fontSize * yy
            , leading: leading === null ? null : yy === "" ? leading : leading * yy
            , color: color
            , tracking: tracking
            , caps: caps === null ? null : capsMap[caps] ? capsMap[caps] : null
            , baseline: baseline === null ? null : baselineMap[baseline] ? baselineMap[baseline] : null
            , underline: underline === null ? null : underlineMap[underline] ? underlineMap[underline] : null
            , strikethrough: strikethrough === null ? null : strikethroughMap[strikethrough] ? strikethroughMap[strikethrough] : null
        });

    }

    // 结果去重
    var uniqueTextInfoList = distinct(textInfoList);

    //把对象数组格式化为json字符串返回
    return formatResult(textInfoList);
    // return formatResult(uniqueTextInfoList);
}

try {
    var result = get_layer_text_info();
    alert( result);
} catch (error) {
    var errMsg = "ps_script_error:" + error;
    alert(errMsg);
}

 

 

 

 

 

TOPICS
Actions and scripting , Windows
764
Translate
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
Adobe
Community Expert ,
Sep 28, 2024 Sep 28, 2024

Please provide the file. 

Translate
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
Participant ,
Sep 28, 2024 Sep 28, 2024

psd has been uploaded and is in the attachment

Translate
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 ,
Sep 29, 2024 Sep 29, 2024

The Layer Style seems to cause the issue. 

Try using »boundsNoEffects« instead of »bounds«, though the numbers may still be slightly off compared to the ones given in the Properties Panel. 

Translate
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
Participant ,
Sep 29, 2024 Sep 29, 2024

Thanks a lot, boundsNoEffects works

Translate
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
Participant ,
Sep 29, 2024 Sep 29, 2024

But I still don’t know the reason for the repeated problem of Layer Style.

Translate
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 ,
Sep 30, 2024 Sep 30, 2024
quote

But I still don’t know the reason for the repeated problem of Layer Style.


By @javaer

I am not sure I understand. 

Translate
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
LEGEND ,
Sep 30, 2024 Sep 30, 2024
LATEST

Anything that alters the text styling will change the bounding box. Let's say you crop to the boundsnoEffects property values, you could be cutting off part of your text styling.

Translate
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