How to write text to a text file by Photoshop JavaScript?

前端 未结 5 821
隐瞒了意图╮
隐瞒了意图╮ 2021-02-07 17:50

I took a look at Photoshop CS5 Scripting Guide and Photoshop CS5 JavaScript Reference, but I couldn\'t find out a method to write text to a plain text file. Is there any way to

5条回答
  •  南笙
    南笙 (楼主)
    2021-02-07 18:05

    This works for me, saves text with the same name as original document, but with extension txt:

    function saveTxt(txt)
    {
        var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
        var Ext = decodeURI(app.activeDocument.name).replace(/^.*\./,'');
        if (Ext.toLowerCase() != 'psd')
            return;
    
        var Path = app.activeDocument.path;
        var saveFile = File(Path + "/" + Name +".txt");
    
        if(saveFile.exists)
            saveFile.remove();
    
        saveFile.encoding = "UTF8";
        saveFile.open("e", "TEXT", "????");
        saveFile.writeln(txt);
        saveFile.close();
    }
    

    I don't know how it works, photoshop scripting is a huge mess, I just kept mixing together a few scripts that I found until it worked.

    Also, if anyone needs this, here is a script that also saves active document as png image:

    function savePng()
    {
        var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
        var Ext = decodeURI(app.activeDocument.name).replace(/^.*\./,'');
        if (Ext.toLowerCase() != 'psd')
            return;
    
        var Path = app.activeDocument.path;
        var saveFile = File(Path + "/" + Name +".png");
    
        if(saveFile.exists)
            saveFile.remove();
    
        var o = new ExportOptionsSaveForWeb();
            o.format = SaveDocumentType.PNG;
            o.PNG8 = false;
            o.transparency = true;
            o.interlaced = false;
            o.includeProfile = false;
        activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, o);
    }
    

提交回复
热议问题