Pause script until specific text appears in the terminal window

I'm applescript stupid but I've done okay with KM. What I'm trying to do is I have a macro that will sucessfully log into my Cpanel Server and auto create a cpmove file for a specific user. I also have another macro that will RSYNC that file down to my 4TB server backup drive and another macro that will append the date to the downloaded file. 3 macros requiring me to trigger them independantly.
What I'd like to do is put them all chained in one macro where the CPMOVE file creation runs the macro is paused until the bottom text on the screen is waiting for input then it runs the 2nd macro and does the same until the RSYNC is done and the bottom line of text is waiting for input again and then run the 3rd macro to rename the file.
I"m also create bash scripts stupid so I have no idea how to do this. Is there anything in KM that can be the trigger to wait/pause until the bottom text matches a specific string?

Thank you for your time and consideration of my issue,
Matt Caswell

This should work. You said "Bottom Text" so I chose "Ends With". And I'm assuming that your Terminal window is your front window (if not, we may have to change something here)

You didn't say what "string" was at the bottom, so I assumed it was "MyPrompt>".

I have no idea if there's a way to do it with AppleScript querying the Terminal app for the text in the window. If so, that might be much less CPU intensive. But even then, it will still be polling in an infinite loop. And most infinite loops are CPU intensive.

I can't thank you enough for your time in attempting to assist me. Yes, the bottom text is the ssh input prompt that returns after the macro event runs.
I'll try this and see what happens. I plan on running it in the middle of the night so resources shouldn't be a problem as my iMac 2020 27" has 128GB of ram.
I'll let you know.
thanks again,
matt

Thanks for your "thanks."

In any case, you should probably test it during the day, manually, rather than wait until overnight to run your first test, because I didn't test it myself.

One possible bug is that sometimes (10% of the time?) Apple's OCR adds a Carriage Return to the end of its string. In which case it's probably better to add a filter action to remove CR's, like this:

It's not working.. this is the last text before the cursor sits waiting for input: / % it has a space after the %. the full last line is: "mattcaz@Matthews-iMac / % "
Your time is valuable so please don't spend any more time on my issue. I appreciate you getting me this far so now I can try and modify it to work.

Unless I'm missing something, this should be much simpler. Do all three macros use Terminal? If so, just put all the commands in one shell script action, and they'll fire in order:

Do Shell Script
first command
second command
third command

But even without that, KM runs things synchronously. For example, consider this macro:

The "All done!" text won't appear until six seconds have passed. So in theory, you can just put your call to macro #2 right as the final step in the first macro, then call macro #3 from the first macro. All three should run sychronously—but without seeing the macros, that's hard to say for certain.

If that doesn't work, you could do something like this, at least if Terminal commands are involved and they don't generate output:

Do Shell Script
your real command
echo "Done"

Save the results to a variable, then in the next step, check to see that the variable is "Done." You could even put this in an Until loop, cycling until the variable had that value.

But I don't think you need to do any of this, if this is all Terminal stuff. Can you post the three macros for us to see?

-rob.

I've tried stringing them in a script but for some reason it never executes the 2nd and 3rd. the first one executes on the Centos 7 server, the next 2 are done in terminal as well but they are doing RSYNC locally and rename Locally. here's the 3 terminal commands with personal stuff XXXX out.

ssh -p XXXX root@XXX.XXX.XX.XX /usr/local/cpanel/scripts/pkgacct xxxxxx /home/_xxxxxx

/usr/bin/rsync -ar -e "ssh -p XXXX" --progress root@IP :/home/_directory /Volumes/4TBHD/

mv cpmove-filename.tar.gz "cpmove-filename $(date +"%m-%d-%y").tar.gz"

Matt

The first command is running remotely; I missed that bit. I think in that case, you could use something like this:

ssh -p XXXX root@XXX.XXX.XX.XX /usr/local/cpanel/scripts/pkgacct xxxxxx /home/_xxxxxx
echo "All done!"

ㅤㅤ
Save the results to a local variable, local_jobDone or whatever. Then, for the next step, try using an Until action, with a Variable condition set to check for local_jobDone being equal to All done!.

Then you could put your next two commands, and given they're both local, I'd put them in one shell script action, one per line.

Edit: Your text was large because you used multiple equals signs as dividers, and an equal sign at the start of a row is Markdown shorthand for...

big and bolding the preceding text.

-rob.

thanks again for all your time and assistance. I'll give it a try.
matt

DOH! I also own Lingon X and with my current scripts I can set times to execute the commands and it runs. 3 days of wasted time because I didn't think it would do it because it doesn't show any progress on my screen.
You didn't waste your time on my issue because now I've learned something else about KM which I use all the time, especially in Quickbooks to mark invoices paid with just one keystroke.

Thanks again for your patience and expertise!
matt

I was out shopping for the last couple of hours, but I'm back now. I don't mind helping you. My time is yours. For some people I spend hours helping them because I like to help people. Big revelation: since you are extremely polite, I like helping you more than some other people on these forums.

@griffman is giving you some great advice. It would be wise to take his advice, but even if you don't take his advice, I think I can still make it work for you. I'm reading your newest messages and I can't quite tell if you've resolved your issue or not.

The main idea in my solution is to examine if the Terminal window "ends" with a prompt, and that can be done using my OCR technique. It may require a little tweaking because OCR sometimes adds newlines. It may even require that you change your prompt. It seems that your prompt contains spaces and special characters, so in order to improve the reliability of OCR, I would change your prompt to something that OCR never fails on. For example, in your case I would probably change the prompt to "ABCDEFG:"

thanks for your time. Using Lingon X and the /usr/local/bin/keyboardmaestro +macroname in KM, I've got it working where I can set the time to execute in Lingon X and it runs the macro automatically.
I have no idea how to change my terminal idle prompt so I'll have to just let it be. I think I'll search for how to do it just so I know.
I seem to be okay for now and thank everyone for taking the time to bail me out.
matt

This is how you change the prompt. (I'm assuming you are using the default shell.)

PROMPT="ABCDEFG:"

No problem. I enjoy coming back to this website several times per day to help people.

Just a reminder that KM macros can have a cron trigger that does almost the same thing.

Thanks for pointing that out. I forgot about Cron.. most of my other timed scripts are running in cron.

The only reason I can think of for this is that the remote command launches a process that runs in the background, so the command you've run finishes and the ssh session terminates before background process completes.

You could include a "then wait until this file appears" into the ssh command -- but the simplest solution is to run the ssh command at 2am and the others at 3am!