FuPeiJiang / VD.ahk

Windows Virtual Desktop, AutoHotkey, Windows 11 support, Windows Server 2022, switch desktop, move window(wintitle) to current desktop; createDesktop, PinWindow, getCount, getDesktopNumOfWindow -> mute all windows in Virtual Desktop
MIT License
348 stars 47 forks source link

WinExist in another virtual desktop #68

Open jdmarch opened 6 months ago

jdmarch commented 6 months ago

Use case: It would be great to have a hotkey which checks whether a specific app (window) is open in any Virtual Desktop, and if it is, activates it in its already-existing desktop. If it does not yet exist, I would open it in the current desktop, but YMMV. Thanks! (Win 11)

FuPeiJiang commented 6 months ago

what do you mean ? can you write pseudocode ? 3 possibilities I can think of: 1)

if (VD.WinExistInDesktopNum(winTitle,desktopNum:=2)) {
  WinActivate(winTitle)
} else {
  Run(command)
}

2)

if (VD.WinExistInAnyDesktopNum(winTitle) {
  WinActivate(winTitle)
} else {
  Run(command)
}

3)

if (VD.WinExistInDesktopNum(winTitle,desktopNum:=2)) {
  WinActivate(winTitle)
} else {
  VD.MoveWindowToCurrentDesktop(winTitle)
  WinActivate(winTitle)
}
Morgenkaff commented 4 months ago

Personally I would like

if (VD.WinExistInAnyDesktopNum(winTitle) {
  WinActivate(winTitle)
} else {
  Run(command)
}

and

if (VD.WinExistInNotCurrentDesktop(winTitle) {
  WinActivate(winTitle)
} else {
  Run(command)
}

Unless there is is another way to know if a given window exists, and then move it to current desktop.

FuPeiJiang commented 4 months ago

@Morgenkaff WinExist with DetectHiddenWindows 1 will search in all VD

DetectHiddenWindows 1
if (WinExist(winTitle)) {
    VD.MoveWindowToCurrentDesktop(winTitle)
} else {
    Run(command)
}

getDesktopNumOfWindow probably searches in all VD too

if (VD.getDesktopNumOfWindow(winTitle)!==VD.getCurrentDesktopNum()) {
    VD.MoveWindowToCurrentDesktop(winTitle)
} else {
    Run(command)
}
mariusmg commented 4 months ago

Hello,

Would it be possible to also add a version of MoveWindowToCurrentDesktop which supports PID ? On a hotkey shortcut, i want to check if a specific process exists then move the window to current virtual desktop and then activate it :

VD.MoveWindowToCurrentDesktop("ahk_pid" PID)
WinActivate("ahk_pid" PID)

Right now this seems to only work with window titles.

FuPeiJiang commented 4 months ago

@mariusmg

VD.MoveWindowToCurrentDesktop("ahk_pid" PID)
WinActivate("ahk_pid" PID)

does this code work for you ? if yes, I don't know what you're asking, because this is by PID if no, what is the program that it doesn't work for ? because it will probably work for notepad.exe

On a hotkey shortcut, i want to check if a specific process exists then move the window to current virtual desktop and then activate it :

if (VD.getDesktopNumOfWindow("ahk_exe notepad.exe")!==VD.getCurrentDesktopNum()) {
VD.MoveWindowToCurrentDesktop("ahk_exe notepad.exe")
}

do you really need PID when you can use ahk_exe ?

mariusmg commented 4 months ago

@FuPeiJiang

You're right, works fine with ahk_exe 👍

Thanks for suggestion and VD.ahk

Morgenkaff commented 4 months ago

@Morgenkaff WinExist with DetectHiddenWindows 1 will search in all VD

DetectHiddenWindows 1
if (WinExist(winTitle)) {
    VD.MoveWindowToCurrentDesktop(winTitle)
} else {
    Run(command)
}

getDesktopNumOfWindow probably searches in all VD too

if (VD.getDesktopNumOfWindow(winTitle)!==VD.getCurrentDesktopNum()) {
    VD.MoveWindowToCurrentDesktop(winTitle)
} else {
    Run(command)
}

I've somehow overlooked both options. Works perfectly. Thanks!