Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; Profile Manager
- ; Monitors active applications and switches AutoHotkey profiles accordingly
- #NoEnv
- #SingleInstance Force
- #Persistent
- ; Configuration
- ScriptsPath := "C:\Users\user\AutoHotkey\"
- CheckInterval := 500 ; Check every 0.5 second(s)
- ; Global variables
- CurrentProfile := ""
- CurrentScriptPID := 0
- LastActiveProcess := ""
- ; Application to script mapping
- AppProfiles := {}
- ; Application Group 1
- AppProfiles["iTunes.exe"] := "AHK_iTunes.ahk" ; Example ⸺ AHK_iTunes.ahk is in another pasted script, and includes dual-layer functionality and infinite mouse wheel scrolling
- ; Application Group 2
- AppProfiles["App1.exe"] := "Profile1.ahk"
- AppProfiles["App2.exe"] := "Profile2.ahk"
- ; Application Group 3
- AppProfiles["App7.exe"] := "Profile7.ahk"
- AppProfiles["App8.exe"] := "Profile8.ahk"
- ; Start monitoring
- SetTimer, CheckActiveWindow, %CheckInterval%
- CheckActiveWindow:
- WinGet, ActiveProcess, ProcessName, A
- ; Only check if the process actually changed (performance optimization)
- if (ActiveProcess != LastActiveProcess) {
- LastActiveProcess := ActiveProcess
- ; Determine which profile to use
- TargetScript := AppProfiles[ActiveProcess]
- if (!TargetScript) {
- TargetScript := "DesktopProfile.ahk" ; Default fallback
- }
- ; Switch profile if different from current
- if (TargetScript != CurrentProfile) {
- SwitchToProfile(TargetScript)
- }
- }
- return
- SwitchToProfile(ScriptName) {
- global ScriptsPath, CurrentScriptPID, CurrentProfile
- ; Don't kill the Profile Manager itself
- ProfileManagerPID := DllCall("GetCurrentProcessId")
- if (CurrentScriptPID > 0 && CurrentScriptPID != ProfileManagerPID) {
- Process, Close, %CurrentScriptPID%
- CurrentScriptPID := 0
- }
- ; Don't relaunch the same profile
- if (ScriptName = CurrentProfile)
- return
- ; Launch new profile script
- FullPath := ScriptsPath . ScriptName
- if (FileExist(FullPath)) {
- Run, "%A_AhkPath%" "%FullPath%", , , NewPID
- CurrentScriptPID := NewPID
- CurrentProfile := ScriptName
- ; Optional: Show tray tip for debugging
- ; TrayTip, Profile Manager, Switched to: %ScriptName%, 1, 1
- } else if (ScriptName != "DesktopProfile.ahk") {
- ; Only fall back to Desktop if not already trying Desktop
- SwitchToProfile("DesktopProfile.ahk")
- }
- }
- ; Cleanup on exit
- OnExit, CleanupAndExit
- CleanupAndExit:
- if (CurrentScriptPID > 0) {
- Process, Close, %CurrentScriptPID%
- }
- ExitApp
- ; Hotkey to manually reload ProfileManager (optional)
- ^!r::Reload
- ; Hotkey to show current profile (optional for debugging)
- ^!p::
- TrayTip, Profile Manager, Current Profile: %CurrentProfile%, 2, 1
- return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement