我已经在Javascript中为photoshop编写了一个脚本,它可以获取用户输入(桌面上的盒子数量),然后将其转换为像素大小。问题在于调整大小,图层不会更改为该数量,它会更改该数量。例如。 microsoftbox从800 * 800开始,然后应该更改为500 * 500但是将其加到等于1300 * 1300。
app.preferences.rulerUnits = Units.PIXELS;
//Get all input
var microsoftboxes = prompt("How many boxes for microsoft?");
var gamesboxesx = prompt("How many across boxes for games?");
var gamesboxesy = prompt("How many down boxes for games?");
var adobeboxes = prompt("How many boxes for adobe?");
var filesboxes = prompt("How many boxes for files?");
var toolsboxes = prompt("How many boxes for tools?");
var recycleboxes = 1;
//Add percentage on and convert to pixel num
var mb = microsoftboxes * 75;
mb = mb + (mb * 0.04);
var gbx = gamesboxesx * 75;
gbx = gbx + (gbx * 0.04);
var gby = gamesboxesy * 75;
gby = gby + (gby * 0.04);
var ab = adobeboxes * 75;
ab = ab + (ab * 0.04);
var fb = filesboxes * 75;
fb = fb + (fb * 0.04);
var tb = toolsboxes * 75;
tb = tb + (tb * 0.04);
var rb = recycleboxes * 75;
rb = rb + (rb * 0.04);
//vars for change size
var doc = app.activeDocument;
var m = doc.layers.getByName('microsoft');
var g = doc.layers.getByName('games');
var a = doc.layers.getByName('adobe');
var t = doc.layers.getByName('tools');
var f = doc.layers.getByName('files');
var r = doc.layers.getByName('recycle');
m.resize(mb, 73, AnchorPosition.MIDDLELEFT);
g.resize(gbx, gby, AnchorPosition.TOPCENTER);
a.resize(ab, 73, AnchorPosition.MIDDLELEFT);
f.resize(fb, 73, AnchorPosition.MIDDLELEFT);
t.resize(tb, 73, AnchorPosition.MIDDLELEFT);
r.resize(rb, 73, AnchorPosition.MIDDLELEFT);
--- --- UPDATE 我已经设法解决它尽管被设置为像素,但按百分比变化。例如。文件应该已设置为78,但它已设置为
250 (the current value) * 0.78(78 as a percentage) = 195
文件被设置为195像素。我能解决这个问题吗?
现在代码:
var startru = app.preferences.rulerUnits;
var starttu = app.preferences.typeUnits;
app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.PIXELS;
//Get all input
var microsoftboxes = prompt("How many boxes for microsoft?");
var gamesboxesx = prompt("How many across boxes for games?");
var gamesboxesy = prompt("How many down boxes for games?");
var adobeboxes = prompt("How many boxes for adobe?");
var filesboxes = prompt("How many boxes for files?");
var toolsboxes = prompt("How many boxes for tools?");
var recycleboxes = 1;
//Add percentage on and convert to pixel num
var mb = microsoftboxes * 75;
mb = mb + (mb * 0.04);
var gbx = gamesboxesx * 75;
gbx = gbx + (gbx * 0.04);
var gby = gamesboxesy * 75;
gby = gby + (gby * 0.04);
var ab = adobeboxes * 75;
ab = ab + (ab * 0.04);
var fb = filesboxes * 75;
fb = fb + (fb * 0.04);
var tb = toolsboxes * 75;
tb = tb + (tb * 0.04);
var rb = recycleboxes * 75;
rb = rb + (rb * 0.04);
//vars for change size
var doc = app.activeDocument;
var m = doc.layers.getByName('microsoft');
var g = doc.layers.getByName('games');
var a = doc.layers.getByName('adobe');
var t = doc.layers.getByName('tools');
var f = doc.layers.getByName('files');
var r = doc.layers.getByName('recycle');
m.resize(mb, 85, AnchorPosition.MIDDLELEFT);
g.resize(gbx, gby, AnchorPosition.TOPCENTER);
a.resize(ab, 85, AnchorPosition.MIDDLELEFT);
f.resize(fb, 85, AnchorPosition.MIDDLELEFT);
t.resize(tb, 85, AnchorPosition.MIDDLELEFT);
r.resize(rb, 85, AnchorPosition.MIDDLELEFT);
app.preferences.rulerUnits = startru;
app.preferences.typeUnits = starttu;
答案 0 :(得分:0)
如我的评论中所述,ArtLayer对象的resize方法明确地期望百分比值,并且不能接受用于调整大小的像素值。我确定有一个公式可以使用当前图层的大小来确定获得新的特定大小所需的百分比,但是我认为阻力最小的途径是
下面是我为完成该任务而编写的示例。它不是解决问题的复制/粘贴解决方案,而是使用您要做什么的想法进行说明。我认为修改此代码以满足您的确切需求应该相当容易。
app.preferences.rulerUnits = Units.PIXELS;
var activeDoc = app.activeDocument;
//store the active document's resolution for when we create the temp doc
var res = activeDoc.resolution;
//capture the existing color mode for the temp doc
var newDocMode;
switch(activeDoc.mode) {
case DocumentMode.RGB:
newDocMode = NewDocumentMode.RGB;
break;
case DocumentMode.CMYK:
newDocMode = NewDocumentMode.CMYK;
break;
}
//my preference to store sizes as a JSON object
var msboxSizes = {
sm: {
width: 200,
height: 200
},
md: {
width: 500,
height: 500
},
lg: {
width: 800,
height: 800
}
};
//target our layer
var msboxLayer = activeDoc.artLayers.getByName("microsoftbox");
//make it the active layer
activeDoc.activeLayer = msboxLayer;
//get the bounds of the layer so we can create a new doc the same size as the image on this layer
var msboxSize = activeDoc.activeLayer.bounds;
//use the bounds to determine the width and height of the layer
var msboxWidth = (msboxSize[2].value - msboxSize[0].value).toFixed(0);
var msboxHeight = (msboxSize[3].value - msboxSize[1].value).toFixed(0);
//convert those values back into pixels
msboxWidth = new UnitValue(msboxWidth, "px");
msboxHeight = new UnitValue(msboxHeight, "px");
//copy our layer to the clipboard
msboxLayer.copy();
//create a new doc that is the same size, resolution and color mode as the layer we want to resize
var tempDoc = app.documents.add(msboxWidth, msboxHeight, res, "Temp Doc", newDocMode, DocumentFill.TRANSPARENT);
//set the temp doc as the active document
app.activeDocument = tempDoc;
//paste the layer into the temp doc
tempDoc.paste();
//store the different size obj keys in an array (could use array of objects and skip this bit)
var resizes = ["lg","md","sm"]
//loop through resizing to each size in the array, copy and paste them as layers into the original document
for (i = 0; i < resizes.length; i++) {
var newWidth = msboxSizes[resizes[i]].width;
var newHeight = msboxSizes[resizes[i]].height;
app.activeDocument = tempDoc;
tempDoc.resizeImage(newWidth, newHeight, res, ResampleMethod.BICUBIC);
tempDoc.activeLayer.copy();
app.activeDocument = activeDoc;
activeDoc.paste();
}
//close the temp doc and dont save it
tempDoc.close(SaveOptions.DONOTSAVECHANGES);