码迷,mamicode.com
首页 > 其他好文 > 详细

photoshop脚本切图方法(习惯快捷键的朋友可以用,很方便)

时间:2015-04-07 17:38:50      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:ps   切图   

photoshop脚本切图方法(习惯快捷键的朋友可以用,很方便)

by  伍雪颖

其实脚本切图的原理是跟安装插件切图是一样的,
不过像安装DevRocket这种插件特麻烦,还经常装不成功,于是直接用脚本.
技术分享

#target photoshop

function main(){
	var doc = app.activeDocument;
	var psdPath = app.activeDocument.path;
	var outFolderName="pngExport";
	var twosize=true;

	var outFolder = new Folder(psdPath + "/"+outFolderName);
	if (!outFolder.exists) {
	    outFolder.create();
	}

	var lname = doc.activeLayer.name;
	saveLayer(doc.activeLayer, lname, psdPath, false);	

	function saveLayer(layer, lname, path, shouldMerge) {
	    dupLayers();
	    if (shouldMerge === undefined || shouldMerge === true) {
		activeDocument.mergeVisibleLayers();
	    }
	    activeDocument.trim(TrimType.TRANSPARENT,true,true,true,true);
	    var saveFile= File(path +"/"+outFolderName+"/"+lname+"@2x.png");
	    SavePNG(saveFile);

	    if(twosize){
	    
	    var re_width=activeDocument.width*0.5;
	    var re_height=activeDocument.height*0.5;

	    activeDocument.resizeImage(re_width,re_height);
	    var saveFilesmall= File(path +"/"+outFolderName+"/"+lname+".png");
	    SavePNG(saveFilesmall);
	    
	    }
	    

	    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
	}
}


if(documents.length && !activeDocument.activeLayer.isBackgroundLayer){
	try {
            main();
        } catch (e) {
            alert(e);
        }
}

function dupLayers() { 
        var descac = new ActionDescriptor();
        var refac1 = new ActionReference();
        refac1.putClass( charIDToTypeID('Dcmn') );
	descac.putReference( charIDToTypeID('null'), refac1 );
        descac.putString( charIDToTypeID('Nm  '), activeDocument.activeLayer.name );
        var refac2 = new ActionReference();
        refac2.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
        descac.putReference( charIDToTypeID('Usng'), refac2 );
        executeAction( charIDToTypeID('Mk  '), descac, DialogModes.NO );
}

function SavePNG(saveFile){
    var pngOpts = new ExportOptionsSaveForWeb; 
    pngOpts.format = SaveDocumentType.PNG
    pngOpts.PNG8 = false; 
    pngOpts.transparency = true; 
    pngOpts.interlaced = false; 
    pngOpts.quality = 100;
    activeDocument.exportDocument(new File(saveFile),ExportType.SAVEFORWEB,pngOpts); 
}


photoshop脚本切图方法(习惯快捷键的朋友可以用,很方便)

标签:ps   切图   

原文地址:http://blog.csdn.net/rainlesvio/article/details/44922497

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!