r/applescript • u/DyIsexia • Jun 14 '26
Would appreciate some help with optimizing my AppleScripts
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. Does 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. Sorry for the garbage formatting.
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
3
Jun 14 '26
[removed] — view removed comment
3
u/DyIsexia Jun 14 '26
If anything I’m that AI guy lol, I don’t even know how to write AppleScripts. At the same time that’s the reason why I wrote the post, is because I have no idea if what it outputted was good or not, so I appreciate that you’ve confirmed it’s decent!
1
u/copperdomebodha Jun 15 '26
Not to be that anti-AI guy, but chatGPT and Claude both smash headlong into every AppleScript with code that is not entirely wrong. Getting from there to functional requires either a massive number of iterations between relaying errors or a significant AppleScript knowledge to begin with.
In this example, there is no recognition of repetitive tasks, no usage of basic AppleScript functionality ( ie Properties, handlers etc ) and not even a suggestion of the process flow. But generally not terribly far off as far as the actual code goes.
Actual tight, clever AppleScript code is quite entirely still dependent on the user. I use them too, but more like reading a blog post than expecting final code.
1
u/copperdomebodha Jun 15 '26
Posting code correctly will allow others to more easily run your code and provide feedback. As-is those smart quotes have to get replaced. This code didn't compile here. Too many end statements. So, I don't know what it was producing there.
Here is my reworking of your posted code. This will minimize the frontmost window of the frontmost app ( unless running in a script editor which will cause it to target the finder ) and / or un-minimize the last window that it minimized.
Single script. Just run it and it should do the correct thing.