【Photoshop JSX脚本】交换两个图层位置

选中两个图层,交换图层位置

// swapPosition-mirror.jsx - Adobe Photoshop Script  
// Version: 0.2.1  
// Author: Anton Lyubushkin (nvkz.nemo@gmail.com)  
// Website: http://lyubushkin.pro/  
// ============================================================================  
// Installation:  
// 1. Place script in:  
//    PC(32):  C:\Program Files (x86)\Adobe\Adobe Photoshop CC#\Presets\Scripts\  
//    PC(64):  C:\Program Files\Adobe\Adobe Photoshop CC# (64 Bit)\Presets\Scripts\  
//    Mac:     <hard drive>/Applications/Adobe Photoshop CC#/Presets/Scripts/  
// 2. Restart Photoshop  
// 3. Choose File > Scripts > swapPosition-mirror  
// ============================================================================  
 
//#target photoshop  
  
app.bringToFront();  
  
var doc = app.activeDocument;  
var selectedLayers = getSelectedLayersIndex(doc);  
  
if (selectedLayers.length == 2) {  
    try {  
        for (var i = 0; selectedLayers.length > i; i++) {  
  
            var sLayers = new Array();  
            for (var i = 0, l = selectedLayers.length; i < l; i++) {  
                selectLayerByIndex(selectedLayers[i], false);  
                if (app.activeDocument.activeLayer.typename == "LayerSet") {  
                    executeAction(stringIDToTypeID("newPlacedLayer"), undefined, DialogModes.NO);  
                    var xpos = activeDocument.activeLayer.bounds[0];  
                    var ytop = activeDocument.activeLayer.bounds[1];  
                    var ybot = activeDocument.activeLayer.bounds[3];  
                  var xleft = activeDocument.activeLayer.bounds[0]  
                  var xright = activeDocument.activeLayer.bounds[2]  
                    var height = activeDocument.activeLayer.bounds[3] - activeDocument.activeLayer.bounds[1];  
                    app.activeDocument.activeHistoryState = app.activeDocument.historyStates[app.activeDocument.historyStates.length - 2];  
                    selectLayerByIndex(selectedLayers[i], false);  
                } else {  
                    var xpos = activeDocument.activeLayer.bounds[0];  
                    var ytop = activeDocument.activeLayer.bounds[1];  
                    var ybot = activeDocument.activeLayer.bounds[3];  
                    var height = activeDocument.activeLayer.bounds[3] - activeDocument.activeLayer.bounds[1];  
                  var xleft = activeDocument.activeLayer.bounds[0]  
                  var xright = activeDocument.activeLayer.bounds[2]  
                }  
  
                sLayers.push([activeDocument.activeLayer, xpos, ytop, ybot, height, xleft, xright]);  
            }  
  
            var delta0_y = 0;  
            var delta1_y = 0;  
  
            function differentHeight() {  
                if (sLayers[1][4] != sLayers[0][4]) {  
                    return true  
                } else {  
                    return false  
                }  
            }  
  
            function returnSmaller() {  
                if (sLayers[1][4] < sLayers[0][4]) {  
                    return [sLayers[1][0], sLayers[0][0]]  
                } else {  
                    return [sLayers[0][0], sLayers[1][0]]  
                }  
            }  
  
            if (sLayers[1][2] != sLayers[0][2]) {  
                if (differentHeight() == true) {  
                    var smallLyr = returnSmaller();  
                    if (smallLyr[0].bounds[1] < smallLyr[1].bounds[1] || smallLyr[0].bounds[3] > smallLyr[1].bounds[3]) {  
                        delta0_y = (sLayers[1][3] - ((sLayers[1][3] - sLayers[1][2]))) - (sLayers[0][3] - ((sLayers[0][3] - sLayers[0][2])));  
                        delta1_y = (sLayers[0][3] - ((sLayers[0][3] - sLayers[0][2]))) - (sLayers[1][3] - ((sLayers[1][3] - sLayers[1][2])));  
                    } else {  
                        delta0_y = 0;  
                        delta1_y = 0;  
                    }  
                } else {  
                    delta0_y = (sLayers[1][3] - ((sLayers[1][3] - sLayers[1][2]))) - (sLayers[0][3] - ((sLayers[0][3] - sLayers[0][2])));  
                    delta1_y = (sLayers[0][3] - ((sLayers[0][3] - sLayers[0][2]))) - (sLayers[1][3] - ((sLayers[1][3] - sLayers[1][2])));  
                }  
            }  
            
          if (sLayers[0][5] < sLayers[1][5] && sLayers[0][6] < sLayers[1][6]) {  
            var delta0_x = sLayers[1][6] - sLayers[0][6];  
            var delta1_x = sLayers[0][5] - sLayers[1][5];  
          } else {  
            var delta0_x = sLayers[1][5] - sLayers[0][5];  
            var delta1_x = sLayers[0][6] - sLayers[1][6];  
          }  
              
  
            sLayers[0][0].translate(delta0_x, delta0_y);  
            sLayers[1][0].translate(delta1_x, delta1_y);  
  
            for (var i = 0, l = selectedLayers.length; i < l; i++) {  
                selectLayerByIndex(selectedLayers[i], true);  
            }  
  
        }  
    } catch (e) {  
        alert("Oops! These layers cannot be repositioned");  
    }  
}  
  
function getSelectedLayersIndex(doc) {  
    var selectedLayers = [];  
    var ref = new ActionReference();  
    ref.putEnumerated(cTID('Dcmn'), cTID('Ordn'), cTID('Trgt'));  
    var desc = executeActionGet(ref);  
    if (desc.hasKey(sTID('targetLayers'))) {  
        desc = desc.getList(sTID('targetLayers'));  
        var c = desc.count;  
        for (var i = 0; i < c; i++) {  
            try {  
                doc.backgroundLayer;  
                selectedLayers.push(desc.getReference(i).getIndex());  
            } catch (e) {  
                selectedLayers.push(desc.getReference(i).getIndex() + 1);  
            }  
        }  
    } else {  
        var ref = new ActionReference();  
        ref.putProperty(cTID('Prpr'), cTID('ItmI'));  
        ref.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));  
        try {  
            doc.backgroundLayer;  
            selectedLayers.push(executeActionGet(ref).getInteger(cTID('ItmI')) - 1);  
        } catch (e) {  
            selectedLayers.push(executeActionGet(ref).getInteger(cTID('ItmI')));  
        }  
    }  
    return selectedLayers;  
}  
  
function selectLayerByIndex(index, add) {  
    var ref = new ActionReference();  
    ref.putIndex(charIDToTypeID("Lyr "), index);  
    var desc = new ActionDescriptor();  
    desc.putReference(charIDToTypeID("null"), ref);  
    if (add) desc.putEnumerated(stringIDToTypeID("selectionModifier"), stringIDToTypeID("selectionModifierType"), stringIDToTypeID("addToSelection"));  
    desc.putBoolean(charIDToTypeID("MkVs"), false);  
    try {  
        executeAction(charIDToTypeID("slct"), desc, DialogModes.NO);  
    } catch (e) {}  
}  
  
function cTID(s) {return app.charIDToTypeID(s);}  
function sTID(s) {return app.stringIDToTypeID(s);}  

 

PhotoshopJSX脚本是基于JavaScript的一种脚本语言,它可以用来自动化Photoshop中的各种操作。通过JSX脚本,你可以编写代码来创建图层图层组,甚至导入图片到Photoshop中。以下是一个简单的JSX脚本示例,它将演示如何通过一个图片文件创建一个图层,并将该图层放入一个新创建的图层组中: ```javascript // 确保Photoshop中已打开文档 if (app.documents.length > 0) { var doc = app.activeDocument; // 获取当前活动文档 // 图片路径,请根据实际情况修改 var imagePath = "C:\\path\\to\\your\\image.jpg"; // 创建一个新的图层组 var layerGroup = doc.artLayers.add(); layerGroup.name = "My Layer Group"; // 设置图层组名称 // 通过图片创建图层 var newLayer = layerGroup.layers.add(); newLayer.kind = LayerKind.NORMAL; // 设置图层类型为普通图层 newLayer.name = "My New Layer"; // 设置图层名称 // 将图片放置到新图层上,需要将图片路径赋值给图层的property newLayer.property("image", new File(imagePath)); // 可选操作:调整图层位置或大小,这里以调整到文档中心为例 var bounds = newLayer.bounds; var x = (doc.width / 2) - (bounds.width / 2); var y = (doc.height / 2) - (bounds.height / 2); newLayer.move([x, y], ElementPlacement.PLACEATBEGINNING); } else { alert("请先打开一个Photoshop文档。"); } ``` 在上述脚本中,首先检查Photoshop是否已经打开了一个文档,然后创建一个新的图层组和一个普通图层。通过设置图层的`property`属性,将外部图片文件导入到新创建的图层中。 注意:在实际使用中,路径`imagePath`需要根据你的图片实际位置进行修改。另外,由于不同版本的Photoshop脚本API可能有所差异,上述代码可能需要根据你所使用的Photoshop版本进行适当的调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值