Macro won't wait before proceeding

Hi, all,

I'm trying to get part of my macro to wait until the bit before has finished. Usually, this works automatically without the need to control it but in my case, it needs something to help it wait.

When I run the macro, it's moving straight on and trying to run the "Execute Macro 2 - Create Image Set", before "Execute Macro 1 - Export..." has finished.

What the macro should be doing is...

  1. Execute macro 1 (export images)
  2. When all the windows have closed, execute macro 2 (create image set)
  3. When the destination folder is empty, execute macro 3 (create master set)

Can any kind soul please help?

My guess would be the problem is with the way you're closing your 8 windows.

Sometimes the fact that KM processes each action so quickly means that your Mac can't actually keep up - especially true when you're dealing with the user interface.

I would try adding a Pause action immediately following the Type Command-W action with a delay of, say, 0.2 seconds. Make sure the Pause is inside the Repeat loop.

You might have to tweak the delay value depending on how slow/fast your Mac is...

Another approach would be to use Semaphore actions, but that's a bit more complicated and might still not solve the problem if your windows are not closing properly.

1 Like

Okay. The first thing to set straight is that the Calling Macro actually will wait for the Called Macro to do all its steps/Actions before moving on (unless you have set it to Execute Asynchronously - and that is probably worth just quickly checking by clicking on the “gear” in the top right-hand corner of the Execute Macro Action).

But the overlap will be the System taking time to complete those steps (as @tiffle has already said).

So, your solution method is correct (look for some test that the steps have been completed by the System before moving on).

I’m not sure why your Window title test is not working. But I have found a very reliable test is to check if any Menu items become enabled/disabled during and after a task. If for example, your task results in no windows being open at the end of the process then probably File>Save will not be enabled. Or, during the Save process I suppose File>Open might not be enabled. It’s worth just having a play.

Also, with these kinds of timing issues, I usually start by adding in a simple time pause - starting way longer than needed (say, 10 seconds) to check that this is indeed what is going on. Even with a conditional pause it can sometimes be good to have (or at least start by having) a tiny time pause (like, 0.2 seconds) and then disable/delete that Action if it is not needed.

Note: I'm a complete newb, so the above answers are far more likely to be right than this one! But I was already most of the way through before @tiffle and @zabobon posted and, if nothing else, I'll learn from people telling me why I'm wrong :wink:

For the first pause -- I think you've got your logic wrong. Consider this:

  • Window 1: Title "myFile1 • CMYK • 100%"
  • Window 2: Title "myFile2 • RGB • 100%"

As written, your condition 1 evaluates to "true" because Window 2 does not contain "CMYK". Condition 2 evaluates to "true" because Window 1 does not contain "RGB". And if the frontmost window title does not contain "master" then condition 3 is also true -- "all of the following are true" and so there is no pause.

If you want to "wait until none of the window titles contain 'CMYK' or 'RGB' and the front window title does not contain 'master'" try "Pause until none of the following are true" and changing everything to "does contain". That should also take care of any "window closing" timing issues.

I think you've got similar issues elsewhere, eg the last action in the image executes if the "FolderIsEmpty" variable contains "Y", yet in the step before you explicitly set "FolderIsEmpty" to "N" -- so it will never execute.

2 Likes

I'll check this all out a bit later today. Thank you all for such fast responses and helpful advice.

2 Likes

Ok, finally got round to testing and I still can't make it work.

No matter what I do it's still jumping the gun. Or even doing things it isn't even meant to do, such as running the master set macro (even though I disabled it).

Each macro runs perfectly on its own. But as soon as I try and run them in sequence, that's when the trouble starts.

I'll have to come back to it another time when I'm not up to my eyeballs in work.

It's hard without actually having your Macros to play with. But my guess is your first pause is in the wrong place. If you want the Calling Macro to pause until the Called Macro has done its stuff (or rather, the Mac System has caught up with what it is doing) you need to put a conditional pause straight after your first Execute Macro Action. Otherwise if whatever your Macro 1 - Export PNG, JPG and EPS is doing is taking the System time to process, there is nothing to stop your Calling Macro from beginning to close the 8 Windows.

I took the close windows bit out and have also tested just the export script followed by the image set macro.

I think the issue is to do with how Keyboard Maestro sees that nothing is open in Adobe Illustrator. I may have to explore getting a JSX or Applescript to check if no documents are open and to send the result to Keyboard Maestro as a variable. Then when KM sees the variable is true, it carries on with the macro.

Right, it's vastly more technical and complex than I was hoping but I've got it working.

After the first macro has started, the next one (2) waits until the following script returns YES:

//checks to see if all windows are closed
checkClosed();
function checkClosed() {
 if (app.documents.length == 0) { 
 return 'YES';
 }
 }

Now it runs the 2nd macro with the third macro waiting in the wings.

This time it's waiting for an Applescript to see if the Export folder is empty. I tried many different ways to do this, e.g. shell script, javascript, Keyboard Maestro actions. This Applescript is the only one that worked reliably.

It's been a bit of a journey and the many, many hours producing the scripts and macros, and piecing it all together in Keyboard Maestro in no way makes up for the time it will save. However, I'm pleased I did it and have learned a few things along the way.

1 Like

The "Pause until..." action in your macro above doesn't look to see if nothing is open, which is why it was stepping through -- the condition will be met any time you have at least one "CMYK" window and at least one "RGB" window open, with the frontmost window not containing "Master".

You could do check for no windows with JSX or AppleScript, but it might be slightly faster to re-visit you logic and do it within KM rather than spawning another process.

Actually, it's repeatedly running until there's only one file left in that folder, the hidden ".DS_Store". Two problems with that -- it's a waste of resources, so you might want to put a pause in there (either as a KM action or in the testing AppleScript) to give your machine more time to do what you want it to do. More worryingly is that there's a chance that the condition will never be true -- if the folder goes from 1 "real" file plus ".DS_Store" (numberOfFiles = 1) to truly empty (numberOfFiles = -1) between tests, the loop will be endless. Set it "less than or equal to 0" instead, just to be safe.

You could also avoid the issue by using the Finder rather than System Events:

tell application "Finder"
  set numberOfFiles to number of files in folder ((path to home folder as text) & "Dropbox:redacted:Artwork:Export) as alias
end tell

...which will return 0 when there are no files -- no need for the "-1", and so no chance of skipping your ending condition.

Thanks for trying to help but I've also tried it with this and it still progresses to the next part. The only workaround is as above using the scripts.
Screenshot 2022-05-05 at 11.40.57

That would pause while the front window title contains "master" -- is that what you want it to do?

Perhaps you could post an example screenshot of the window titles just before the "Repeat Actions 8 times..." action -- if nothing else, people with Illustrator 2022 could try and replicate and see if there might be a bug in eg pulling window titles. That would be very useful to know...

Okay. On my Mac I have found two pauses that work in Adobe Illustrator - (that wait for there to be no open documents before moving on).

Here is the first:

It seems like Adobe Illustrator always appends some info in parentheses at the end of the file name, in its window title, like (RGB/Preview). So every open document window will have a "(" in its title.

The below also works, with the same rule for a "(" in the title.

I just did a simple test where the Macro pauses until I have closed all the open windows manually and either of the above seemed to work fine.

Screen Recording 2022-05-05 at 15.40.03-Animated GIF

I think the wording of the first condition is ambiguous - to me anyway. "Any" window "does not contain" is a hard one to get my head around. In practice (by my testing) it works as "no window contains".

As this particular pause waits for there to be no open windows, you need to only start closing windows at the end of the sub macro. So, I would put the ⌘W Actions in the sub macro rather than in the main macro.

And trying to eliminate as many potential problems as I can see in your Calling Macro:

You are calling your sub Macro only once but closing 8 document windows. I am assuming the Sub Macro works on all open documents for that to work?

If I'm remembering Illustrator correctly, every title bar also has an "@" in it to show zoom level -- no better in practice, but might be a little more self-explanatory when you revisit the macro in a year's time!

Definitely a hard one to get your head around! This one's easier if you think the "wrong way round" -- rather than true if any window does not contain, think false if every window does contain.

That's why OP's original first pause never happened. "CMYK" and "RGB" are mutually exclusive window titles, so as soon as you have just one document open either the first or second condition must be FALSE, so "Pause until all the following are true:" will only pause when there are no documents open!

2 Likes

Whilst those other solutions do work in isolation, when they’re combined with my export macro - or rather the javascript that the macro is telling Illustrator to run - it’s still calling the next macro too soon.

My hunch is KM thinks all windows have closed when an export or save as progress window pops up.

That implies that the Javascript is running "asynchronously" -- check the cog-wheel drop down in the top-right of the action. I can't see any obvious reason to have that set -- after all, you don't want to continue to the next step until all the files have been exported so you might as well keep it synchronous.

That also suggests a better solution -- since you are exporting each document with a Javascript and want to close each document after it's been exported, close the document in the Javascript! Put it inside the (I'm assuming) "for each document" loop and that will also make your script more versatile as you won't be limited to 8 and only 8 documents.

That should be easy enough to check. I'll see what I can do.

But a big problem here is that we have absolutely no idea of the windows you have open, the way you Illustrator's GUI set up, etc, etc. It would really help if you pop a "Pause" step in after that first action and did a full-screen screenshot at that point and posted it here. Uploading your macro would also help, but if you've got anything "secret" in the Javascript actions you should redact it.

Easiest way to troubleshoot is to reproduce the problem -- I'm happy to have a go as part of my "learning about KM" mission but need more information because, as it stands, what you're doing does work for me for my best-guess of your setup and after I've changed the test's logic.

1 Like