Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature request] Launch a program at the current desktop #15

Closed
llinfeng opened this issue Jul 13, 2022 · 10 comments
Closed

[Feature request] Launch a program at the current desktop #15

llinfeng opened this issue Jul 13, 2022 · 10 comments

Comments

@llinfeng
Copy link

llinfeng commented Jul 13, 2022

Motivation: The OneNote for Windows 10 windows are not managed by the Task View thing properly - if I have two Virtual Desktops (VD1 and VD2), with have one OneNote window opened in VD1, all new OneNote windows should open in VD1. That is, if I were at VD2 and try to start a new window of OneNote, VD1 is activated and the new OneNote window is started there. I cannot tell whether the new OneNote window starts first, or the switch of VD.

This behavior won't change whether I open OneNote through the Start menu, or using AHK's run command.

A tentative solution is to have a VD.run("exe_path", "VD_num") function that directly starts something at the numbered virtual desktop. If the second argument is left empty, the VD.run command makes sure the program is launched in the current desktop.

As a side note, it also helps to have a function called VD.goToPreviousDesktop(). (Please let me know if this should be documented in a separate issue.) In a similar vein, based on what we have, it may also help to allow for arguments that move a window to the left/right.

  1. VD.MoveWindowToDesktopNum("A","+1"), and
  2. VD.MoveWindowToDesktopNum("A","-1")
@FuPeiJiang
Copy link
Owner

let me rephrase / did I understand correctly ?

context:
you are in VD2
got:
you are moved to VD1, OneNote is started in VD1
want:
you stay in VD2, OneNote is started in VD2

@FuPeiJiang
Copy link
Owner

if OneNote is changing your virtual desktop, I can't prevent it
I can save your current virtual desktop
start OneNote
move back to saved virtual desktop, move OneNote to saved virtual desktop

though it is pretty much impossible since Start is by PID, a PID can have 2 windows,
a PID can just inform the other PID, and quit itself, while the other PID creates another window
how would you know from the PID you started, that this newly created window (on a different existing PID) was the result of it?
you can only check if another window of certain wintitle or processName was created, the newly created one must be it
wait, get WinTitle, get PID, var map1 map[hwnd]bool=get all windows of that PID, Start, get all windows of that PID, newly created windows in this timespan are the ones you caused.

@llinfeng
Copy link
Author

Ha, to clarify, here are two things I had in mind when opening this issue:

  1. As you said, when launching OneNote specifically, it helps to keep it in the current virtual desktop and switching back to the current desktop if VD-switching happens unexpectedly;
  2. Further, it may help to start a window in a "hidden" VD, by literally "sending the window to the destination VD". This helps to create an easy-to-use launcher script, that populates all virtual desktops with the programs needed in that destination.

Towards the end of your most recent post, it looks like you found a way to identify the new OneNote window? That's great!

@FuPeiJiang
Copy link
Owner

thanks for the use cases 1. and 2., allows me to write functions based on use case, instead of functionality : Launch a program at the current desktop, which I would optimize wrongly, idk, maybe it's even better to have both functionality and use case


@llinfeng
test https://github.com/FuPeiJiang/VD.ahk/tree/run_in_VD

run in VD.ahk, run in VD lock.ahk, VD.Run_lock_VD name to be (maybe) redecided

I used https://chris.dziemborowicz.com/apps/hourglass/ to test, you should test using OneNote (which I didn't want to install)

@llinfeng
Copy link
Author

@FuPeiJiang I tested the syntax in run in VD.ahk and it worked well for launching and sending gvim.exe, for example. Though, could you explain what is the VD.Run_lock_VD function in run in VD lock.ahk intended for?
In an effort of trying to make sense from the following line with the "lock" function - is it meant to keep the focus at VD 2, and launch a new window in VD 3? (Or, to flip it, to keep the focus at VD 3, and launch a new window in VD 2?)

VD.Run_lock_VD("""C:\Program Files (x86)\Hourglass\Hourglass.exe""","","","","Hourglass.exe",2,3)

For this "OneNote for Windows 10" app, nope, all windows were opened in the current virtual desktop, with nothing forwarded to the intended destination. Here is the window information for those OneNote windows in case it may help us get dispatched to the intended VD.

---------------------------
Warning
---------------------------
Paperless ‎- OneNote for Windows 10 
ahk_class ApplicationFrameWindow 
ahk_exe ApplicationFrameHost.exe 
hostname AW-R5
NumMonitor: 6, 2.666666 
PrimaryMonintor: 1200x1920
---------------------------
OK   
---------------------------

@FuPeiJiang
Copy link
Owner

it's to launch a new window in VD 2, and set focus at VD 3

window_desktopNum, your_desktopNum

VD.ahk/_VD.ahk

Line 680 in c2a7e4b

Run_lock_VD(Target, WorkingDir, this_titleName, this_class, this_processName, window_desktopNum, your_desktopNum) {


for OneNote

Paperless ‎- OneNote for Windows 10
ahk_class ApplicationFrameWindow
ahk_exe ApplicationFrameHost.exe

can you send me full script that didn't work ?

if you did it like this, I don't think it will work

VD.Run_lock_VD("""OneNote.exe""","","Paperless ‎- OneNote for Windows 10","ApplicationFrameWindow","ApplicationFrameHost.exe",2,VD.getCurrentDesktopNum())

from where can I install your version of OneNote ?
from Microsoft Store ? https://apps.microsoft.com/store/detail/onenote/9WZDNCRFHVJL

@llinfeng
Copy link
Author

it's to launch a new window in VD 2, and set focus at VD 3

To make the variable names self-explanatory, how about window_desktopNum -> new_window_desktopNum and current_desktopNum? (I assume the intention is to keep the focus "local" while sending windows around. Then, it helps to use a wildcard symbol/number(0) to indicate the current desktop.)


Here is the full script that opened four OneNote windows in the current desktop. These UWP apps are hard to run direclty through Run command. For now, the only method I know is to first go to shell:AppsFolder using the Windows Run window, create a desktop shortcut for OenNote, and then, feed the link's path to the Run command.

Folded script
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance force
ListLines Off
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
SetBatchLines -1
#KeyHistory 0

#Include ..\VD.ahk

VD.startShellMessage()
VD.Run("""c:\users\llinfeng\Desktop\OneNote for Windows 10 - Shortcut.lnk""","","","","",1)
VD.Run("""c:\users\llinfeng\Desktop\OneNote for Windows 10 - Shortcut.lnk""","","","","",2)
VD.Run("""c:\users\llinfeng\Desktop\OneNote for Windows 10 - Shortcut.lnk""","","","","",3)
VD.Run("""c:\users\llinfeng\Desktop\OneNote for Windows 10 - Shortcut.lnk""","","","","",4)
return

f3::Exitapp

And, yes, the OneNote app I use most often is the free one from the Microsoft Store as you linked. It does the best job of syncing, where the thing from Office subscription would run into sync issues very frequently for me. Here is another script where I was trying to see if I can dispatch 4 onenote.exe windows to four virtual desktops. Upon running the script, only one onenote.exe instance is opened.

Folded script
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance force
ListLines Off
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
SetBatchLines -1
#KeyHistory 0

#Include ..\VD.ahk

VD.startShellMessage()
VD.Run("""C:\Program Files\Microsoft Office\root\Office16\ONENOTE.EXE""","","","","",1)
VD.Run("""C:\Program Files\Microsoft Office\root\Office16\ONENOTE.EXE""","","","","",2)
VD.Run("""C:\Program Files\Microsoft Office\root\Office16\ONENOTE.EXE""","","","","",3)
VD.Run("""C:\Program Files\Microsoft Office\root\Office16\ONENOTE.EXE""","","","","",4)


return

f3::Exitapp

Per its current state, VD.Run() is doing a great job. I assume it should work for normal windowed applications that are launched by running an executable file. Though, why both versions of OneNote are behaving as such, I don't have a good understanding yet. They do have a few things in common - it is hard to launch, and would fail to sync sometimes. Though, on the good part - they both offer an infinite canvas for scribbling with a digitizer (pen), and they are both helpful for annotating screenshots for snippets of code for ideation purposes.

@FuPeiJiang
Copy link
Owner

VD.startShellMessage()
VD.goToDesktopNum(1)
;assume no OneNote window exists
;first window needs 5 seconds to ready
Run % "shell:AppsFolder\Microsoft.Office.OneNote_8wekyb3d8bbwe!microsoft.onenoteim"
Sleep 5000 ;subsequent runs will be ignored without waiting 5000ms
VD.Run("shell:AppsFolder\Microsoft.Office.OneNote_8wekyb3d8bbwe!microsoft.onenoteim","","OneNote for Windows 10","ApplicationFrameWindow","ApplicationFrameHost.exe",2)
VD.Run("shell:AppsFolder\Microsoft.Office.OneNote_8wekyb3d8bbwe!microsoft.onenoteim","","OneNote for Windows 10","ApplicationFrameWindow","ApplicationFrameHost.exe",3)

this is the best I could do..

remember to update branch https://github.com/FuPeiJiang/VD.ahk/tree/run_in_VD before running

@llinfeng
Copy link
Author

Fantastic. The example you give works great. It is very thoughtful that you switch to VD1 and launch the first OneNote window there.

Question: what does VD.startShellMessage() do? Without it, it looks like all OneNote windows are opened in the active virtual desktop.

@llinfeng
Copy link
Author

@FuPeiJiang Will you merge these back to the main branch? I assume it is class_VD?

It is a great pleasure to find this repo, and I'm planning to stay up-to-date as much as I can :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants