photoshop_execute_script
Execute custom ExtendScript (JSX) in Photoshop when no existing tool covers the operation. Use for advanced automation; returns script output as text or JSON.
Instructions
Execute custom ExtendScript (JSX) code inside Photoshop (advanced escape hatch).
Use when: no existing tool covers the operation and you can write safe JSX. Do NOT use when: a recipe or atomic tool exists — prefer photoshop_recipe_* or photoshop_* tools. Do NOT use when: painting a two-color gradient — use photoshop_fill_gradient. Document.gradients does not exist and throws "undefined is not an object".
Returns: script return value serialized as text/JSON.
IMPORTANT: Your code runs inside a wrapping IIFE. Use an explicit return to pass data back — a bare trailing expression returns undefined. Example: return { ok: true }; Objects are serialized with toSource() and parsed automatically on macOS and Windows.
Long scripts: pass timeout_ms (up to 600000) so the default 30s budget does not kill the job.
Preconditions: valid ExtendScript; active document if script expects one. Side effects: depends on code.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | ExtendScript code to execute | |
| timeout_ms | No | Script timeout in milliseconds (default 30000, max 600000). Override with env PHOTOSHOP_SCRIPT_TIMEOUT. Use for long loops, batch jobs, or large documents. | |
| document_id | Yes | Photoshop document id from photoshop_get_state / photoshop_list_documents. Send null or 0 to use the active document. A positive number activates that document before the tool runs. When no document is open, a stale id does not block the call. |