Last active
September 1, 2025 19:40
-
-
Save anvanvan/cafcccb80a062b2c396b6be42441186b to your computer and use it in GitHub Desktop.
Universal App Toggle Script for macOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/osascript | |
| # Toggle macOS App Window State | |
| # | |
| # This script provides smart window toggling for any macOS application: | |
| # - If app is not running → launches and activates it | |
| # - If app is minimized → restores and brings to front | |
| # - If app is in background → brings to front | |
| # - If app is in foreground → minimizes to dock | |
| # | |
| # Usage: | |
| # 1. Change 'appName' to your target application | |
| # 2. Save as .scpt or .applescript file | |
| # 3. Run via Terminal: osascript toggle-app.scpt | |
| # 4. Or integrate with Raycast, Alfred, Keyboard Maestro, etc. | |
| # | |
| # Requirements: | |
| # - macOS with AppleScript support | |
| # - Accessibility permissions for Script Editor/Terminal | |
| # (System Settings → Privacy & Security → Accessibility) | |
| # | |
| # Author: An Van (@anvanvan) | |
| # License: MIT | |
| # Configuration - Change this to your target app | |
| set appName to "Claude" -- Examples: "Safari", "Slack", "Terminal", "Code" | |
| # Check if the application process is currently running | |
| tell application "System Events" | |
| set appRunning to (name of processes) contains appName | |
| end tell | |
| if not appRunning then | |
| # App not running → Launch and bring to foreground | |
| activate application appName | |
| else | |
| # App is running → Determine current state and toggle accordingly | |
| tell application "System Events" | |
| tell process appName | |
| # Get current window state | |
| set isFrontmost to frontmost | |
| set windowCount to count of windows | |
| if windowCount > 0 then | |
| # App has windows → Check if minimized | |
| set isMinimized to value of attribute "AXMinimized" of window 1 | |
| if isFrontmost and not isMinimized then | |
| # App is in foreground and visible → Minimize to dock | |
| # This is the "hide" action when user wants to get app out of the way | |
| set value of attribute "AXMinimized" of window 1 to true | |
| else | |
| # App is either in background or minimized → Restore and activate | |
| # First, make the process frontmost | |
| set frontmost to true | |
| # If minimized, restore the window | |
| if isMinimized then | |
| set value of attribute "AXMinimized" of window 1 to false | |
| end if | |
| # Ensure window is raised to top of window stack | |
| # This handles edge cases where window might be behind others | |
| perform action "AXRaise" of window 1 | |
| end if | |
| else | |
| # App is running but has no windows (rare case) | |
| # Some apps can run without windows (menu bar apps, background processes) | |
| # Attempt to make it frontmost which should create/show a window | |
| set frontmost to true | |
| end if | |
| end tell | |
| end tell | |
| # Failsafe: If app still isn't frontmost after our attempts, force activate | |
| # This handles stubborn apps that don't respond to standard window management | |
| tell application "System Events" | |
| if not (frontmost of process appName) then | |
| activate application appName | |
| end if | |
| end tell | |
| end if | |
| # Note: Some apps may behave differently: | |
| # - Electron apps might need additional handling | |
| # - Menu bar only apps won't minimize properly | |
| # - Multi-window apps will only toggle the first window | |
| # | |
| # For multi-window support, modify the minimize section to: | |
| # repeat with w in windows | |
| # set value of attribute "AXMinimized" of w to true | |
| # end repeat |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment