So I'm trying to create a flow where if I hit cmd+W BTT will run a script that checks if all windows have been closed and Quit the app. I managed to do this with this by creating a flow of:
- cmd+W
- wait 3 sec
- Run this script (Stickies in this example)
set timeoutLimit to 5
set startTime to (current date)
repeat
tell application "System Events"
if application process "Stickies" exists then
set stickiesProcess to application process "Stickies"
set stickyWindows to windows of stickiesProcess
-- Quit Stickies only if no windows are open
if (count of stickyWindows) is 0 then
tell application "Stickies" to quit
exit repeat
end if
end if
end tell
-- Check if 5 seconds have passed
set elapsedTime to (current date) - startTime
if elapsedTime ≥ timeoutLimit then
exit repeat
end if
end repeat
However, I want to create a CONDITIONAL ACTIVATION GROUP for specific apps, and change this flow by altering the above script to instead use a BTT variable to quit the app. Example:
- Set Active App Name to Persistant Variable (THIS WORKS SUCCESSFULLY)
tell application "BetterTouchTool"
set_persistent_string_variable "lastActiveApp" to get_string_variable "active_app_name"
end tell
- cmd+W
- wait 3 sec
- Run Script that quits the app name under the variable "lastActiveApp" <-THIS IS THE STEP IM STRUGGLING WITH
Tried asking ChatGPT and Gemini with no help. This is what ChatGPT came up with but doesn't work:
set timeoutLimit to 5
set startTime to (current date)
-- Retrieve the last active app name from BTT persistent variable
tell application "BetterTouchTool"
set lastActiveApp to get_string_variable "lastActiveApp"
end tell
repeat
tell application "System Events"
-- Check if the last active app process exists
if application process lastActiveApp exists then
set appProcess to application process lastActiveApp
set appWindows to windows of appProcess
-- Quit the app only if no windows are open
if (count of appWindows) is 0 then
tell application lastActiveApp to quit
exit repeat -- Exit the loop after quitting
end if
end if
end tell
-- Check if 5 seconds have passed (timeout)
set elapsedTime to (current date) - startTime
if elapsedTime ≥ timeoutLimit then
exit repeat
end if
delay 0.1 -- Small delay to avoid excessive CPU usage
end repeat
Anyone got any ideas? Would be much appreciated.