Samples
Create a new layer
You can create a script that creates a new layer, with the help of the Photoshop DOM API:
Copied to your clipboard1const app = require('photoshop').app;2await app.documents.add();3await app.activeDocument.createLayer();4app.activeDocument.layers[0].name = 'New layer';
Access the local filesystem
You can create a script that accesses your local filesystem, with the help of the UXP storage module:
Copied to your clipboard1const uxpfs = require("uxp").storage;2const ufs = uxpfs.localFileSystem;34try {5 const folder = await ufs.getTemporaryFolder()6 const metadata = await folder.getMetadata();7 console.log(JSON.stringify(metadata));8} catch (e) {9 console.log(`Local File system error: ${e}`);10}
Using a file picker
You can create a script that instantiates a file picker for writing/saving to files.
Copied to your clipboard1const fs = require('uxp').storage.localFileSystem;2try {3 // save file to a location using file picker4 const file = await fs.getFileForSaving("demo.txt");5 await file.write("Hello World! This is demo.");6} catch (e) {7 console.log(e);8}
Read/write to clipboard
You can access the clipboard module (navigator.clipboard
) to:
- write to a clipboard (
setContent()
) - read a clipboard's contents (
readText()
)
Copied to your clipboard1try {2 const clipboard = navigator.clipboard;3 await clipboard.setContent({ 'text/plain': "Test string" });4} catch (e) {5 throw new Error(e);6}7try {8 const clipboard = navigator.clipboard;9 const text = await clipboard.readText();10 console.log(text);11} catch (e) {12 throw new Error(e);13}
Create dialog UIs
Simple example
Copied to your clipboard1async function createDialog() {2 const dialog = document.createElement("dialog");3 dialog.style.color = "white";4 const div = document.createElement("div");5 div.style.display = "block";6 div.style.height = "200px";7 div.style.width = "400px";8 const header = document.createElement("h2");9 header.id = "head";10 header.style.color = "white";11 header.textContent = "Sample Dialog";12 div.appendChild(header);13 dialog.appendChild(div);14 await document.body.appendChild(dialog).showModal();15}16// Wait for the dialog to render17await createDialog();
Example with "Done" button
Copied to your clipboard1async function showDialog() {2 let dialog = createDialog();3 document.body.appendChild(dialog).showModal();45 // Give the script time to show the dialog by returning a promise. Make sure that it is resolved/rejected later.6 return new Promise((resolve, reject) => {7 try {8 // Resolve the promise and dismiss the dialog when when user clicks on 'Done' button9 const doneBtn = document.getElementById("done");10 doneBtn.addEventListener("click", () => {11 console.log("user is done");12 dialog.close();13 resolve("user is done");14 })1516 // reject when dialog is cancelled/closed17 dialog.addEventListener("cancel", () => {18 console.log("dialog cancelled");19 reject("dialog cancelled");20 });2122 dialog.addEventListener("close", () => {23 console.log("dialog closed");24 reject("dialog closed");25 });26 } catch (e) {27 console.log(e);28 reject(e);29 }30 })31}3233function createDialog() {34 const dialog = document.createElement("dialog");35 dialog.style.color = "white";36 const div = document.createElement("div");37 div.style.display = "block";38 div.style.height = "200px";39 div.style.width = "400px";40 const header = document.createElement("h2");41 header.id = "head";42 header.style.color = "white";43 header.textContent = "Sample Dialog";44 div.appendChild(header);45 const doneButton = document.createElement("button");46 doneButton.id = "done";47 doneButton.textContent = "Done";48 div.appendChild(doneButton);49 dialog.appendChild(div);50 return dialog;51}5253// Wait for the dialog to render54await showDialog();
Access installed fonts
Photoshop has set the permission for the Fonts module to ReadInstalled
. This means that if no font is specified or if the font is not installed, then UXP will fallback to "system-ui" font (the default OS system UI font).
Copied to your clipboard1async function createDialog() {2 const dialog = document.createElement("dialog");34 dialog.style.color = "white";56 const div = document.createElement("div");7 div.style.display = "block";8 div.style.height = "200px";9 div.style.width = "450px";1011 const p1 = createParagraph("system-ui"); // use default OS font12 const p2 = createParagraph("non-existent-font"); // try using a non existent font. Will resolve to OS default.13 const p3 = createParagraph("Courier New"); // use any installed font14 div.appendChild(p1);15 div.appendChild(p2);16 div.appendChild(p3);1718 dialog.appendChild(div);19 await document.body.appendChild(dialog).showModal();20}2122function createParagraph(fontFamily) {23 const p = document.createElement("p");24 p.style.color = "white";25 p.style.fontFamily = fontFamily;26 p.textContent = `font-family:${fontFamily}: A fox jumps over the lazy dog`;27 return p;28}2930// Wait for the dialog to render31await createDialog();