Page 1 of 1

Successive Script commands question

Posted: 09 Apr 2020, 21:54
by Craig
In my external software program (Lazarus Pascal) I have this call to AA:

AASendCommand(AA_SCRIPT_RUN, PChar('Image.ClosePrevious'));
AASendCommand(AA_SCRIPT_RUN, PChar('Image.Open("' + theImage + '")'));

The intent is to close the last opened image and replace it with a new image. When I do this the script editor displays only the second command but does not execute either of these. I am guessing the first call to AA_SCRIPT_RUN opened the editor, inserted the line but did not execute because the second command came in, overwrote the first script and then did not execute because it was confused.

But if I only do this:

AASendCommand(AA_SCRIPT_RUN, PChar('Image.Open("' + theImage + '")'));

Then it does open the new image.

My question then is, does successive calls to AA_SCRIPT_RUN require some sort of delay, or a 'RUN' command of some type in the script?

Craig

Re: Successive Script commands question

Posted: 09 Apr 2020, 22:40
by Craig
An update, I changed the Image.ClosePrevious to Image.Close and now the previously opened image does close but the subsequent open new image does not. If I click Run in the script editor it then opens the new image. So it looks like the second command is not Run but is loaded into the script editor.

Craig

Re: Successive Script commands question

Posted: 09 Apr 2020, 23:09
by Craig
I found a solution:

AASendCommand(AA_SCRIPT_RUN, PChar('Image.Close'));
while (AASendCommand(AA_SCRIPT_GetStatus,nil) <> AAS_IDLE) do
Sleep(50);
AASendCommand(AA_SCRIPT_RUN, PChar('Image.Open("' + theImage + '")'));

To keep the program from hanging, this would be better:

AASendCommand(AA_SCRIPT_RUN, PChar('Image.Close'));
n := 0;
while (AASendCommand(AA_SCRIPT_GetStatus,nil) <> AAS_IDLE) do
begin
Sleep(50);
inc(n);
if n > 100 then break;
end;
AASendCommand(AA_SCRIPT_RUN, PChar('Image.Open("' + theImage + '")'));

The program will wait a maximum of 5 seconds before giving up.

Craig

Re: Successive Script commands question

Posted: 10 Apr 2020, 17:12
by fabdev
Yes, there should be some delay between executing a script and a newer one.
Actually it's much better to send complete scripts, so I would join commands when possible:

script = 'Image.ClosePrevious' + <cr> + 'Image.Open("' + theImage + '")'
AASendCommand(AA_SCRIPT_RUN, PChar(script))

between scripts, I would also empty the message queue: <Application.ProcessMessages>