How to detect when a app quits

I have an app script that hides the dock and launches two finder windows. I would like it to also unhide the dock when the finder is closed but I am a newb and can’t figure it out.

[AppleScript]
tell application “System Events” to set the autohide of the dock preferences to true
tell application “Finder”
close windows
open (“/Users/arthur/” as POSIX file)
set screenBounds to bounds of window of desktop
set screenWidth to item 3 of screenBounds
set screenHeight to item 4 of screenBounds
set the position of the front Finder window to {0, 0}
set the bounds of the front Finder window to {0, 0, screenWidth * 0.5, screenHeight * 1}
make Finder window
set the position of the front Finder window to {screenWidth * 0.5, 0}
set the bounds of the front Finder window to {screenWidth * 0.5, 0, screenWidth, screenHeight * 1}
set the target of Finder window 1 to (“/Users/arthur/Downloads” as POSIX file)
activate
end tell



Model: Macbook Air M1
AppleScript: 2.7
Browser: Safari 605.1.15
Operating System: Other

You should add 2 handlers to your script and save it as stay-open application. Then the dock will appear and the Finder windows will dissappear, when you quit the running application.


tell application "System Events" to set the autohide of the dock preferences to true
tell application "Finder"
	close windows
	open ("/Users/arthur/" as POSIX file)
	set screenBounds to bounds of window of desktop
	set screenWidth to item 3 of screenBounds
	set screenHeight to item 4 of screenBounds
	set the position of the front Finder window to {0, 0}
	set the bounds of the front Finder window to {0, 0, screenWidth * 0.5, screenHeight * 1}
	make Finder window
	set the position of the front Finder window to {screenWidth * 0.5, 0}
	set the bounds of the front Finder window to {screenWidth * 0.5, 0, screenWidth, screenHeight * 1}
	set the target of Finder window 1 to ("/Users/arthur/Downloads" as POSIX file)
	activate
end tell

on idle
	return 1
end idle

on quit {}
	tell application "Finder" to close windows
	tell application "System Events" to set the autohide of the dock preferences to false
	continue quit
end quit

NOTE: you can assign some keyboard shortcut to QUIT menu item of your application. Or, move mouse pointer to left edge of the screen and quit application when the dock appears permanently.

I’ve never written a script with an idle or quit handler, so I found KniazidisR’s suggestion of interest. The thought occurred to me that the OP may want the script to quit when the Finder windows are closed, and I’ve included a small edit of the handlers of KniazidisR’'s script to do this.

on idle
	tell application "Finder"
		if not (exists window 1) then quit of me
	end tell
	return 1
end idle

on quit {}
	tell application "System Events" to set the autohide of the dock preferences to false
	continue quit
end quit

Save this AppleScript code as a stay open application

property homeFolder : (path to home folder) as text
property downloadsFolder : (path to downloads folder) as text

tell application "System Events" to set the autohide of the dock preferences to true

tell application "Finder"
	set screenBounds to bounds of window of desktop
	close windows
	set finderWindow1 to make new Finder window to folder homeFolder
	set finderWindow2 to make new Finder window to folder downloadsFolder
	set bounds of finderWindow1 to ¬
		{0, 0, (item 3 of screenBounds) * 0.5, item 4 of screenBounds}
	set bounds of finderWindow2 to ¬
		{(item 3 of screenBounds) * 0.5, 0, item 3 of screenBounds, item 4 of screenBounds}
end tell

on idle
	tell application "Finder" to set windowCount to count Finder windows
	if windowCount = 0 then quit
	return 0.1
end idle

on quit
	tell application "System Events" to set the autohide of the dock preferences to false
	continue quit
end quit

3 ready-made solutions are already shown here. They are all working and the choice depends on the preferences of the OP.

Personally, I still see the possibility of an improved version (although I don’t want to develop ready-made code now). I would use for myself instead of 2 Finder windows 1 splitview using AsObjC. Then I would bind the dock open action to the closing of this splitview.

I happened to notice that the OP set the bounds of the first Finder window to {0, 0, screenWidth * 0.5, screenHeight * 1} and wondered if the Finder will allow a window to cover the menu bar. Turns out it won’t:

tell application "Finder"
	set the bounds of Finder window 1 to {0, 0, 500, 500}
	set theBounds to bounds of Finder window 1 --> {0, 23, 500, 523}
end tell

Thanks all, exactly what I was looking for. I love helpful active communities such as this.

I kinda hoped it would but i can live with it ???

The only other method I know of to reposition a Finder window is System Events but it won’t let you cover the menu bar either.

tell application "System Events" to tell process "Finder"
	set position of window 1 to {0, 0}
	set size of window 1 to {500, 500} -- this is not required
end tell

Okay, so my version of the script is now bound to a keyboard shortcut, and I’m happy :). Here it is if anyone is interested. Also, open to critiques if I can optimize something or made a stupid oversite. thanks for all the help


tell application "System Events" to set the autohide of the dock preferences to true
tell application "System Events" to set the autohide menu bar of the dock preferences to true
tell application "Finder"
	close windows
	open ("System:Users:arthur")
	set screenBounds to bounds of window of desktop
	set screenWidth to item 3 of screenBounds
	set screenHeight to item 4 of screenBounds
	set the position of the front Finder window to {0, 0}
	set the bounds of the front Finder window to {0, 0, screenWidth * 0.5, screenHeight * 1}
	make Finder window
	set the position of the front Finder window to {screenWidth * 0.5, 0}
	set the bounds of the front Finder window to {screenWidth * 0.5, 0, screenWidth, screenHeight * 1}
	set the target of Finder window 1 to ("System:Users:arthur:Downloads")
	activate
end tell

on idle
	tell application "Finder" to set windowCount to count Finder windows
	if windowCount = 0 then
		tell application "System Events" to set the autohide of the dock preferences to false
		tell application "System Events" to set the autohide menu bar of the dock preferences to false
		quit
	end if
	return 0.1
end idle