r/MacOS•
DyIsexia

Would appreciate some help with optimizing my AppleScripts

Help

I have two applescripts that interact with each other, set to different hotkeys. to put it simply (because I suck at AppleScript and honestly don’t know what everything does because an LLM did most of the work), one minimizes a window and does some other stuff so that when I activate the other, it’ll know if I just minimized a window and so will unminimize it, otherwise it’ll fullscreen the selected window with control+option+f. Doed anybody here know if you can optimize these to make the processes quicker in any way without compromising functionality? Thanks. Even just general pointers would be nice.

Minimize/Save:

set stateFile to POSIX path of (path to library folder from user domain) & “Caches/btt_last_window.txt”

tell application “System Events”
set frontProc to first application process whose frontmost is true
set appName to name of frontProc

tell frontProc
try
set win to first window
set winName to name of win

	\-- minimize window  
	set value of attribute "AXMinimized" of win to true  
	  
	\-- save state  
	set fileRef to open for access stateFile with write permission  
	set eof fileRef to 0  
	write (appName & "||" & winName) to fileRef  
	close access fileRef  
end try  

end tell

end tell

Dynamic Unminimize/Fullscreen:

set stateFile to POSIX path of (path to library folder from user domain) & “Caches/btt_last_window.txt”

set savedApp to “”
set savedWin to “”

– read saved state
try
set fileRef to open for access stateFile
set savedData to read fileRef
close access fileRef

set AppleScript's text item delimiters to "||"
set savedApp to text item 1 of savedData
set savedWin to text item 2 of savedData

end try

set didRestore to false

– TRY RESTORE FIRST
try
tell application “System Events”
set frontProc to first application process whose frontmost is true
set currentApp to name of frontProc

if currentApp is equal to savedApp then  
	tell frontProc  
		set win to first window whose name is savedWin  
		set value of attribute "AXMinimized" of win to false  
	end tell  
	  
	do shell script "rm -f " & quoted form of stateFile  
	set didRestore to true  
end if  

end tell

end try

– FALLBACK: FULLSCREEN (FIXED WITH DELAY)
if didRestore is false then
tell application “System Events”
delay 0.1
tell process (name of first application process whose frontmost is true)
keystroke “f” using {control down, option down}
end tell
end tell
end if

	\-- save state  
	set fileRef to open for access stateFile with write permission  
	set eof fileRef to 0  
	write (appName & "||" & winName) to fileRef  
	close access fileRef  
end try  

end tell

end tell

Posted Jun 13, 2026, 11:42 PM UTC

02
Loading comments…