How to invoke main plugin command twice when the plugin button is pressed once


#1

Hello,
I want to invoke the main plugin function command twice when the user clicks the button or uses it’s shortcut.
How can I achieve that


#2

May I ask what’s the use-case here? I might misunderstand you (if so, please tell me), but why can’t you just do a simple loop to run the code twice (see below)?

function main(selection) {
  for (let i = 0; i < 2; i++) {
    // Your code here
  }
}

module.exports = {[...]}

Best,
Pablo


#3

The use case is if I run my plugin two times continuously I’m able to get my desired output.
But, If I run my plugin only once the output is different or maybe not expected.

So, my requirement is whenever the user runs my plugin for once it should be invoked twice(The whole java script should be executed twice). Any help?


#4

Then, my question why you can’t simply use a for loop (see my answer above) for that stays :wink:


#5

Sounds more like there’s a bug going on that causes the plugin to return incorrect results the first time it is run. Rather than executing things twice, I would concentrate on finding the underlying issue.

Sharing some code snippets might help :slight_smile:


#6

In my code in main function I have three sections as follows:

  1. Getting the temporary folder and rendering a image file into the temporary folder.

  2. After creating trying to fill the file in the selection.items[0].
    3.After filling trying to delete the temporary file which is created in the first step.

But the thing is when I run the code into for the first time it says:

Plugin Error: Cannot load ImageFill from path: C:\Users\rajasekhar.b\AppData\Local\Packages\Adobe.CC.XD_adky2gkssdxte\TempState\UXP\PluginsTemp\External\d94b0698\compressed.jpg
at new ImageFill (plugins/BitmapFillWrapper.js:1:514)
at myPluginCommand (C:\Users\rajasekhar.b\AppData\Local\Packages\Adobe.CC.XD_adky2gkssdxte\LocalState\develop\Compress Now\main.js:67:28)

For the 2nd time when I run that:
It’s filling the image file into the selection and deleting the temporary file.


#7

I tried. But, it won’t work:neutral_face:

The main thing is instead of creating temp file first. The plugin runs the imagefill command.
Not executing in order


#8

Are you awaiting (the Promise of) the creation of the image? It kind of sounds to me like you’re not awaiting this asynchronous creation and it’s finished when you run it the second time…


#9

How to await the asynchronous creation?


#10

The File IO APIs are asynchronous and use the (standard) JavaScript Promise APIs. You can read more about the asynchronous part at https://adobexdplatform.com/plugin-docs/reference/javascript/sync-async.html. If you don’t know Promises yet, just google for JavaScript Promises and you’ll find many articles explaining it.

Hope this helps,
Best,
Pablo


#12

Could you possibly create a Gist on GitHub or some other platform where I can easily refer to line numbers etc.? Then, I’ll be happy to do a quick review of your code (like this, I’d need to refer to things like “where you write …”, which is a bit imprecise and cumbersome :wink:).

Thank you very much in advance :+1:


#14

Perfect, thank you. As far as I can see it, you can simply replace

application.createRenditions(renditions)
            .then(results => {
                
                // // create the dialog
                // let dialog = document.createElement("dialog");

                // // main container
                // let container = document.createElement("div");
                // container.style.minWidth = 400;
                // container.style.padding = 60;

                // // add conten
                // let title = document.createElement("h2");
                // title.style.padding = 20;
                // title.textContent = `Compress Now worked!`;
                // container.appendChild(title);

                // // close button
                // let closeButton = document.createElement("button");
                // closeButton.textContent = "Got it!";
                // closeButton.style.padding = 20;
                // container.appendChild(closeButton);
                // closeButton.onclick = (e) => {
                //     dialog.close();
                // }

                // document.body.appendChild(dialog);
                // dialog.appendChild(container);
                // dialog.showModal()
                console.log("Image Created");
                
            })
            .catch(error => {
            })

with

await application.createRenditions(renditions);

in the main.js file (lines 27-60) of the a-way-to-fix-bug-branch, and it should do the trick. I don’t have much time right now, so if this doesn’t work, there isn’t much I can do to help you at this point in time, but I think it should at least get you one step closer to getting it to work (it isn’t my task to fix all potential issues, but only this one, after all :wink:)


#15

Thanks alot!
Maybe now I can fix that