diff --git a/.prettierrc b/.prettierrc index cce9d3c..e74ed9f 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,3 +1,6 @@ { - "semi": false + "trailingComma": "es5", + "tabWidth": 4, + "semi": false, + "singleQuote": true } diff --git a/dialog_box.js b/dialog_box.js index 5d38ec1..1dc465f 100644 --- a/dialog_box.js +++ b/dialog_box.js @@ -1,53 +1,56 @@ async function prompt( - heading, - body, - buttons = ["Cancel", "Ok"], - options = { title: heading, size: { width: 360, height: 280 } } + heading, + body, + buttons = ['Cancel', 'Ok'], + options = { title: heading, size: { width: 360, height: 280 } } ) { - const [dlgEl, formEl, headingEl, dividerEl, bodyEl, footerEl] = [ - "dialog", - "form", - "sp-heading", - "sp-divider", - "sp-body", - "footer", - ].map((tag) => document.createElement(tag)) - ;[headingEl, dividerEl, bodyEl, footerEl].forEach((el) => { - el.style.margin = "6px" - el.style.width = "calc(100% - 12px)" - }) + const [dlgEl, formEl, headingEl, dividerEl, bodyEl, footerEl] = [ + 'dialog', + 'form', + 'sp-heading', + 'sp-divider', + 'sp-body', + 'footer', + ].map((tag) => document.createElement(tag)) + ;[headingEl, dividerEl, bodyEl, footerEl].forEach((el) => { + el.style.margin = '6px' + el.style.width = 'calc(100% - 12px)' + }) - formEl.setAttribute("method", "dialog") - formEl.addEventListener("submit", () => dlgEl.close()) + formEl.setAttribute('method', 'dialog') + formEl.addEventListener('submit', () => dlgEl.close()) - footerEl.style.marginTop = "26px" + footerEl.style.marginTop = '26px' - dividerEl.setAttribute("size", "large") + dividerEl.setAttribute('size', 'large') - headingEl.textContent = heading + headingEl.textContent = heading - bodyEl.textContent = body + bodyEl.textContent = body - buttons.forEach((btnText, idx) => { - const btnEl = document.createElement("sp-button") - btnEl.setAttribute( - "variant", - idx === buttons.length - 1 ? btnText.variant || "cta" : "secondary" + buttons.forEach((btnText, idx) => { + const btnEl = document.createElement('sp-button') + btnEl.setAttribute( + 'variant', + idx === buttons.length - 1 ? btnText.variant || 'cta' : 'secondary' + ) + if (idx === buttons.length - 1) + btnEl.setAttribute('autofocus', 'autofocus') + if (idx < buttons.length - 1) btnEl.setAttribute('quiet') + btnEl.textContent = btnText.text || btnText + btnEl.style.marginLeft = '12px' + btnEl.addEventListener('click', () => + dlgEl.close(btnText.text || btnText) + ) + footerEl.appendChild(btnEl) + }) + ;[headingEl, dividerEl, bodyEl, footerEl].forEach((el) => + formEl.appendChild(el) ) - if (idx === buttons.length - 1) btnEl.setAttribute("autofocus", "autofocus") - if (idx < buttons.length - 1) btnEl.setAttribute("quiet") - btnEl.textContent = btnText.text || btnText - btnEl.style.marginLeft = "12px" - btnEl.addEventListener("click", () => dlgEl.close(btnText.text || btnText)) - footerEl.appendChild(btnEl) - }) - ;[headingEl, dividerEl, bodyEl, footerEl].forEach((el) => - formEl.appendChild(el) - ) - dlgEl.appendChild(formEl) - document.body.appendChild(dlgEl) + dlgEl.appendChild(formEl) + document.body.appendChild(dlgEl) - return dlgEl.uxpShowModal(options) + return dlgEl.uxpShowModal(options) } // const r1 = await prompt( diff --git a/export_png.js b/export_png.js index 1db811c..87393b7 100644 --- a/export_png.js +++ b/export_png.js @@ -1,121 +1,121 @@ -const { executeAsModal } = require("photoshop").core -const storage = require("uxp").storage +const { executeAsModal } = require('photoshop').core +const storage = require('uxp').storage const fs = storage.localFileSystem -const formats = require("uxp").storage.formats +const formats = require('uxp').storage.formats async function exportPngCommand() { - const batchPlay = require("photoshop").action.batchPlay - // const result = await batchPlay { _obj: “exportSelectionAsFileTypePressed”} + const batchPlay = require('photoshop').action.batchPlay + // const result = await batchPlay { _obj: “exportSelectionAsFileTypePressed”} - // const destFolder = (await storage.localFileSystem.getDataFolder()).nativePath; - const storage = require("uxp").storage - const fs = storage.localFileSystem + // const destFolder = (await storage.localFileSystem.getDataFolder()).nativePath; + const storage = require('uxp').storage + const fs = storage.localFileSystem - let pluginFolder = await fs.getPluginFolder() - // await fs.getFolder("./init_images") - let init_images_dir = await pluginFolder.getEntry( - "./server/python_server/init_images" - ) + let pluginFolder = await fs.getPluginFolder() + // await fs.getFolder("./init_images") + let init_images_dir = await pluginFolder.getEntry( + './server/python_server/init_images' + ) - const exportCommand = { - _obj: "exportSelectionAsFileTypePressed", - _target: { _ref: "layer", _enum: "ordinal", _value: "targetEnum" }, - fileType: "png", - quality: 32, - metadata: 0, - destFolder: init_images_dir.nativePath, - sRGB: true, - openWindow: false, - _options: { dialogOptions: "dontDisplay" }, - } - const result = await batchPlay([exportCommand], { - synchronousExecution: true, - modalBehavior: "execute", - }) + const exportCommand = { + _obj: 'exportSelectionAsFileTypePressed', + _target: { _ref: 'layer', _enum: 'ordinal', _value: 'targetEnum' }, + fileType: 'png', + quality: 32, + metadata: 0, + destFolder: init_images_dir.nativePath, + sRGB: true, + openWindow: false, + _options: { dialogOptions: 'dontDisplay' }, + } + const result = await batchPlay([exportCommand], { + synchronousExecution: true, + modalBehavior: 'execute', + }) - return result + return result } async function exportPng() { - const { executeAsModal } = require("photoshop").core - try { - await executeAsModal(exportPngCommand) - } catch (e) { - console.warn("exportPng error:", e) - } + const { executeAsModal } = require('photoshop').core + try { + await executeAsModal(exportPngCommand) + } catch (e) { + console.warn('exportPng error:', e) + } } const readPng = async (image_name) => { - // image_name = 'test.png' - try { - await executeAsModal( - async (control) => { - // const tempFolder = await fs.getTemporaryFolder() ; - const pluginFolder = await fs.getPluginFolder() + // image_name = 'test.png' + try { + await executeAsModal( + async (control) => { + // const tempFolder = await fs.getTemporaryFolder() ; + const pluginFolder = await fs.getPluginFolder() - let init_images_dir = await pluginFolder.getEntry( - "./server/python_server/init_images" + let init_images_dir = await pluginFolder.getEntry( + './server/python_server/init_images' + ) + // let init_images_dir = await pluginFolder.getEntry( + // './server/python_server/init_images' + // ) + const file = await init_images_dir.createFile(image_name, { + overwrite: true, + }) + + const currentDocument = app.activeDocument + await currentDocument.saveAs.png( + file, + { + compression: 6, + }, + true + ) + + // const arrayBuffer = await file.read({format: formats.binary}) ; + // console.log(arrayBuffer, 'arrayBuffer') ; + }, + + { commandName: 'readPng' } ) - // let init_images_dir = await pluginFolder.getEntry( - // './server/python_server/init_images' - // ) - const file = await init_images_dir.createFile(image_name, { - overwrite: true, - }) - - const currentDocument = app.activeDocument - await currentDocument.saveAs.png( - file, - { - compression: 6, - }, - true - ) - - // const arrayBuffer = await file.read({format: formats.binary}) ; - // console.log(arrayBuffer, 'arrayBuffer') ; - }, - - { commandName: "readPng" } - ) - } catch (e) { - console.warn(e) - } + } catch (e) { + console.warn(e) + } } -const psapi = require("./psapi") +const psapi = require('./psapi') async function newExportPng(layer, image_name) { - //store layers we want to export in variables - // let layerToExports = - // create new document - // duplicate the layers to the new documnet - //select the layer channel selectLayerChannelCommand - //document.crop - //export using readPng() + //store layers we want to export in variables + // let layerToExports = + // create new document + // duplicate the layers to the new documnet + //select the layer channel selectLayerChannelCommand + //document.crop + //export using readPng() - try { - //get the active layers - // const layersToExport = app.activeDocument.activeLayers + try { + //get the active layers + // const layersToExport = app.activeDocument.activeLayers - //create new document - let exportDoc = await executeAsModal(async () => { - return await app.documents.add - }) + //create new document + let exportDoc = await executeAsModal(async () => { + return await app.documents.add + }) - // for (layer of layersToExport) { - await executeAsModal(async () => { - console.log(layer.id) - const dupLayer = await layer.duplicate(exportDoc) - await psapi.selectLayers([dupLayer]) - await psapi.selectLayerChannelCommand() - const selection_info = await psapi.getSelectionInfoExe() - await exportDoc.crop(selection_info) - // export_image_name = `${layer.name}.png` - await readPng(image_name) - // await exportDoc.closeWithoutSaving() - }) - // } - } catch (e) { - console.warn(e) - } + // for (layer of layersToExport) { + await executeAsModal(async () => { + console.log(layer.id) + const dupLayer = await layer.duplicate(exportDoc) + await psapi.selectLayers([dupLayer]) + await psapi.selectLayerChannelCommand() + const selection_info = await psapi.getSelectionInfoExe() + await exportDoc.crop(selection_info) + // export_image_name = `${layer.name}.png` + await readPng(image_name) + // await exportDoc.closeWithoutSaving() + }) + // } + } catch (e) { + console.warn(e) + } } ///////////////////////Start method 3/////////////////////// @@ -140,68 +140,70 @@ For the save function, if running UXP AP1 version 1 then use "wait" for modalBeh If running on UXP API version 2 then use "execute" for modalBahavior, or remove the options and use {} so the options go to default. */ async function savePNG(saveDataTemp) { - const batchPlay = require("photoshop").action.batchPlay + const batchPlay = require('photoshop').action.batchPlay - async function savePNGCommand() { - var saveFolder = - await require("uxp").storage.localFileSystem.getPluginFolder() + async function savePNGCommand() { + var saveFolder = + await require('uxp').storage.localFileSystem.getPluginFolder() - // Again, my variable here is global and assigned earlier. The value is changed during a batch loop for each file. - var saveFile = await saveFolder.createFile("fileName.png") + // Again, my variable here is global and assigned earlier. The value is changed during a batch loop for each file. + var saveFile = await saveFolder.createFile('fileName.png') - const saveData = - await require("uxp").storage.localFileSystem.createSessionToken(saveFile) + const saveData = + await require('uxp').storage.localFileSystem.createSessionToken( + saveFile + ) - const result = await batchPlay( - [ - { - _obj: "save", - as: { - _obj: "PNGFormat", - method: { - _enum: "PNGMethod", - _value: "quick", - }, - PNGInterlaceType: { - _enum: "PNGInterlaceType", - _value: "PNGInterlaceNone", - }, - PNGFilter: { - _enum: "PNGFilter", - _value: "PNGFilterAdaptive", - }, - compression: 6, - }, - in: { - _path: saveData, - _kind: "local", - }, - saveStage: { - _enum: "saveStageType", - _value: "saveBegin", - }, - _isCommand: false, - _options: { - dialogOptions: "dontDisplay", - }, - }, - ], - { - synchronousExecution: true, - modalBehavior: "execute", - } - ) - } - await executeAsModal(async () => { - savePNGCommand() - }) + const result = await batchPlay( + [ + { + _obj: 'save', + as: { + _obj: 'PNGFormat', + method: { + _enum: 'PNGMethod', + _value: 'quick', + }, + PNGInterlaceType: { + _enum: 'PNGInterlaceType', + _value: 'PNGInterlaceNone', + }, + PNGFilter: { + _enum: 'PNGFilter', + _value: 'PNGFilterAdaptive', + }, + compression: 6, + }, + in: { + _path: saveData, + _kind: 'local', + }, + saveStage: { + _enum: 'saveStageType', + _value: 'saveBegin', + }, + _isCommand: false, + _options: { + dialogOptions: 'dontDisplay', + }, + }, + ], + { + synchronousExecution: true, + modalBehavior: 'execute', + } + ) + } + await executeAsModal(async () => { + savePNGCommand() + }) } ////////////////////End method 3//////////////////////////// module.exports = { - exportPng, - readPng, - savePNG, - newExportPng, + exportPng, + readPng, + savePNG, + newExportPng, } diff --git a/helper.js b/helper.js index ce71e8b..f36b4e5 100644 --- a/helper.js +++ b/helper.js @@ -1,110 +1,110 @@ -const { unselectActiveLayers } = require("./psapi") +const { unselectActiveLayers } = require('./psapi') -const app = window.require("photoshop").app +const app = window.require('photoshop').app function getActiveLayer() { - let activeLayers = app.activeDocument.activeLayers - // console.dir(getSize()) - for (const layer of activeLayers) { - console.dir({ layer }) - const name = layer.name - console.dir({ name }) - let layer_size = getLayerSize(layer) - console.dir({ layer_size }) - } + let activeLayers = app.activeDocument.activeLayers + // console.dir(getSize()) + for (const layer of activeLayers) { + console.dir({ layer }) + const name = layer.name + console.dir({ name }) + let layer_size = getLayerSize(layer) + console.dir({ layer_size }) + } - return activeLayers[0] + return activeLayers[0] } function getSize() { - let doc = app.activeDocument - return { height: doc.height, width: doc.width } + let doc = app.activeDocument + return { height: doc.height, width: doc.width } } -const { batchPlay } = require("photoshop").action -const { executeAsModal } = require("photoshop").core +const { batchPlay } = require('photoshop').action +const { executeAsModal } = require('photoshop').core async function reselectBatchPlay(selectionInfo) { - const result = await batchPlay( - [ - { - _obj: "set", - _target: [ - { - _ref: "channel", - _property: "selection", - }, + const result = await batchPlay( + [ + { + _obj: 'set', + _target: [ + { + _ref: 'channel', + _property: 'selection', + }, + ], + to: { + _obj: 'rectangle', + top: { + _unit: 'pixelsUnit', + _value: selectionInfo.top, + }, + left: { + _unit: 'pixelsUnit', + _value: selectionInfo.left, + }, + bottom: { + _unit: 'pixelsUnit', + _value: selectionInfo.bottom, + }, + right: { + _unit: 'pixelsUnit', + _value: selectionInfo.right, + }, + }, + _options: { + dialogOptions: 'dontDisplay', + }, + }, ], - to: { - _obj: "rectangle", - top: { - _unit: "pixelsUnit", - _value: selectionInfo.top, - }, - left: { - _unit: "pixelsUnit", - _value: selectionInfo.left, - }, - bottom: { - _unit: "pixelsUnit", - _value: selectionInfo.bottom, - }, - right: { - _unit: "pixelsUnit", - _value: selectionInfo.right, - }, - }, - _options: { - dialogOptions: "dontDisplay", - }, - }, - ], - { - synchronousExecution: true, - modalBehavior: "execute", - } - ) + { + synchronousExecution: true, + modalBehavior: 'execute', + } + ) } async function reselect(selectionInfo) { - await executeAsModal( - async () => { - reselectBatchPlay(selectionInfo) - }, - { commandName: "reselect" } - ) + await executeAsModal( + async () => { + reselectBatchPlay(selectionInfo) + }, + { commandName: 'reselect' } + ) } //unselect the rectangular marquee selection area async function unSelect() { - const batchPlay = require("photoshop").action.batchPlay + const batchPlay = require('photoshop').action.batchPlay - const result = await batchPlay( - [ - { - _obj: "set", - _target: [ - { - _ref: "channel", - _property: "selection", - }, + const result = await batchPlay( + [ + { + _obj: 'set', + _target: [ + { + _ref: 'channel', + _property: 'selection', + }, + ], + to: { + _enum: 'ordinal', + _value: 'none', + }, + _options: { + dialogOptions: 'dontDisplay', + }, + }, ], - to: { - _enum: "ordinal", - _value: "none", - }, - _options: { - dialogOptions: "dontDisplay", - }, - }, - ], - { - synchronousExecution: true, - modalBehavior: "execute", - } - ) + { + synchronousExecution: true, + modalBehavior: 'execute', + } + ) - return result + return result } // async function layerToSelectionHelper () { @@ -203,5 +203,5 @@ async function unSelect() { // } module.exports = { - // layerToSelection + // layerToSelection } diff --git a/index.html b/index.html index 69df257..fdcdc18 100644 --- a/index.html +++ b/index.html @@ -1,1072 +1,1128 @@ -
- - - - - - - - -|
-
-
-
-
-
-
+
+
-
-
-
- |
-
-
-
-
-
-
-
-
- |
-
|
+
+
+
+
+
+
+
+
+
+ |
+
+
+
+
+
+
+
+
+ |
+