在Photoshop脚本中如何跳过对话框?

3
我正在尝试创建一个脚本,可以在一次单击中从Photoshop保存JPG文件。它将以与原始PSD文件相同的文件名相同的目录中保存,只是以JPG格式保存。我在网上找到了一个脚本,可以实现我想要的90%,但是会打开两个对话框 - 第一个询问是否要重命名文件,第二个对话框指定位置。目前我可以按两次“Enter”,它就可以工作,但我不想在第一次出现这两个对话框(因此脚本将自动为我在这些对话框上按“Enter”键,希望甚至不需要打开这些对话框,而是在后台运行)。
以下是我目前拥有的脚本。我稍微修改了一下,所以它不像最初那样保存PSD文件,只保存JPG。不需要Illustrator部分,所以不要太担心它。
    // ***** SaveTo.jsx Version 1.1. *****

    Changelog:

    v.1.1.
        - Fixed a weird issue where saving as jpg causes weird dialog stuff to happen if it's not saved as a copy. This started happening somewhere between PS CC 2017 and PS CC 2018.
        - Variable "pdfProfileAI" now has a failsafe where if the set profile doesn't exist, the first found profile is used instead. So what ever happens the pdf will be saved, but it might be using a wrong profile, but don't worry, the script will tell you if this happened and gives the name of the profile that was used. This failsafe would still fail if someone doesn't have any presets at all, but if that ever happens, he had it coming...
        - Added a new variable "listOfPresetsAI" so that you can easily get a list of preset so you can copy and paste the preset name to "pdfProfileAI";

var newDocPath = "~/Desktop/";
var listOfPresetsAI = false; // When set to true the script won't save anything but instead gives you a list of pdf preset names.
var pdfProfileAI = "CMYK - Swop v2s";
var dialogOffsetX = 250; // Subtracts from the center of the screen = Center of the screen horizontally - 250px.
var dialogOffsetY = 200; // Just a static value = 200px from the top of the screen.

var o = {};
var appName = app.name;
var pdfPresetNoMatch = false;

// If Photoshop
if ( appName === "Adobe Photoshop" ) {
    o.dialogTitle = "Save As: .jpg";
    o.formats = ['jpg'];
    o.app = 'ps';
}
// If Illustrator
else if ( appName === "Adobe Illustrator" ) {
    o.dialogTitle = "Save As: .ai and .pdf";
    o.formats = ['pdf','ai'];
    o.app = 'ai';
}

function dialog() {

    // Dialog
    var dlg = new Window("dialog");
    dlg.text = o.dialogTitle;

    var panel = dlg.add('panel');
    panel.alignChildren = ['left','center'];
    panel.orientation = 'row';

    var text1 = panel.add('statictext');
    text1.text = 'Filename: ';

    var text2 = panel.add('editText');
    text2.text = app.activeDocument.name.split('.')[0];
    text2.preferredSize = [530,23];
    text2.active = true;

    var button1 = panel.add('button', undefined, undefined, { name: 'cancel'});
    button1.text = "Cancel";

    var button2 = panel.add('button', undefined, undefined, { name: 'ok'});
    button2.text = "Save...";

    button2.onClick = function ( e ) {
        save.init( dlg, text2.text );
    };

    dlg.onShow = function () {
        dlg.location.x = dlg.location.x - dialogOffsetX;
        dlg.location.y = dialogOffsetY;
    }

    dlg.show();

}

var save = {
    init: function( dlg, dialog_filename ) {

        dlg.close();

        var doc = app.activeDocument;

        var doc_path;
        try {
            doc_path = doc.path.toString();
        } catch(e) {
            doc_path = newDocPath;
        }

        var output_folder = Folder.selectDialog( 'Output folder', doc_path === "" ? newDocPath : doc_path );

        if ( output_folder != null ) {
            for ( var i = 0; i < o.formats.length; i++ ) {
                var save_options = save[ o.formats[i] ]();

                if ( o.app === 'ps' ) {
                    doc.saveAs( new File( output_folder + '/' + dialog_filename ), save_options, ( o.formats[i] === 'jpg' ? true : false ), Extension.LOWERCASE );
                }
                else if ( o.app === 'ai' ) {
                    doc.saveAs( new File( output_folder + '/' + dialog_filename ), save_options );
                }
            }
        }

    },
    ai: function() {

        var ai_options = new IllustratorSaveOptions();
        ai_options.flattenOutput = OutputFlattening.PRESERVEAPPEARANCE;
        return ai_options;

    },
    pdf: function() {

        var pdf_Options = new PDFSaveOptions();
        pdf_Options.pDFPreset = checkPresets( false, pdfProfileAI );
        return pdf_Options;

    },
    psd: function() {

        var psd_Options = new PhotoshopSaveOptions();
        return psd_Options;

    },
    jpg: function() {

        var jpg_Options = new JPEGSaveOptions();
        jpg_Options.embedColorProfile = true;
        jpg_Options.FormatOptions = FormatOptions.OPTIMIZEDBASELINE; // OPTIMIZEDBASELINE, PROGRESSIVE, STANDARDBASELINE
        // jpg_Options.scans = 5; // For FormatOptions.PROGRESSIVE
        jpg_Options.matte = MatteType.WHITE; // BACKGROUND, BLACK, FOREGROUND, NETSCAPE, NONE, SEMIGRAY, WHITE
        jpg_Options.quality = 11; // 0-12
        return jpg_Options;

    }
};

function checkPresets( list, testPreset ) {

    var pdfPresets = app.PDFPresetsList;

    if ( list === true ) {
        alert( "\n" + pdfPresets.join('\n') );
    }
    else {
        var preset = null;
        for ( var i = pdfPresets.length; i--; ) {
            if ( pdfPresets[i] === testPreset ) {
                preset = testPreset;
            }
        }
        pdfPresetNoMatch = (preset === null);
        return (pdfPresetNoMatch ? pdfPresets[0] : preset);
    }

}

if ( listOfPresetsAI === true ) {
    checkPresets( true );
}
else if ( app.documents.length > 0 ) {

    dialog();
    if ( pdfPresetNoMatch ) alert( "Couldn't use your PDF preset!!! \n Used " + app.PDFPresetsList[0] + " instead." );

}

非常感谢您的帮助。我有基本的编码技能,但从未使用过Photoshop脚本。
谢谢你抽出时间。
Alexey.B

你无法在对话框函数的顶部仅输入 save.init(dlg, text2.text); 并跳过其余部分吗? - Grumpy
不确定如何做到这一点。我将您的代码片段复制并粘贴到对话框函数的顶部,但出现了“未定义对象”的错误。我需要先声明它吗?如果是这样,在哪里声明? - Alexey_Berezov
我理解问题出在对话框函数中,但是不知道该删除哪些内容而不破坏整个脚本。我试图对其进行修剪,但每次尝试做任何事情时都会出现错误。如果可能的话,您能否请粘贴完整函数的代码,并提供您的建议解决方案? - Alexey_Berezov
4个回答

1
如果您只想将其保存为JPEG格式(保存到新位置或源文件所在位置),您只需要使用以下简单的代码:

没有对话框中断!

// Switch off any dialog boxes
displayDialogs = DialogModes.NO; // OFF

//pref pixels
app.preferences.rulerUnits = Units.PIXELS;

var newDocPath = "~/Desktop/";

// jpeg quality
var jq = 11;

// call the source document
var srcDoc = app.activeDocument;

var fileName = app.activeDocument.name;
// remove the extension (assuming it's .xxx)
var docName = fileName.substring(0,fileName.length -4);

// uncomment this for placing the jpeg in the source path
//var filePath = srcDoc.path + "\\" + docName + ".jpg";

// save jpeg at the location of newDocPath
var filePath = newDocPath + "\\" + docName + ".jpg";

// Flatten the psd
flatten_it();

// Save it out as jpeg
jpegIt(filePath, jq);

// Close it 
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

// Set Display Dialogs back to normal
displayDialogs = DialogModes.ALL; // NORMAL



// function FLATTEN IT ()
// ----------------------------------------------------------------
function flatten_it()
{
  activeDocument.flatten();
}



// function JPEG IT (file path + file name, quality)
// ----------------------------------------------------------------
function jpegIt(filePath, jpgQuality)
{
  if(! jpgQuality) jpgQuality = 12;


  // jpg file options
  var jpgFile = new File(filePath);
  jpgSaveOptions = new JPEGSaveOptions();
  jpgSaveOptions.formatOptions = FormatOptions.OPTIMIZEDBASELINE;
  jpgSaveOptions.embedColorProfile = true;
  jpgSaveOptions.matte = MatteType.NONE;
  jpgSaveOptions.quality = jpgQuality;

  activeDocument.saveAs(jpgFile, jpgSaveOptions, true, Extension.LOWERCASE);
}

你确定DialogModes.ALL是默认的吗?我认为应该是DialogModes.ERROR和.ALL将显示所有对话框。 - Sergey Kritskiy
@SergeyKritskiy 我认为DialogModes.ERROR会将所有对话框显示为正常,因为脚本失败://displayDialogs = DialogModes.NO; // OFF displayDialogs = DialogModes.ERROR; // ERROR var srcDoc = app.activeDocument; // no document - should be an error displayDialogs = DialogModes.ALL; // NORMAL - Ghoul Fool
从搜索基本上这个问题而来,将 displayDialogs = DialogModes.NO 添加到我的脚本中是我需要的答案! - ClickRick

0
如果您已经安装了ImageMagick,您可以简单地执行一个一行脚本: $convert [psd文件作为参数传递在这里].psd covert [psd名称].jpg
就这样。

谢谢您的建议,但必须使用Photoshop。我与多个工作室和客户合作,他们都使用Photoshop,我不能要求他们安装另外的软件来代替PS。 - Alexey_Berezov

0
试试这个,你可能需要再调整一下,但它可以跳过整个对话部分。
var newDocPath = "~/Desktop/";
var listOfPresetsAI = false; // When set to true the script won't save anything but instead gives you a list of pdf preset names.
var pdfProfileAI = "CMYK - Swop v2s";
var dialogOffsetX = 250; // Subtracts from the center of the screen = Center of the screen horizontally - 250px.
var dialogOffsetY = 200; // Just a static value = 200px from the top of the screen.

var o = {};
var appName = app.name;
var pdfPresetNoMatch = false;

// If Photoshop
if ( appName === "Adobe Photoshop" ) {
    dialog('jog');
}
// If Illustrator
else if ( appName === "Adobe Illustrator" ) {
    dialog('pdf');
}

function dialog(what) {
    var doc = app.activeDocument;

    var doc_path;


    try {
        doc_path = doc.path.toString();
    } catch(e) {
        doc_path = newDocPath;
    }

    var output_folder = Folder.selectDialog( 'Output folder', doc_path === "" ? newDocPath : doc_path );

    if ( output_folder != null ) {
        for ( var i = 0; i < o.formats.length; i++ ) {
            var save_options = save[ o.formats[i] ]();

            if ( o.app === 'ps' ) {
                doc.saveAs( new File( output_folder + '/' + dialog_filename ), save_options, ( o.formats[i] === 'jpg' ? true : false ), Extension.LOWERCASE );
            }
            else if ( o.app === 'ai' ) {
                doc.saveAs( new File( output_folder + '/' + dialog_filename ), save_options );
            }
        }
    }
    if(what=='jpg') {
        var jpg_Options = new JPEGSaveOptions();
        jpg_Options.embedColorProfile = true;
        jpg_Options.FormatOptions = FormatOptions.OPTIMIZEDBASELINE; // OPTIMIZEDBASELINE, PROGRESSIVE, STANDARDBASELINE
        // jpg_Options.scans = 5; // For FormatOptions.PROGRESSIVE
        jpg_Options.matte = MatteType.WHITE; // BACKGROUND, BLACK, FOREGROUND, NETSCAPE, NONE, SEMIGRAY, WHITE
        jpg_Options.quality = 11; // 0-12
        return jpg_Options;
    }

    if(what=='pdf'){
        var pdf_Options = new PDFSaveOptions();
        pdf_Options.pDFPreset = checkPresets( false, pdfProfileAI );
        return pdf_Options;        
    }
};
function checkPresets( list, testPreset ) {

    var pdfPresets = app.PDFPresetsList;

    if ( list === true ) {
        alert( "\n" + pdfPresets.join('\n') );
    }
    else {
        var preset = null;
        for ( var i = pdfPresets.length; i--; ) {
            if ( pdfPresets[i] === testPreset ) {
                preset = testPreset;
            }
        }
        pdfPresetNoMatch = (preset === null);
        return (pdfPresetNoMatch ? pdfPresets[0] : preset);
    }

}

if ( listOfPresetsAI === true ) {
    checkPresets( true );
}
else if ( app.documents.length > 0 ) {

alert( "Couldn't use your PDF preset!!! \n Used " + app.PDFPresetsList[0] + " instead." );

}

嘿,Grumpy,非常感谢您的回复。我粘贴了您的代码,但仍然收到相同的“错误23:没有值。第64行->},”提示。 - Alexey_Berezov
我认为我遇到了一个小的技术问题。可能是多打了一个括号,或者没有关闭某个函数……?我将会把完整的代码发布出来。 - Alexey_Berezov

0

我复制了Grumpy的代码,但现在出现错误23:没有值。第64行 -> },

这是新代码粘贴进去的代码:

var newDocPath = "~/Desktop/";
var listOfPresetsAI = false; // When set to true the script won't save anything but instead gives you a list of pdf preset names.
var pdfProfileAI = "CMYK - Swop v2s";
var dialogOffsetX = 250; // Subtracts from the center of the screen = Center of the screen horizontally - 250px.
var dialogOffsetY = 200; // Just a static value = 200px from the top of the screen.

var o = {};
var appName = app.name;
var pdfPresetNoMatch = false;

// If Photoshop
if ( appName === "Adobe Photoshop" ) {
    o.dialogTitle = "Save As: .jpg";
    o.formats = ['jpg'];
    o.app = 'ps';
}
// If Illustrator
else if ( appName === "Adobe Illustrator" ) {
    o.dialogTitle = "Save As: .ai and .pdf";
    o.formats = ['pdf','ai'];
    o.app = 'ai';
}

function dialog() {
            var doc = app.activeDocument;

            var doc_path;


try {
            doc_path = doc.path.toString();
        } catch(e) {
            doc_path = newDocPath;
        }

        var output_folder = Folder.selectDialog( 'Output folder', doc_path === "" ? newDocPath : doc_path );

        if ( output_folder != null ) {
            for ( var i = 0; i < o.formats.length; i++ ) {
                var save_options = save[ o.formats[i] ]();

                if ( o.app === 'ps' ) {
                    doc.saveAs( new File( output_folder + '/' + dialog_filename ), save_options, ( o.formats[i] === 'jpg' ? true : false ), Extension.LOWERCASE );
                }
                else if ( o.app === 'ai' ) {
                    doc.saveAs( new File( output_folder + '/' + dialog_filename ), save_options );
                }
            }
        }

    },
    ai: function() {

        var ai_options = new IllustratorSaveOptions();
        ai_options.flattenOutput = OutputFlattening.PRESERVEAPPEARANCE;
        return ai_options;

    },
    pdf: function() {

        var pdf_Options = new PDFSaveOptions();
        pdf_Options.pDFPreset = checkPresets( false, pdfProfileAI );
        return pdf_Options;

    },
    psd: function() {

        var psd_Options = new PhotoshopSaveOptions();
        return psd_Options;

    },
    jpg: function() {

        var jpg_Options = new JPEGSaveOptions();
        jpg_Options.embedColorProfile = true;
        jpg_Options.FormatOptions = FormatOptions.OPTIMIZEDBASELINE; // OPTIMIZEDBASELINE, PROGRESSIVE, STANDARDBASELINE
        // jpg_Options.scans = 5; // For FormatOptions.PROGRESSIVE
        jpg_Options.matte = MatteType.WHITE; // BACKGROUND, BLACK, FOREGROUND, NETSCAPE, NONE, SEMIGRAY, WHITE
        jpg_Options.quality = 11; // 0-12
        return jpg_Options;

    }
};
function checkPresets( list, testPreset ) {

    var pdfPresets = app.PDFPresetsList;

    if ( list === true ) {
        alert( "\n" + pdfPresets.join('\n') );
    }
    else {
        var preset = null;
        for ( var i = pdfPresets.length; i--; ) {
            if ( pdfPresets[i] === testPreset ) {
                preset = testPreset;
            }
        }
        pdfPresetNoMatch = (preset === null);
        return (pdfPresetNoMatch ? pdfPresets[0] : preset);
    }

}

if ( listOfPresetsAI === true ) {
    checkPresets( true );
}
else if ( app.documents.length > 0 ) {

    dialog();
    if ( pdfPresetNoMatch ) alert( "Couldn't use your PDF preset!!! \n Used " + app.PDFPresetsList[0] + " instead." );

}

我在这里缺少什么?感觉像是我不知道的技术问题。

再次感谢!

Alexey. B


我修改了我的回答。 - Grumpy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接