You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript: How to self-close all window of a app in a number of times without clicking/contact?

What's an AppleScript that will close all windows(not quitting)of a app after a while without clicking to having contact with it?

for example, I want to close/hide(not quitting) all window of an app after 10 sec without touching it

Would that be possible? Please let me know, how Thanks!

MacBook Pro 13″, macOS 11.2

Posted on Mar 22, 2021 10:45 PM

Reply
Question marked as Top-ranking reply

Posted on Mar 23, 2021 6:50 AM

Both AppleScript and Automator solutions are user interactive and do not run as background processes that you can trigger at will. What you can do is save the AppleScript as an application to your Desktop, where you double-click it when the need arises to close the respective application's windows.

6 replies
Question marked as Top-ranking reply

Mar 23, 2021 6:50 AM in response to Steffine741

Both AppleScript and Automator solutions are user interactive and do not run as background processes that you can trigger at will. What you can do is save the AppleScript as an application to your Desktop, where you double-click it when the need arises to close the respective application's windows.

Mar 23, 2021 5:27 AM in response to Steffine741

It would depend on the application and whether it is supported by an AppleScript dictionary. For instance, let's say I have configured Apple's Preview application to open every image in its own window, and I open three images.


The AppleScript to close all of these open image windows, and leave Preview running would be the following:


use scripting additions

tell application "Preview"
	if it is running then
		delay 10
		close every window of it
	end if
end tell
return



Mar 23, 2021 10:40 AM in response to Steffine741

> how do I keep this code working in the back all the time?


It is possible to do via AppleScript's idle handler - the script will stay in the background and periodically trigger its actions:


on idle
	-- do something
	return 10 -- come back and do it again in 10 seconds
end idle


However, you need to be clearer about how you want/expect this to work. Having the script blinding run every 10 seconds seems counter-intuititive - how do you know that the window in question is not actively being worked on?


Also you have to consider what happens when you normally close a window - for example, most applications would prompt you about whether or not to save the document before closing. How do you expect your script to handle this?


I suspect that what you want is to detect if the application has been idle for 10 seconds (or more), which is much harder to do than just running a periodic check. You'd need some way to detect changes/activity in the application to know if it's been idle or not.

AppleScript: How to self-close all window of a app in a number of times without clicking/contact?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.