Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
graphic:javascript:photoshop [2023/03/05 02:57] – [Doc operation code] ying | graphic:javascript:photoshop [2023/03/05 03:02] (current) – [Doc operation code] ying | ||
---|---|---|---|
Line 287: | Line 287: | ||
</ | </ | ||
* resize doc <code javascript> | * resize doc <code javascript> | ||
+ | * resize to A4 size ratio <code javascript> | ||
+ | /* | ||
+ | reisize to fit A4 | ||
+ | by ying n chatGPT | ||
+ | 2023.03.05 | ||
+ | */ | ||
+ | // Get the active document | ||
+ | var doc = app.activeDocument; | ||
+ | app.backgroundColor.rgb.red = 255; | ||
+ | app.backgroundColor.rgb.green = 255; | ||
+ | app.backgroundColor.rgb.blue = 255; | ||
+ | // Define the A4 size in pixels (assuming 300 DPI) | ||
+ | var a4Width = 2480; // 8.27 inches * 300 pixels per inch | ||
+ | var a4Height = 3508; // 11.69 inches * 300 pixels per inch | ||
+ | var a4Ratio = a4Width / a4Height; | ||
+ | |||
+ | // Get the current canvas size | ||
+ | var currentWidth = doc.width.value; | ||
+ | var currentHeight = doc.height.value; | ||
+ | var currentRatio = currentWidth / currentHeight; | ||
+ | |||
+ | // Calculate the new canvas size | ||
+ | var newWidth = currentWidth; | ||
+ | var newHeight = currentHeight; | ||
+ | if (currentRatio > a4Ratio) { | ||
+ | // Extend the height to match the A4 ratio | ||
+ | newHeight = Math.round(newWidth / a4Ratio); | ||
+ | } else { | ||
+ | // Extend the width to match the A4 ratio | ||
+ | newWidth = Math.round(newHeight * a4Ratio); | ||
+ | } | ||
+ | |||
+ | // Only resize the canvas if the new size is larger than the current size | ||
+ | if (newWidth > currentWidth || newHeight > currentHeight) { | ||
+ | // Calculate the position of the current content | ||
+ | var x = Math.round((newWidth - currentWidth) / 2); | ||
+ | var y = Math.round((newHeight - currentHeight) / 2); | ||
+ | |||
+ | // Resize the canvas | ||
+ | doc.resizeCanvas(newWidth, | ||
+ | } | ||
+ | |||
+ | </ | ||
* file and folder <code javascript> | * file and folder <code javascript> | ||
var samplesFolder = Folder(app.path + "/ | var samplesFolder = Folder(app.path + "/ | ||
Line 323: | Line 366: | ||
</ | </ | ||
* print each layer <code javascript> | * print each layer <code javascript> | ||
- | /* | + | var msg = "Make sure you set your print setting correct and done,\nthen click Yes to run batch print layer" |
- | reisize | + | if (confirm(msg)) { |
- | by ying n chatGPT | + | // Get the current |
- | 2023.03.05 | + | var doc = app.activeDocument; |
- | */ | + | |
- | // Get the active document | + | |
- | var doc = app.activeDocument; | + | |
- | app.backgroundColor.rgb.red = 255; | + | |
- | app.backgroundColor.rgb.green = 255; | + | |
- | app.backgroundColor.rgb.blue = 255; | + | |
- | // Define the A4 size in pixels (assuming 300 DPI) | + | |
- | var a4Width = 2480; // 8.27 inches * 300 pixels per inch | + | |
- | var a4Height = 3508; // 11.69 inches * 300 pixels per inch | + | |
- | var a4Ratio = a4Width / a4Height; | + | |
- | // Get the current canvas size | + | |
- | var currentWidth | + | var layers |
- | var currentHeight = doc.height.value; | + | for (var i = 0; i < doc.layers.length; i++) { |
- | var currentRatio | + | if(!doc.layers[i].isBackgroundLayer){ |
+ | layers.push(doc.layers[i]); | ||
+ | } | ||
+ | } | ||
+ | // Hide all the layers | ||
+ | for (var i = 0; i < layers.length; | ||
+ | layers[i].visible = false; | ||
+ | } | ||
- | // Calculate the new canvas size | + | |
- | var newWidth | + | for (var i = 0; i < layers.length; |
- | var newHeight | + | // Show the current layer |
- | if (currentRatio > a4Ratio) { | + | layers[i].visible |
- | // Extend | + | |
- | | + | // Print the current layer |
+ | app.activeDocument.print(); | ||
+ | //alert(' | ||
+ | |||
+ | // Hide the current layer | ||
+ | layers[i].visible = false; | ||
+ | } | ||
+ | |||
+ | // Show all the layers again | ||
+ | for (var i = 0; i < layers.length; i++) { | ||
+ | layers[i].visible = true; | ||
+ | } | ||
} else { | } else { | ||
- | | + | |
- | newWidth = Math.round(newHeight * a4Ratio); | + | |
} | } | ||
+ | </ | ||
+ | * split image vertically equally into N part, and stack into new doc, optionally crop border pixel <code javascript> | ||
+ | // | ||
+ | //# image split by n | ||
+ | // | ||
+ | var doc = app.activeDocument; | ||
- | // Only resize | + | // Prompt |
- | if (newWidth > currentWidth || newHeight > currentHeight) { | + | var n = parseInt(prompt("Enter the number of parts to split the image into:", |
- | // Calculate the position | + | |
- | var x = Math.round((newWidth - currentWidth) / 2); | + | |
- | var y = Math.round((newHeight - currentHeight) / 2); | + | |
- | | + | // Calculate |
- | doc.resizeCanvas(newWidth, newHeight, AnchorPosition.MIDDLECENTER); | + | var partHeight = Math.floor(doc.height / n); |
+ | |||
+ | // Create a new document with the width of the original image and the height of one part | ||
+ | var newDoc = app.documents.add(doc.width, | ||
+ | |||
+ | // Loop through each part of the image | ||
+ | for (var i = 0; i < n; i++) { | ||
+ | | ||
+ | var sourceRect = [ | ||
+ | [0, i * partHeight], | ||
+ | [doc.width, i * partHeight], | ||
+ | [doc.width, | ||
+ | [0, (i + 1) * partHeight] | ||
+ | ]; | ||
+ | |||
+ | // Activate the source document | ||
+ | app.activeDocument = doc; | ||
+ | // Deselect any active selection | ||
+ | doc.selection.deselect(); | ||
+ | // Create a new selection | ||
+ | doc.selection.select(sourceRect); | ||
+ | // Copy the part to the new document | ||
+ | doc.selection.copy(); | ||
+ | // Activate the new document | ||
+ | app.activeDocument = newDoc; | ||
+ | // Paste the part into the new document | ||
+ | newDoc.paste(); | ||
+ | // Move the selection down by the height of one part | ||
+ | app.activeDocument = doc; | ||
+ | doc.selection.translateBoundary(0, | ||
} | } | ||
+ | // | ||
+ | //# trim border | ||
+ | // | ||
+ | app.activeDocument = newDoc; | ||
+ | newDoc.trim(TrimType.TOPLEFT, | ||
</ | </ | ||
===== Execute code ===== | ===== Execute code ===== |