Skip to main content
Inspiring
August 21, 2023
Answered

Write a word or special character in the selected areas automatically

  • August 21, 2023
  • 3 replies
  • 543 views

Sometimes I have high number of chart images and convert them to following by photoshop action: 

 

above images are 3 transparent sample of my edited chart images. 

Now I want to write a word or special character in the selected areas automatically like following: 

 

above screenshot is selected areas. 

In following I write "#" in front of left side chart names: 

 

I must do this automatically by photoshop action or script. 

Note that number of selected areas in each image maybe different and script or action must write # in all of selected areas. 

Note that I don't want to split selected areas to multiple layers because I want to edit high number of chart images and it take long time. 

I attach my 3 sample as gif file in this post for test. 

This topic has been closed for replies.
Correct answer abolfazl29032603daba

I write following script by ChatGPT and it working well but before run this script I must convert selection to a workpath with name of "Path 1" and after that I can run this script: 

var doc = app.activeDocument;
var workPathIndex = findWorkPathIndex(doc, "Path 1");

if (workPathIndex >= 0) {
    var workPath = doc.pathItems[workPathIndex];
    markSubpathCenters(workPath);
} else {
    alert("Work path 'Path 1' not found.");
}

function findWorkPathIndex(document, workPathName) {
    for (var i = 0, len = document.pathItems.length; i < len; i++) {
        if (document.pathItems[i].name === workPathName) {
            return i;
        }
    }
    return -1;
}

function markSubpathCenters(workPath) {
    var subPathCenters = [];

    for (var i = 0, len = workPath.subPathItems.length; i < len; i++) {
        var subPath = workPath.subPathItems[i];
        var center = getSubpathCenter(subPath);

        if (center) {
            subPathCenters.push(center);
        }
    }

    createMarkers(subPathCenters);
}

function getSubpathCenter(subPath) {
    var pathPoints = subPath.pathPoints;
    
    if (pathPoints.length > 0) {
        var totalX = 0;
        var totalY = 0;

        for (var i = 0, len = pathPoints.length; i < len; i++) {
            totalX += pathPoints[i].anchor[0];
            totalY += pathPoints[i].anchor[1];
        }

        var centerX = totalX / pathPoints.length;
        var centerY = totalY / pathPoints.length;

        return [centerX, centerY];
    }

    return null;
}

function createMarkers(centers) {
    var markerLayer = doc.artLayers.add();
    markerLayer.kind = LayerKind.TEXT;
    var textItem = markerLayer.textItem;

    textItem.contents = "+";
    textItem.size = 124;
    textItem.color.rgb.hexValue = "000000";
    textItem.justification = Justification.CENTER;

    for (var i = 0, len = centers.length; i < len; i++) {
        var center = centers[i];
        var x = center[0];
        var y = center[1];
        var markerCopy = markerLayer.duplicate();
        markerCopy.textItem.position = [x, y];
    }
}

 

 

3 replies

abolfazl29032603dabaAuthorCorrect answer
Inspiring
August 22, 2023

I write following script by ChatGPT and it working well but before run this script I must convert selection to a workpath with name of "Path 1" and after that I can run this script: 

var doc = app.activeDocument;
var workPathIndex = findWorkPathIndex(doc, "Path 1");

if (workPathIndex >= 0) {
    var workPath = doc.pathItems[workPathIndex];
    markSubpathCenters(workPath);
} else {
    alert("Work path 'Path 1' not found.");
}

function findWorkPathIndex(document, workPathName) {
    for (var i = 0, len = document.pathItems.length; i < len; i++) {
        if (document.pathItems[i].name === workPathName) {
            return i;
        }
    }
    return -1;
}

function markSubpathCenters(workPath) {
    var subPathCenters = [];

    for (var i = 0, len = workPath.subPathItems.length; i < len; i++) {
        var subPath = workPath.subPathItems[i];
        var center = getSubpathCenter(subPath);

        if (center) {
            subPathCenters.push(center);
        }
    }

    createMarkers(subPathCenters);
}

function getSubpathCenter(subPath) {
    var pathPoints = subPath.pathPoints;
    
    if (pathPoints.length > 0) {
        var totalX = 0;
        var totalY = 0;

        for (var i = 0, len = pathPoints.length; i < len; i++) {
            totalX += pathPoints[i].anchor[0];
            totalY += pathPoints[i].anchor[1];
        }

        var centerX = totalX / pathPoints.length;
        var centerY = totalY / pathPoints.length;

        return [centerX, centerY];
    }

    return null;
}

function createMarkers(centers) {
    var markerLayer = doc.artLayers.add();
    markerLayer.kind = LayerKind.TEXT;
    var textItem = markerLayer.textItem;

    textItem.contents = "+";
    textItem.size = 124;
    textItem.color.rgb.hexValue = "000000";
    textItem.justification = Justification.CENTER;

    for (var i = 0, len = centers.length; i < len; i++) {
        var center = centers[i];
        var x = center[0];
        var y = center[1];
        var markerCopy = markerLayer.duplicate();
        markerCopy.textItem.position = [x, y];
    }
}

 

 

Inspiring
August 22, 2023

I can do this by custom pattern and random fill with "+" character but it is not accurate: 

 

Inspiring
August 21, 2023

@c.pfaffenbichler @Stephen Marsh @r-bin   

 

Can you tell me how can i get info of selected areas by script for this process?