Sitemap

Announcing UXP Scripting in Photoshop

Introducing Photoshop Scripting powered by UXP.

4 min readAug 9, 2022

--

Press enter or click to view image in full size
Psjs — Photoshop Scripting powered by UXP

This post is meant for developers who would like to automate simple tasks using JavaScript along with Photoshop’s UXP APIs. Learn more about the APIs over here and here. Scripting is currently available in Photoshop v23.5.

UXP Scripting 101

What is UXP Scripting?

UXP Scripting is a low-code initiative that lets a single JavaScript file use Photoshop and UXP APIs to accomplish simple tasks.

How does it work?

Let’s start with a very simple example. Create a file and add the code:

console.log("Hello world!!!");

Save it with a .psjs extension in your file system.

How do I run a script?

Select the script to be executed from Photoshop’s File menu -> Scripts -> Browse

How to debug scripts

UDT (the UXP Developer Tool) v1.6 introduces the capability to debug UXP scripts.

Once you have Photoshop running, you can see it listed under ‘Connected apps’. Notice the button ‘Debug Script’. Click on the button, select the script file, and debug using the developer tool.

Press enter or click to view image in full size
Debug script using UDT

UXP Scripting in detail

With the basics out of the way, let’s look at some slightly more complex examples.

Use Photoshop UXP APIs in your script for creating a new layer:

Use UXP APIs in your script for accessing the local file system:

What happens under the hood?

Photoshop runs scripts in a separate execution environment. This is referred to as the ‘execution context’. You can access ExecutionContext via the UXP script module.

The script module provides details for the current script execution, methods to send data to the host app and events to manage script life. You can access the execution script module by the following code snippet:

User Interface
Scripts can show dialogs, but cannot create panels. Any dialog created by a script is transient and will be destroyed when the script completes.

Progress Bar
Your script might take a few seconds to complete. Any task that takes longer than 2–3 seconds will automatically show a progress bar.

Cancel events
Your script may be canceled by the host in case of an error, or from the UI by the user.

Listen to onCancel event to take necessary actions:

The UXP Permission Model

UXP v6.0 has introduced a permission model which requires special permission in order to access certain modules. In the case of UXP Scripting, the permission for these modules are managed by Photoshop. For now, only Font, Clipboard and LocalFileSystem modules are enabled. Keep in mind that attempting to use a module that is not permitted will result in an exception and will terminate the script.

  • Currently, inter-script communication is not possible. A script cannot invoke another script.
  • Passing arguments to scripts is not supported as of now, but will be possible in a future release.
  • You can execute only one script at a time.

UXP Scripting vs UXP Plugins

Ideally, the code you write for UXP scripts should be able to run within your UXP plugin as well. However, there are few differences:

  • The biggest advantage that scripting offers is its simplicity. In order to build and run a script, you don’t need to go through the steps required for setting up a plugin.
  • Scripts can only show Dialog and can’t have/show panel UI like plugins.
  • A script’s lifetime is only till its execution.
  • Scripts don’t have access to persistent storage like plugin-data or localStorage but you can access the plugin’s temp folder via `getTemporaryFolder()` and use file picker `getFileForOpening()`, `getFileForSaving()` or `getFolder()`. Refer to UXP File system APIs to learn more about these methods.
  • The permissions to UXP modules are managed by the host application in case of UXP Scripting. For now, only a limited number of modules are enabled by default (Font, Clipboard and LocalFileSystem).
  • All in all, if you would like to perform more complex tasks, you should opt to write a UXP plugin.
  • UXP Scripting is not supported in our distribution channels yet (that is to say, you cannot list your UXP script on the Adobe Exchange). For now, the scripts are expected to be shared manually (i.e., transfer your files to end users directly).

UXP vs. ExtendScript (and ScriptUI)

ExtendScript is based on the antiquated JavaScript engine (ECMAScript 3). Your ExtendScript scripts will continue to run as is but if you want to write scripts in modern JavaScript (ECMAScript 6) then you should use UXP Scripting.

ExtendScript requires ScriptUI for any user interface. You don’t need ScriptUI for rendering UI in UXP.

ExtendScript is and will continue to be supported by the majority of our flagship applications but UXP Scripting is available only in Photoshop and InDesign (pre-release).

UXP scripting vs. “New world” scripting in Premiere Pro

The “New World” Scripting in Premiere Pro replaced the antiquated ExtendScript interpreter with a modern JavaScript Engine. The previously written ExtendScript scripts were expected to continue to run even after this change. UXP Scripting makes use of the modern Javascript engine, too, but would require you to use UXP APIs to accomplish tasks. (UXP is not yet open for public use in Premiere Pro at the time of this writing.)

Be sure to try out UXP Scripting in Photoshop v23.5 and use the developer forum to talk to UXP experts.

Also join us at the upcoming Photoshop Developer Meetup on September 8th to know more about concepts of UXP Scripting, the new DOM APIs, and Photoshop roadmap.

For more stories like this, subscribe to our Creative Cloud Developer Newsletter.

--

--

No responses yet