Ventura – Terminal Won't Quit Via AppleScript

Hi Nige_S,

So your example works great, thank you. The mouse I am trying to connect to is sometimes item 2 in scroll list but the order can change, so can I select an item based on the item name?

I have another question which is also based on AppleScript but is not a menu item. I have a script that runs in terminal to delete all hidden Time Machine thinlocalsnapshots. This actually still works, but my original script before Ventura I would quit Terminal once the process is completed. It's this quitting terminal that no longer works, do you have any suggestions?

set isBusy to true

repeat until isBusy is false
   tell application "Terminal"
      tell window 1
         set isBusy to busy as boolean --> Test if busy
      end tell
   end tell
   delay 1 --> Check every 2 seconds
end repeat

--display dialog "Terminal is No Longer Busy!"
delay 5
quit application "Terminal"

That script has lost a lot when being transferred to the Forum. Try again, putting it into a code block (the </> icon at the top of the edit box).

Is the process interactive, maybe requiring your password? Otherwise I'm not sure why you're bothering with Terminal at all. Indeed, why are you even deleting them? The OS's management of these seems much improved these days, so if this is a hold-over from a previous OS I suggest you try doing without for a while and seeing if you do actually have a problem.

Hi @Nige_S,

See script below now properly formatted.

The script used to work prior to Ventura and he's used in conjunction with another script that deletes all the time machine snapshots.

Connect a Bluetooth Mouse Using a Macro

I am looking for an AppleScript that checks when the Terminal is no longer busy, wait five seconds and then quits app. Any help you can offer will be gratefully received.

set isBusy to true

repeat until isBusy is false
   tell application "Terminal"
      tell window 1
         set isBusy to busy as boolean --> Test if busy
      end tell
   end tell
   delay 1 --> Check every 2 seconds
end repeat

--display dialog "Terminal is No Longer Busy!"
delay 5
quit application "Terminal"

That's the Bluetooth thread -- have you got the actual Terminal script/command you are running, in case there's something funky there?

Meanwhile, this works for me:

tell application "Terminal"
	do script "ping -c 30 192.168.0.1"
	delay 2
	repeat while busy of window 1
		delay 1
	end repeat
    delay 5
	quit
end tell

Test the above and, if it works, try substituting your desired Terminal command for the ping command.

It takes time for the Terminal to fire off the command and become busy, so you might have to increase that first delay.

Again, though -- I'd urge you to reconsider the whole thing. I'm not seeing the old local snapshots problems in Ventura and they should self-manage; if you have a lot of "churn" that makes them overly large you should change your data management so that the rapidly-changing data that you don't want to back up is in TM-excluded folders; and if you've so little free disk space that, after all the above, you still have problems then you need to make more space to prevent other bad things happening.

Sorry my link was wrong & I couldn't see how to edit post.

Sadly I have tried your script and it doesn't work here. My programming skills are very limited but what is :-

do script "ping -c 30 192.168.0.1"

Doesn't work in what way?

Because it is in a tell application "Terminal" block,

do script "ping -c 30 192.168.0.1"

...will open a new Terminal window and in that run the command "try and ping the IP address 192.168.0.1 30 times" -- all we're doing is running a totally innocuous command that will keep the Terminal window busy for 30 seconds so we can test the loop.

Nige_S

So two things. I didn't realise that the script would take that long to execute and as no window appeared I didn't think anything had happen. I should point out I am visually impaired and sometimes I miss certain things on the screen. Based on this, I have run the script again, but it still doesn't quite terminal.

I have attached a sample macro that I have added your script to which is activated only in finder using F12.

Andrew

Test Nige_S Macro.kmmacros (2.6 KB)

My bad -- I should have included an activate command to bring Terminal to the front to make things more obvious.

There's quite a pause at the end while ping gathers its stats. Terminal will then return to the command prompt, at which point it is no longer busy and your 5 second final delay starts. Terminal should quit after that.

Does Terminal put up a dialog asking if it should quit? It may that you've changed your default Terminal profile to "Always ask before closing":

Otherwise you could try putting a KM "Quit Application" action after the "Execute AppleScript". This version reduces the number of pings so you don't have to wait so long and will give 3 beeps after the ping has finished (but before the AppleScript completes) so you can track things more easily, the extra "Quit" action just in case, and a final "Play Bottle" on completion:

Test Nige_S Macro v2.kmmacros (3.6 KB)

Image

Does Terminal put up a dialog asking if it should quit? - No

I have run your macro and terminal launches but nothing else happens.

That almost looks like KM's Automation permissions for Terminal have been turned off. Open System Settings->Privacy & Security->Automation and make sure that KM is allowed to control Terminal:

@Nige_S,

Thank you so much, that was the problem. As you can see Terminal was enabled in KB Engine only, so have now enabled it in main app as well and the macro runs fine. Now my original script to close Terminal works but I have used yours as it seems more logical.

I have learnt a lot from you and are most grateful. I have been playing with your macro to understand how it works and found if I remove "activate" I get the warning in the second screen shot below. Is there a way to prevent this warning in NON Applescripts, i.e. in a KM macro?

Thanks again and if anybody is interested in my original macro to delete TimeMachine 'local snapshots' that can use up storage space on your computer, I have attached my macro.

Andrew

Delete TimeMachine hidden files.kmmacros (5.9 KB)

Screenshot 2023-03-25 at 08.40.05

There's probably a better way to do this which will avoid all the palaver with AppleScript and the Terminal -- you can run that command you are entering in Terminal directly from KM, using an "Execute Shell Script" action.

Delete TimeMachine hidden files v2.kmmacros (2.6 KB)

Image

You get that when you try to close a Terminal window that has a still-running process -- which you generally won't want to do because you'll want the process to complete first. (I'm not sure why taking activate out is causing a problem -- it didn't for me in the script I posted earlier.)

Easiest way to avoid it is to not use Terminal, but to use the "Execute Shell Script" action instead. Save Terminal for when you need to interact with your shell script, to type in a password for instance. Otherwise, using your busy test will generally work.

Hi Nige_S

Your "Execute Shell Script" works but because there is nothing visually happening I am not sure its running. I will use the AppleScript version with your improved quit method.

I have another new topic which maybe you can help with which I will title "Detect when macOS wakes" which I will raise soon if you would like to read that.

Andrew

You could change the action so that it "Displays results in a window" -- that way each run would give you the same "report" as if you did it in Terminal. Depends on whether you like these things to happen in the background or to be more obvious.