Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; ############################################################################
- ; ## Profile Manager v2
- ; ## Monitors active applications and switches AutoHotkey profiles accordingly.
- ; ##===========================================================================
- #Requires AutoHotkey v2.0
- #SingleInstance Force
- A_WorkingDir := A_ScriptDir
- if !A_IsAdmin {
- Run('*RunAs "' A_ScriptFullPath '"')
- ExitApp()
- }
- Persistent()
- InstallKeybdHook()
- InstallMouseHook()
- ; ############################################################################
- ; ## Configuration
- ; ##===========================================================================
- #Include "ProfileManagerConfig.ahk"
- ScriptsPath := "C:\Users\user\Apps & Utilities\Custom Scripts - AutoHotkey\"
- CheckInterval := 500 ; Check every 0.5 seconds
- ; ############################################################################
- ; ## Global Variables
- ; ##===========================================================================
- global AppProfiles := GetAppProfiles()
- global ComplexProfiles := GetComplexProfiles()
- global SpecializedScripts := GetSpecializedScripts()
- global CurrentProfile := ""
- global CurrentScriptPIDs := []
- global SpecializedScriptPIDs := Map()
- global LastActiveProcess := ""
- global LastActiveTitle := ""
- global LastAdobeAutohide := "off"
- ; ############################################################################
- ; ## Process Helper Functions
- ; ##===========================================================================
- ; Returns an array of PIDs for all processes matching the given exe name
- ProcessGetList_PM(Name) {
- list := []
- for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process where Name='" Name "'")
- list.Push(process.ProcessId)
- return list
- }
- ; Returns the command line of a process given its PID
- ProcessGetCommandLine(PID) {
- for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process where ProcessId=" PID)
- return process.CommandLine
- return ""
- }
- ; ############################################################################
- ; ## Main Monitoring Loop
- ; ##===========================================================================
- SetTimer(CheckActiveWindow, CheckInterval)
- MonitorSpecializedScripts() ; Initial check
- SetTimer(MonitorSpecializedScripts, 3000)
- CheckActiveWindow() {
- global LastActiveProcess, LastActiveTitle, CurrentProfile, AppProfiles, ComplexProfiles
- try {
- ActiveProcess := WinGetProcessName("A")
- ActiveTitle := WinGetTitle("A")
- } catch {
- return ; No active window, skip this check
- }
- ; Only check if the active window's process or title has changed (performance)
- if (ActiveProcess != LastActiveProcess || ActiveTitle != LastActiveTitle) {
- LastActiveProcess := ActiveProcess
- LastActiveTitle := ActiveTitle
- ; Determine which profile to use, defaulting to Desktop
- TargetProfile := ""
- ; 1. Check complex profiles (process + title match)
- for _, rule in ComplexProfiles {
- if (ActiveProcess = rule.process && ActiveTitle = rule.title) {
- TargetProfile := rule.script
- break
- }
- }
- ; 2. If no complex match, check simple app profiles (process match)
- if (TargetProfile = "") {
- TargetProfile := AppProfiles.Has(ActiveProcess) ? AppProfiles[ActiveProcess] : "Desktop.ahk"
- }
- ; Compare profiles to see if a switch is needed
- isSame := false
- if (Type(TargetProfile) = Type(CurrentProfile)) {
- if IsObject(TargetProfile) {
- if TargetProfile.Length == CurrentProfile.Length {
- isSame := true
- loop TargetProfile.Length {
- if (TargetProfile[A_Index] != CurrentProfile[A_Index]) {
- isSame := false
- break
- }
- }
- }
- } else {
- if (TargetProfile = CurrentProfile) {
- isSame := true
- }
- }
- }
- if (!isSame) {
- SwitchToProfile(TargetProfile)
- }
- }
- }
- ; ############################################################################
- ; ## Specialized Script Monitoring
- ; ##===========================================================================
- MonitorSpecializedScripts() {
- global ScriptsPath, SpecializedScripts, SpecializedScriptPIDs
- for scriptName, exeList in SpecializedScripts {
- isTargetRunning := false
- for _, exeName in exeList {
- if WinExist("ahk_exe " . exeName) {
- isTargetRunning := true
- break
- }
- }
- isScriptRunning := false
- if (SpecializedScriptPIDs.Has(scriptName)) {
- pid := SpecializedScriptPIDs[scriptName]
- if (ProcessExist(pid)) {
- isScriptRunning := true
- } else {
- ; The script terminated itself, so remove it from our map
- SpecializedScriptPIDs.Delete(scriptName)
- }
- }
- if (isTargetRunning && !isScriptRunning) {
- ; Launch the script
- fullPath := ScriptsPath . scriptName
- if (FileExist(fullPath)) {
- Run('*RunAs "' A_AhkPath '" "' fullPath '"', , , &newPID)
- SpecializedScriptPIDs[scriptName] := newPID
- TrayTip('Profile Manager', 'Launched: ' . scriptName, 1)
- }
- }
- }
- }
- ; ############################################################################
- ; ## Core Functions
- ; ##===========================================================================
- SwitchToProfile(NewProfile) {
- global ScriptsPath, CurrentScriptPIDs, CurrentProfile
- ProfileManagerPID := DllCall("GetCurrentProcessId")
- ; Close the existing scripts
- for pid in CurrentScriptPIDs {
- if (pid > 0 && pid != ProfileManagerPID) {
- ProcessClose(pid)
- }
- }
- CurrentScriptPIDs := []
- CurrentProfile := NewProfile
- ScriptsToLaunch := IsObject(NewProfile) ? NewProfile : [NewProfile]
- for ScriptName in ScriptsToLaunch {
- FullPath := ScriptsPath . ScriptName
- if (FileExist(FullPath)) {
- Run('*RunAs "' A_AhkPath '" "' FullPath '"', , , &NewPID)
- CurrentScriptPIDs.Push(NewPID)
- TrayTip() ; Clear any previous notifications
- TrayTip('Profile Manager', 'Switched to: ' ScriptName, 1)
- }
- }
- ; If no scripts were launched for the profile (e.g., file not found),
- ; and it's not the default desktop profile, fall back to desktop.
- if (CurrentScriptPIDs.Length = 0) {
- PrimaryNewProfile := IsObject(NewProfile) ? NewProfile[1] : NewProfile
- if (PrimaryNewProfile != "Desktop.ahk") {
- SwitchToProfile("Desktop.ahk")
- }
- }
- }
- ; ############################################################################
- ; ## Hotkeys
- ; ##===========================================================================
- ; Hotkey to manually reload ProfileManager
- ^!r:: { ; Control + Alt + R
- if !A_IsAdmin {
- Run('*RunAs "' A_ScriptFullPath '"')
- ExitApp()
- } else {
- Reload()
- }
- }
- ; Hotkey to show current profile (for debugging)
- ^!p:: { ; Control + Alt + P
- ProfileText := ""
- if IsObject(CurrentProfile) {
- ProfileText := ""
- for i, script in CurrentProfile {
- ProfileText .= script . (i == CurrentProfile.Length ? "" : ", ")
- }
- } else {
- ProfileText := CurrentProfile
- }
- TrayTip('Profile Manager', 'Current Profile: ' ProfileText, 2)
- }
- ; ############################################################################
- ; ## Exit Handling
- ; ##===========================================================================
- OnExit(CleanupAndExit)
- CleanupAndExit(*) {
- global CurrentScriptPIDs, SpecializedScriptPIDs
- for pid in CurrentScriptPIDs {
- if (pid && pid > 0) {
- ProcessClose(pid)
- }
- }
- for _, pid in SpecializedScriptPIDs {
- if (pid && pid > 0) {
- ProcessClose(pid)
- }
- }
- ExitApp()
- }
Advertisement
Add Comment
Please, Sign In to add comment