Skip to main content
Participating Frequently
January 18, 2023
Question

Placing image hangs the script unless it's in Async/Await wrapper

  • January 18, 2023
  • 3 replies
  • 596 views

Without async/await I have nothing on a screen but a spinning wheel. When I try to close it, suddenly a picture appears, then I press "cancel" and have the result on the page. But if I use async/await on the main() function (and placing of the image can be many function calls below), it works just fine.
Hangs:

main();

function main() {
    const myDocument = app.activeDocument;    
    const imagePath = "C:\\Users\\<User>\\Desktop\\Capture11.JPG";
    const imageFrame = myDocument.rectangles.add({
        geometricBounds: ["36px", "36px", "260px", "180px"],
        strokeWeight: 0,
        strokeColor: myDocument.swatches.item('None')
    });
    imageFrame.place(imagePath);
}

Works:

await main();

async function main() {
    return new Promise(async(resolve, reject) => {

        const myDocument = app.activeDocument;    
        const imagePath = "C:\\Users\\Ooo\\Desktop\\Capture11.JPG";
        const imageFrame = myDocument.rectangles.add({
            geometricBounds: ["36px", "36px", "260px", "180px"],
            strokeWeight: 0,
            strokeColor: myDocument.swatches.item('None')
        });
        imageFrame.place(imagePath);

        resolve();
    });
}

 

I'm new to Adobe scripting and that was really confusing to me. Is there some documentation that might help clarify things like that? Thank you in advance!

This topic has been closed for replies.

3 replies

Participating Frequently
January 20, 2023

Actually, after watching Davide Barranca's #5 about UXP for Photoshop:
https://www.youtube.com/watch?v=sf7YPWUppSM&list=PLRR5kmVeh43alNtSKHUlmbBjLqezgwzPJ&index=5
I tried even simpler "await," and it worked:

const myDocument = app.activeDocument;    
const imagePath = "C:\\Users\\Ooo\\Desktop\\Capture11.JPG";
const imageFrame = myDocument.rectangles.add({
    geometricBounds: ["36px", "36px", "260px", "180px"],
    strokeWeight: 0,
    strokeColor: myDocument.swatches.item('None')
});
await imageFrame.place(imagePath);

I'm looking for the same kind of documentation for InDesign as the one Davide Barranca quoted for Photoshop.
https://developer.adobe.com/photoshop/uxp/2022/ps_reference/classes/document/#methods

close  22.5
async : Promise<void>
Closes the document, showing a prompt to save unsaved changes if specified.

I guess we just can keep it in mind for now.

Community Expert
January 20, 2023

Hi Olga,

that's a great one you found with Davide's #5 lecture!

Just using await with method place()

Thanks for sharing.

 

What bugs me most in the current situation is that there seems to be no shortcut for the user like we have in ExtendScript with the tilde in a file path. So like in your case you have either to know the user name or you have to process the current user name through some other methods.

 

Well, that the ExtendScript Toolkit cannot see your InDesign is a different problem.

The ESTK should do that. In my case with my old Windows 10 laptop there is no issue at all.

Both versions of the ESTK that I have installed are able to see InDesign 2023.

ExtendScript Toolkit CC version 4.0.0.1 and also ExtendScript Toolkit CS6 version 3.8.0.12.

Can also run both versions side by side in case I need this.

 

Developing UXP with the ESTK does not work very well.

Ok, you could certainly write code for UXP in the ESTK like in every other editor or word processor, but you will not be able to debug it.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Participating Frequently
January 21, 2023

Hi, Uwe!

I just saw a bit outdated video that recommended ESTK for exploring object models, and they didn't load for some reason. It's ok since I can explore through the console of UXP DevTool.
Recently I had a problem with user paths, and that is what solves it:

require("os").homedir()

or

let scriptPath = app.activeScript;
if (/\//.test(scriptPath)) {
    scriptPath = scriptPath.replace(/(.*\/).*$/, "$1");
}else{
    scriptPath = scriptPath.replace(/(.*\\).*$/, "$1");
}const paths2 = fs.readdirSync(`file:${scriptPath}test`);

Albeit it's less readable than ~user, still possible.

 

That said, I'm no expert in architecture. From Davide's videos, I gathered that Adobe is effectively retiring the whole evolutionary branch. I used to hear that Adobe was doing right what went on the web sideways. So I'm deducting from it that the people in charge have a lot of reasons to take recent changes with a grain of salt. Curiously enough, this happens when the semantic web is trying to make a comeback, and the Google Web Vitals initiative effectively made everyone think twice before using any frameworks or libraries, including React. I don't have strong opinions on that, but I can imagine that specialists do have them. Hope it will boil down to the best from all those evolutionary branches. (Personally, I had to use ES6 and got used to it; it's hard to go back.)

 

On a side note, another thing in Davide's videos that made me envy the Photoshop environment is Action manager/Batch play/Alchemist. I did some scripts for Blender, and Blender too has a console that lets you see what commands are used when you work in UI as usual. It speeds up development unbelievably. Anyway, another nice to have documentation/debugging too.

Regards,
Olga T

Community Expert
January 19, 2023

Hi Olga,

Marc Autret recently did an article about the current state of UXP with InDesign:

 

Getting Started with IDJS (InDesign UXP Scripting Format)
Marc Autret, January 11, 2023

https://indiscripts.com/post/2023/01/getting-started-with-idjs-indesign-uxp-scripting-format

 

When it comes to DOM documentation of ExtendScript, that to an extend could be useful also with UXP, see into:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#about.html

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Participating Frequently
January 19, 2023

Hi, Uwe!

The second link was the main help for me since ExtendedScript Toolkit failed to load the object library, and UXP developer tool closes when it hits the error. (I finally configured ESlint and in Bash terminal

tail -f "C:\Users\<User Name>\AppData\Local\Temp\UXPLogs\UXPErrorLogs_<date>.log"

 to be able to read console.log). The plugin for debugging with VSCode fails to evaluate spread syntax ={...attrs} and some other things, so tail -f works better for me. That's pretty much all environment I was able to assemble so far.

The first link has some new information for me. It's an interesting point of view, and links to GitHub repositories are the most useful guides. I used google translate on German pages (if anyone didn't know, they could do that -- it works pretty well, at least in this case):

https://www-indesignblog-com.translate.goog/2023/01/das-uxp-developer-tool/?_x_tr_sl=de&_x_tr_tl=en&_x_tr_hl=en&_x_tr_pto=wapp
I really appreciate the effort of people who share their experiences. (since, as Marc Autret said, "developers will have to rely on trial and error to document their own experiences.")

 

Thanks for your time. It was great talking to you.

Regards,
Olga T

 

Community Expert
January 18, 2023

"I have nothing on a screen but a spinning wheel"

 

Hi @Olga27210278vxwm ,

I see the same. InDesign 2023 version 18.1 on Windows 10.

When I do a "right button" mouse click (invoking the context menu) the image finally will be placed.

And if I'll do that in a split second after running your first code the image will be placed in a split second.

 

So I cannot answer the "why" of  your UXP Scripting question. Its implementation with InDesign is just in its infancy.

The same function written in ExtendScript using the File object will work without any flaw:

// ExtendScript version:

main();

function main() {
    var myDocument = app.activeDocument;  
    var imageFile = File( "~/Desktop/Capture11.JPG" );
    var imageFrame = myDocument.rectangles.add({
        geometricBounds: ["36px", "36px", "260px", "180px"],
        strokeWeight: 0,
        strokeColor: "None"
    });
    imageFrame.place( imageFile );
};

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Participating Frequently
January 19, 2023

Hello, Uwe Laubender! Thank you! I understand that it must be a sensitive time when the whole system is going through changes. I think Adobe scripting has a bit steep learning curve for people who come from the web side of things because of different terms and surprising ways of how things influence each other. It's not directly related to what form of the script I'll have to use. However, I enjoy discovering new things, and it's fine with me to play through several hick-ups of a system as long as the direction is right. UXP Scripting has covered all my needs so far, and it has been a rewarding experience.
I'm looking forward to new features, though I hope that my script will work a year from now (or with a reasonable amount of changes). I just wish there were a guide for people with a web background that would help to understand the terms and best practices—for example, differences in approach to tables and such, or how "line-height" is called and if adjusting it will have any effect on a table cell. That kind of information can make Adobe products more approachable, and make a learning curve a little bit less steep.

Regards,
Olga T