javascript 从 Photoshop 动作到 Photoshop 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5796764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:22:43  来源:igfitidea点击:

From Photoshop actions to Photoshop scripting?

javascriptphotoshopphotoshop-script

提问by Amelio Vazquez-Reina

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

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

  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
  1. 加载给定文件夹中的所有 PNG 文件。
  2. 将每个文件的模式转换为 RGB color
  3. 为每个文件添加一层
  4. 将文件另存为 PSD 在同一文件夹中

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.

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

One thing I know is that I can't run the task above using Actionsbecause 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.

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

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

这让我想到了以下问题:有没有办法自动生成运行给定操作的 Photoshop Javascript 代码

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

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

采纳答案by jani

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.

它可以在很多方面改进,但我希望它有所帮助。

回答by Kevin Scharnhorst

Let me answer the question you actually asked in bold: There is a tool that automatically generates the Javascript for the actions and events that are taking place in Photoshop. It is called the Script Listener. After using the script listener to record your actions, review the log and make your selective edits.

让我用粗体回答您实际提出的问题:有一个工具可以为 Photoshop 中发生的动作和事件自动生成 Javascript。它被称为脚本侦听器。使用脚本侦听器记录您的操作后,请查看日志并进行选择性编辑。

To begin using the Script Listener

开始使用脚本侦听器

  1. Close Photoshop
  2. Copythe ScriptListener.8lifile from the C:\Program Files\Adobe\Adobe Photoshop CS5\Scripting\Utilitiesfolder
  3. Pastethe file to the C:\Program Files\Adobe\Adobe Photoshop CS5\Plug-ins\Automatefolder.
  4. Run Photoshop, perform actions you want to happen in your script.
  5. Close Photoshop, delete the copy of the script listener from the Automate folder.
  6. Edit the log file that is placed on your desktop by the script listener.
  1. 关闭 Photoshop
  2. 复制ScriptListener.8li文件从 C:\ Program Files文件\的Adobe \ Adobe公司的Photoshop CS5 \脚本\实用程序文件夹
  3. 将文件粘贴C:\Program Files\Adobe\Adobe Photoshop CS5\Plug-ins\Automate文件夹。
  4. 运行 Photoshop,执行您希望在脚本中发生的操作。
  5. 关闭 Photoshop,从 Automate 文件夹中删除脚本侦听器的副本。
  6. 编辑由脚本侦听器放置在桌面上的日志文件。

To get your new fangled script into Photoshop place the file you've created with a jsx extension into C:\Program Files\Adobe\Adobe Photoshop CS5\Presets\Scripts.

要将您的新奇特脚本放入 Photoshop,请将您创建的带有 jsx 扩展名的文件放入C:\Program Files\Adobe\Adobe Photoshop CS5\Presets\Scripts

回答by Steven Christenson

I realize this is an old question, but what the original post is asking for can be done entirely in a Photoshop Action. Photoshop actions are easy to record - you just hit record and do the steps manually. - including the Open, operations and save step.

我意识到这是一个老问题,但是原始帖子要求的内容可以完全在 Photoshop Action 中完成。Photoshop 动作很容易记录 - 您只需点击记录并手动执行这些步骤。- 包括打开、操作和保存步骤。

The only trick left is to make the action run in a specific folder rather than the place you record the action. That's easy, however. Use the "Override Open" and "Override Save" options. You invoke this action using File -> Automate -> Batch or from Adobe Bridge using Tools -> Photoshop -> Action

剩下的唯一技巧是让动作在特定文件夹中运行,而不是在您记录动作的位置。不过,这很容易。使用“覆盖打开”和“覆盖保存”选项。您可以使用 File -> Automate -> Batch 或从 Adob​​e Bridge 使用 Tools -> Photoshop -> Action 调用此操作

See this for an example:

请参阅此示例:

enter image description here

在此处输入图片说明

For some additional tips look here

有关一些其他提示,请查看此处

回答by meo

look for the file SaveAsDifferentFileType.jsx on your computer, i think you could use this as starting point.

在您的计算机上查找文件 SaveAsDifferentFileType.jsx,我认为您可以将其用作起点。

There is now way that i know of to generate this code automatically. I think there is no way around learning how it works:

现在我知道有一种方法可以自动生成此代码。我认为没有办法了解它是如何工作的:

Here the documentation: http://www.adobe.com/devnet/photoshop/scripting.htmlAnd here a tutorial that will tell you where to begin: http://morris-photographics.com/photoshop/tutorials/scripting1.html

这里的文档:http: //www.adobe.com/devnet/photoshop/scripting.html这里有一个教程,告诉你从哪里开始:http: //morris-photographics.com/photoshop/tutorials/scripting1.html

If you are using a MAC you could try the Automator Photoshop actions: http://www.completedigitalphotography.com/?p=339

如果您使用的是 MAC,您可以尝试 Automator Photoshop 操作:http: //www.completedigitalphotography.com/?p=339

They will let you do what you want, without any programming know-how.

他们会让你做你想做的,没有任何编程知识。

回答by Glenn Maynard

To update Kevin's answer, ScriptListener is now hidden in a different place:

为了更新凯文的答案,ScriptListener 现在隐藏在不同的地方:

http://www.adobe.com/devnet/photoshop/scripting.html

http://www.adobe.com/devnet/photoshop/scripting.html

inside "Scripting Listener Plug-in". The plugin directory in Windows has also changed, typically:

在“脚本侦听器插件”中。Windows 中的插件目录也发生了变化,通常是:

C:\Program Files\Common Files\Adobe\Plug-Ins\CC

C:\Program Files\Common Files\Adobe\Plug-Ins\CC