从Photoshop动作到Photoshop脚本? [英] From Photoshop actions to Photoshop scripting?

查看:578
本文介绍了从Photoshop动作到Photoshop脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望Photoshop对给定的文件夹自动执行以下任务:

I would like Photoshop to automatically execute the following task for a given folder:

  1. 将所有PNG文件加载到给定文件夹中.
  2. 将每个文件的模式转换为RGB color
  3. 为每个文件添加一层
  4. 将文件另存为PSD到同一文件夹中
  1. Load all PNG files in a given folder.
  2. Convert each file's mode to RGB color
  3. Add one layer to each file
  4. Save the files as PSD in the same folder

有人告诉我这可以用Photoshop脚本完成,但是我不知道如何开始,因为不幸的是我对JavaScript没有太多的经验.

I have been told that this can be done with Photoshop scripting, but I don't know how to get started since unfortunately I don't have much experience with JavaScript.

我知道的一件事是我无法使用Actions运行上述任务,因为当我记录最后一步(4)时,Photoshop记录了将PSD文件保存到记录该文件时所使用的文件夹中的操作.宏(而不是用于加载原始PNG文件的宏).换句话说,它将目标文件夹固定为宏中使用的那个文件夹.

One thing I know is that I can't run the task above using Actions because when I record the last step (4), Photoshop records the action to save the PSD files in the folder that I use when recording the macro (instead of the one used to load the original PNG files). In other words, it fixes the destination folder to the one used in the macro.

这使我想到以下问题:是否可以自动生成运行给定操作的Photoshop Javascript代码?

This takes me to the following question: Is there a way to automatically generate the Photoshop Javascript code that runs a given action?

如果是这样,我不介意学习如何修改脚本来解决上述文件夹问题.

If so, I wouldn't mind learning how to modify the script to fix the above folder problem.

推荐答案

我制作了一个脚本,可以完成所需的工作:

I made a script which does the required job:

#target photoshop
#strict on

runthis();
function runthis()
{
    var path = "/d/PhotoshopScript/Images/";

     var inputFolder = new Folder(path );
    var inputFiles = inputFolder.getFiles("*.png");

    for(index in inputFiles)
    {
        // open the file
        var fileToOpen = new File(inputFiles[index]);
        open(fileToOpen);

        // Change mode to rgb
        activeDocument.changeMode(ChangeMode.RGB);
        // add a new layer
        activeDocument.artLayers.add();

        //save
        var psdOptions = new PhotoshopSaveOptions();
        psdOptions.alphaChannels = true;
        psdOptions.annotations = false;
        psdOptions.embedColorProfile = false;
        psdOptions.layers = true;
        psdOptions.spotColors = false;

        var file = new File(path + GetFileName(String(inputFiles[index])));
        activeDocument.saveAs(file, psdOptions);

        activeDocument.close();

        // dispose
        fileToOpen = null;
        psdOptions = null;
        file  = null;
    }
    // dispose
    inputFolder = null;
    inputFiles = null;

}

function GetFileName(fullPath)
{
    var m = fullPath.match(/(.*)[\/\\]([^\/\\]+)\.\w+$/);
    return m[2];
}

可以在许多方面进行改进,但希望对您有所帮助.

It can be improved in many ways, but I hope it helps.

这篇关于从Photoshop动作到Photoshop脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆