• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
6

How to create a basic save image UXP script?

Community Beginner ,
Mar 25, 2024 Mar 25, 2024

Copy link to clipboard

Copied

I am new to Photoshop UXP scripts. I am trying to create a simple UXP script that will resize the current image in Photoshop and then save it to a user specified file location. So far, I have only written the image save code (i.e., no resize code), but it is not working. Here is the code:

 

const ps = require('photoshop');
const uxp = require('uxp');
const fs = uxp.storage.localFileSystem;
const core = ps.core;
const app = ps.app;
const img = app.activeDocument;

try {
    const file = await fs.getFileForSaving('', { types: [ 'jpg' ] });
    img.saveAs.jpg(file, { quality: 8 });
} catch (e) {
    core.showAlert(e.message);
    console.log(e);
}

 

I think the code is throwing on img.saveAs after I click the Save button on the Save dialog. However, I cannot get the PS debugger connected to the script, so I have no logs. Curiously, core.showAlert in the catch clause does not display an alert, so I'm completely blind. I can display an alert before the call to img.saveAs, but not immediately after that line., so it must be either throwing or completely crashing when I click the Save button.

 

A small dialog appears on the screen after I click the Save dialog's Save button. The dialog disappears in a flash (I can't even capture it with a screen recorder), but it looks like it might be a progress bar. The image file is not saved.

 

It's very frustrating to have no way to debug the problem, but it is what it is. Can anyone see what is wrong, or have any suggestions?

 

TOPICS
Actions and scripting , Windows

Views

469

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Apr 10, 2024 Apr 10, 2024

Hi,

I've just copied and pasted your code in a `.psjs` file, verbatim, with one minor correction:

 

await img.saveAs.jpg(file, { quality: 8 }); // 👈 note the await

 

I've one document open, I select File > Scripts > Browse...; point to the `.psjs` file; the save dialog pops up, I enter a filename; a progress bar briefly flashes; the file is correctly saved on disk.

 

Please note that:

  •  saveAs methods are asynchronous: they return a promise, hence the need to `await`.
  • Manifest permissions for disk access
...

Votes

Translate

Translate
Adobe
Community Expert ,
Mar 25, 2024 Mar 25, 2024

Copy link to clipboard

Copied

Breaking out the popcorn...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 29, 2024 Mar 29, 2024

Copy link to clipboard

Copied

Bump...

 

if (disgruntled === true) {

    I really don't wish to have a rant here, however, this topic, in a nutshell, is one of the fundmental problems with UXP scripting for new users or users experienced with ExtendScript. I hope that moving forward Adobe do a much better job of making such "simple things" accessible with clear, concise examples of both DOM and batchPlay code.

} else {

    How could one who has an interest in the future of Adobe scripting otherwise feel?

}

 

I don't consider the following to be helpful.

 

https://developer.adobe.com/photoshop/uxp/2022/scripting/samples/#access-the-local-filesystem 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 30, 2024 Mar 30, 2024

Copy link to clipboard

Copied

@Stephen_A_Marsh, Ok, thanks, at least I understand the situation a bit better now.

 

And, yes, I agree, Adobe's filesystem sample was not helpful. I had already found that same example on a different page and I was dumbfounded as to why Adobe was presenting an example of how to save a text file, rather than a useful example of how to save an image file. I then found an activeDocument.saveAs.jpg example elsewhere, but alas, that wasn't enough to get my script working.

 

Everything worked out in the end because I was able to write a UXP script to calculate the reduced image dimensions I needed and resize the image to those new dimensions, which was the script's main purpose. I then invoked the UXP script from a custom Photoshop action. The action then displays the file save dialog after invoking the script. That worked very well for my situation.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 01, 2024 Apr 01, 2024

Copy link to clipboard

Copied

Extendscript examples and documentation are total crap. UXP is even worse. Plus not being cross-application, and not having decent tools (ESTK was about a C-/D+, VSCode is better in a lot of ways but a good example of why Windows apps suck on the Mac, and UXP Developer Tools seems useless. Why can't this all be in XCode for Mac developers? Why can't we get GOOD sample code? A good tutorial series? Something like Apple Playgrounds (for learning the Swift language)?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 01, 2024 May 01, 2024

Copy link to clipboard

Copied

@Stephen_A_Marsh thank you for putting words to the single most maddening barrier to learning how to make useful things for Photoshop - the lack of clear, current, actionable resources for folks who aren't advanced programmers (playground was a great addition btw). There is SO much potential being lost with such a high cost of entry. I have mad respect for our developers, believe me, but this chasm between them and the Photoshop professional is what leaves us with the current state of the plugin market - tools that developers would make. Don't flame me, there are certainly exceptions! But overwhelmingly there are very few plugins that are made by and for Photoshop professionals. Believe me, I have multiple whiteboards filled with plugin outlines. Plugins that most Photoshop developers would never even consider (not their fault - it's not their job!), but would dramatically improve the daily life of Photoshop professionals. 
I know how hard the developer team and community have worked on this monumental project called UXP, respect. Please read my words not as a troll but as imagining a better world 🙂

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 01, 2024 May 01, 2024

Copy link to clipboard

Copied

LATEST

I did have a programming background from college but had to mostly learn from scratch to do my job as a professional product photographer. I rely on a bunch of Bridge, Photoshop, and Excel scripts to do my job. And even though I'm a die-hard Mac guy I had to learn about Powershell. Ugh.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 30, 2024 Mar 30, 2024

Copy link to clipboard

Copied

se vuoi salvare il file in una posizione devi creare un token di sessione. Non ho dubbi che sarai in grado di farlo.

 

I forgot another thing you need to modify and the manifest file you must give write and save permissions

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 30, 2024 Mar 30, 2024

Copy link to clipboard

Copied

@Ciccillotto – thanks for the vote of confidence. I learn by picking apart working code... So to quote the classics:

 

"Show me the money!"

 

"Show me the code!"

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 30, 2024 Mar 30, 2024

Copy link to clipboard

Copied

Stefano I understand what you're asking, unfortunately I'm trying to understand how to do everything. believe me it's not easy, but I will try to do it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 30, 2024 Mar 30, 2024

Copy link to clipboard

Copied

@Ciccillotto 

 

I am not asking for you to post code, although I would be greatly appreciative if you did.

 

I am lamenting the lack of accessible DOM and batchPlay resources from Adobe in fundamental UXP tasks that are "easy" to do in ExtendScript but very different and difficult to do in UXP scripting (forgetting panels). Saving a file is one such example, either DOM or batchPlay. Getting the UXP Developer Tools app to be successfully configured to debug a .psjs is another. I'm sure that there is more. :]

 

I take for granted that ExtensScript has a long history and a mature user base with ample code samples, experience and knowledge.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 30, 2024 Mar 30, 2024

Copy link to clipboard

Copied

unfortunately, as said several times, uxp programmers are very jealous and do everything to hide the code, as you have noticed, I have asked for help several times on this forum and you have helped me several times, the biggest problem is also the poor and often incomplete documentation, then sometimes you do some programming and in the new version of photoshop some things don't work. in my opinion uxp is still immature which is why it is slow to establish itself, then having programmers who do not help with valid codes it becomes even more difficult for this uxp to go in the right direction.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 30, 2024 Mar 30, 2024

Copy link to clipboard

Copied

After reading your reply, I found documentation on the manifest, but it indicated that providing a manifest is only applicable to plug-ins. UXP scripts use a manifest too, but it's an internal, immutable manifest. However, the UXP script manifest already provides full access permission to the file system, so no manifest change is needed for UXP scripts.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 30, 2024 Mar 30, 2024

Copy link to clipboard

Copied

Sorry but you're talking about scripts. psjs, if you talk about this it's one thing, if you talk about uxp plugin with panel and buttons you need the manifest.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 30, 2024 Mar 30, 2024

Copy link to clipboard

Copied

@Ciccillotto , Yes, I've been referring only to UXP scripts (psjs) from the beginning. I saw your comment about manifests and I thought that might be part of what I was missing to get my script working. But then I saw the Adobe docs state that configurable manifests are only applicable to plug-ins, so unfortunately, manifests do not help me.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 31, 2024 Mar 31, 2024

Copy link to clipboard

Copied

psjs files are just single scripts, When we talk about UXP, we mean a panel with buttons. That's why I mentioned the manifest file. happy for you that you solved it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 31, 2024 Mar 31, 2024

Copy link to clipboard

Copied

Ah, ok... I had the impression UXP just the PSJS scripts, but now that I think about it, it makes sense that UXP includes UI components as well. Thanks for the clarification!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 31, 2024 Mar 31, 2024

Copy link to clipboard

Copied


@Ciccillotto wrote:

psjs files are just single scripts


 

I agree

 


When we talk about UXP, we mean a panel with buttons.

 

Here I respectfully disagree.

 

UXP (Unified Extensibility Platform) is for both persistent panels and scripts with or without a UI.

 

UXP is the modern way to create plugins and scripts for Adobe Creative Cloud

 

Running a standalone JavaScript file in UXP is now available in Photoshop 23.5.

Photoshop recognizes the new file extension .psjs for JavaScript files that will be executed by UXP.

 

So, when UXP was first released, it was true that this was only for panels, however, now UXP supports both panels and stand-alone scripts. Therefore, I believe that one now needs to be specific and not just use UXP as a general term, it should be qualified in the context of a panel or script.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 01, 2024 Apr 01, 2024

Copy link to clipboard

Copied

Stephen From what I know the UXP script is only for panels. the files. psjs can be used outside the panel. Then I have no idea how a uxp script can work outside of a panel, the uxp file is a set of individual functions controlled differently. However, I don't know if it can be done.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 10, 2024 Apr 10, 2024

Copy link to clipboard

Copied

quote
quote

psjs files are just single scripts


By @Ciccillotto

 

I agree

 


When we talk about UXP, we mean a panel with buttons.

 

Here I respectfully disagree.

 

UXP (Unified Extensibility Platform) is for both persistent panels and scripts with or without a UI.


By @Stephen_A_Marsh

 

I can confirm that UXP should be considered, in its wider acceptation, as an environment; it's not a language, like ExtendScript was, but rather a comprehensive system that encapsulates plugins, scripts, and their distribution.

 

Davide Barranca - PS developer and author
www.ps-scripting.com

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 10, 2024 Apr 10, 2024

Copy link to clipboard

Copied

Hi,

I've just copied and pasted your code in a `.psjs` file, verbatim, with one minor correction:

 

await img.saveAs.jpg(file, { quality: 8 }); // 👈 note the await

 

I've one document open, I select File > Scripts > Browse...; point to the `.psjs` file; the save dialog pops up, I enter a filename; a progress bar briefly flashes; the file is correctly saved on disk.

 

Please note that:

  •  saveAs methods are asynchronous: they return a promise, hence the need to `await`.
  • Manifest permissions for disk access are needed only in the context of UXP plugins, not UXP scripts.
  • File management is one confusing piece of UXP. Although it is a paid resource, I devote an entire chapter to it in my Professional Photoshop UXP book.

 

I'd kindly remind the moderators to reset the `DAYS_SINCE_QUESTIONS_ABOUT_SAVING_FILES` counter. 🙃

 

 

Davide Barranca - PS developer and author
www.ps-scripting.com

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 10, 2024 Apr 10, 2024

Copy link to clipboard

Copied

Thanks @Davide_Barranca! Although I have not actually tried your solution (I solved the problem via use of an action instead), your answer appears sensible and it sounds like you have tested it.

Still, it seems odd that the lack of an await on the saveAs would have prevented getFileForSaving from properly displaying the Save dialog since getFileForSaving is invoked before saveAs. Also, the missing await should not have been technically necessary for this case (although certainly a good practice!) since there is no code following the saveAs, and the event loop should not exit before all promises are resolved.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 11, 2024 Apr 11, 2024

Copy link to clipboard

Copied

Hiya!

When I tested your code, the save dialog did appear (hence, I guess, `file` was properly formed), but the image wasn't saved on disk: all I got was an empty file. What you say about the event loop makes total JavaScript sense; still, I've been bitten in the rear end so many times by a missing `await` in PS scripting that I tend to always use them.

 

Davide Barranca - PS developer and author
www.ps-scripting.com

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines