Photoshop脚本:Relink智能对象



我正在编写一个脚本,该脚本应该通过photoshop文档并将所有可见的链接对象重新链接到新的指定文件。我已经让循环工作了,它循环遍历每一层,只收集可见的层,但对于我的生命,我找不到是否有一种方法可以重新链接智能对象。我发现最接近的是这个脚本:

https://gist.github.com/laryn/0a1f6bf0dab5b713395a835f9bfa805c

但是当它到达desc3.putPath(idnull, new File(newFile));时,它吐出一个错误,表明该功能可能不存在于当前的Photoshop版本中。脚本本身已经有4年的历史了,所以它可能已经过时了。

任何帮助将不胜感激!

脚本如下:

// SELECT FILE //
var files = File.openDialog("Please select new linked file");
var selectedFile = files[0];
// GET ALL LAYERS //
var doc = app.activeDocument;
var allLayers = [];
var allLayers = collectAllLayers(doc, allLayers);
function collectAllLayers (doc, allLayers)
{
for (var m = 0; m < doc.layers.length; m++)
{
var theLayer = doc.layers[m];

if (theLayer.typename === "ArtLayer")
{
allLayers.push(theLayer);
}
else
{
collectAllLayers(theLayer, allLayers);
}
}
return allLayers;
}
// GET VISIBLE LAYERS //
var visibleLayers = [];
for (i = 0; i < allLayers.length; i++)
{
var layer = allLayers[i];

if (layer.visible && layer.kind == LayerKind.SMARTOBJECT)
{
visibleLayers.push(layer);
}
}
// REPLACE LAYERS
for (i = 0; i < visibleLayers.length; i++)
{
var layer = visibleLayers[i];

//--> REPLACE THE FILE HERE 
}

注意:我知道这个脚本目前可能是容易出错的,如果你不知道它是如何工作的;我现在不打算发表它,所以我现在不太关心这个。大多数情况下,我只需要核心功能的工作。

我使用AM函数来获得可见的智能对象-它的工作速度要快得多。但如果你愿意,你也可以用你的。重要的一点是relinkSO(path);:它也会在你的脚本中工作(只是不要忘记选择一个图层:activeDocument.activeLayer = visibleLayers[i];)

注意,它的工作原理类似于PhotoshopRelink to File命令-如果在智能对象的一个实例上使用,所有的实例将被重新链接。如果你只想重新链接特定的层,你必须首先打破实例化(可能使用New Smart Object via Copy命令)

function main() {
var myFile = Folder.myDocuments.openDlg('Load file', undefined, false);
if (myFile == null) return false;

// gets IDs of all smart objects
var lyrs = getLyrs();
for (var i = 0; i < lyrs.length; i++) {
// for each SO id...
// select it
selectById(lyrs[i]);
// relink SO to file
relinkSO(myFile);
// embed linked if you want
embedLinked()
}
function getLyrs() {
var ids = [];
var layers, desc, vis, type, id;
try
{
activeDocument.backgroundLayer;
layers = 0;
}
catch (e)
{
layers = 1;
}
while (true)
{
ref = new ActionReference();
ref.putIndex(charIDToTypeID('Lyr '), layers);
try
{
desc = executeActionGet(ref);
}
catch (err)
{
break;
}
vis = desc.getBoolean(charIDToTypeID("Vsbl"));
type = desc.getInteger(stringIDToTypeID("layerKind"));
id = desc.getInteger(stringIDToTypeID("layerID"));
if (type == 5 && vis) ids.push(id);
layers++;
}
return ids;
} // end of getLyrs()
function selectById(id) {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID('Lyr '), id);
desc.putReference(charIDToTypeID('null'), ref);
executeAction(charIDToTypeID('slct'), desc, DialogModes.NO);
} // end of selectById()

function relinkSO(path) {
var desc = new ActionDescriptor();
desc.putPath( charIDToTypeID('null'), new File( path ) );
executeAction( stringIDToTypeID('placedLayerRelinkToFile'), desc, DialogModes.NO );
} // end of relinkSO()
function embedLinked() {
executeAction( stringIDToTypeID('placedLayerConvertToEmbedded'), undefined, DialogModes.NO );
} // end of embedLinked()

}
app.activeDocument.suspendHistory("relink SOs", "main()");

最新更新