Old 11-06-2020, 07:45 AM   #1
ovnis
Human being with feelings
 
ovnis's Avatar
 
Join Date: Oct 2011
Posts: 2,019
Default Script to focus main or midi window under mouse. Need help.

Hello,

There is an issue with this script: after having focused on a plug-in, it can't work any more. To enable it again, we have to click on the main or midi window. A solution, please?


function Main()
local win = {}
local _, list = reaper.JS_MIDIEditor_ListAll()
for adr in list:gmatch("[^,]+") do
win[#win+1] = reaper.JS_Window_HandleFromAddress(adr)
end
win[#win+1] = reaper.GetMainHwnd()
local x, y = reaper.GetMousePosition()
local child = reaper.JS_Window_FromPoint(x, y)
for i = 1, #win do
local parent = child
while parent ~= nil do
if parent == win[i] then
reaper.JS_Window_SetFocus(parent)
return
end
parent = reaper.JS_Window_GetParent(parent)
end
end
end

Main()
reaper.defer(function () end)
ovnis is online now   Reply With Quote
Old 11-06-2020, 08:55 AM   #2
amagalma
Human being with feelings
 
amagalma's Avatar
 
Join Date: Apr 2011
Posts: 2,877
Default

What are you trying to do? To focus the MIDI Editor window under the mouse?
__________________
Most of my scripts can be found in ReaPack.
If you find them useful, a donation would be greatly appreciated! Thank you! :)
amagalma is offline   Reply With Quote
Old 11-06-2020, 09:01 AM   #3
ovnis
Human being with feelings
 
ovnis's Avatar
 
Join Date: Oct 2011
Posts: 2,019
Default

https://forum.cockos.com/showthread.php?t=244528
ovnis is online now   Reply With Quote
Old 11-06-2020, 09:26 AM   #4
amagalma
Human being with feelings
 
amagalma's Avatar
 
Join Date: Apr 2011
Posts: 2,877
Default

Did it very fast but should work:

corrected code in post #9
Code:
local win = {}
win[reaper.GetMainHwnd()] = true
local start = reaper.time_precise()

function Main()
  local time = reaper.time_precise()
  if time >= start + 0.2 then
    start = time
    local _, list = reaper.JS_MIDIEditor_ListAll()
    for adr in list:gmatch("[^,]+") do
      local hwnd = reaper.JS_Window_HandleFromAddress(adr)
      if not win[hwnd] then
        win[hwnd] = true
      end
    end
    local x, y = reaper.GetMousePosition()
    local child = reaper.JS_Window_FromPoint(x, y)
    if win[child] then
      reaper.JS_Window_SetFocus(child)
    else
      local parent = reaper.JS_Window_GetParent(parent)
      local tries = 0
      while parent ~= nil do
        tries = tries + 1
        if tries == 5 then break end
        if win[parent] then
          reaper.JS_Window_SetFocus(parent)
          break
        else
          parent = reaper.JS_Window_GetParent(parent)
        end
      end
    end
  end
  reaper.defer(Main)
end
__________________
Most of my scripts can be found in ReaPack.
If you find them useful, a donation would be greatly appreciated! Thank you! :)

Last edited by amagalma; 11-06-2020 at 12:05 PM.
amagalma is offline   Reply With Quote
Old 11-06-2020, 09:33 AM   #5
ovnis
Human being with feelings
 
ovnis's Avatar
 
Join Date: Oct 2011
Posts: 2,019
Default

Thx you. Sadly, it doesn't work. I would like to be able to focus the window which is under the mouse cursor (FX track window, main window, midi window). So I have linked your script to L, but when i press L, it doesn't focus on the good window.
ovnis is online now   Reply With Quote
Old 11-06-2020, 10:11 AM   #6
amagalma
Human being with feelings
 
amagalma's Avatar
 
Join Date: Apr 2011
Posts: 2,877
Default

I thought you were trying to automatically focus the window under the mouse, if it was the main or a midi editor. That is what the script does, no need to press anything.

If you need to press a key to focus a window, why not just click it?
__________________
Most of my scripts can be found in ReaPack.
If you find them useful, a donation would be greatly appreciated! Thank you! :)
amagalma is offline   Reply With Quote
Old 11-06-2020, 10:45 AM   #7
ovnis
Human being with feelings
 
ovnis's Avatar
 
Join Date: Oct 2011
Posts: 2,019
Default

Quote:
I thought you were trying to automatically focus the window under the mouse, if it was the main or a midi editor.
Yes. But I want a script which i will execute with an action. I don't want to have to activate any script at the start of Reaper.

Quote:
That is what the script does, no need to press anything.
Ok, I have installed your script inside the main action. I have started your script, but it doesn't automatically focus. When my mouse in under the main or the midi editor, it happens nothing. My midi editor and FX track are docked.

To be clear, what I want:

I have setted "grid : set to 1" to G, inside main action and midi action. So, if the main window is focused and if I press G, it will change the main grid. If the midi editor is focused and if I press G, it will change the midi editor grid. So, I would like to make a custom action like this :

- Script to focus main or midi window under mouse
- grid : set to 1

So I have not to click on the good window to execute the good action.

Last edited by ovnis; 11-06-2020 at 11:00 AM.
ovnis is online now   Reply With Quote
Old 11-06-2020, 11:20 AM   #8
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: Apr 2020
Location: Leipzig
Posts: 2,231
Default

The script will not work in the midi editor unless you add it as action to the midi-editor-section(and add the shortcut there) as well, when you want to run it via shortcut.

If I understood it correctly.
__________________
Use you/she/her, when contacting me, please. Thanks :) Not mentoring via PMs, sorry.
Ultraschall-API - 1385 ReaScript functions for Reaper - Reaper Internals - Developerdocs4Reaper - Donate, if you wish
Meo-Ada Mespotine is offline   Reply With Quote
Old 11-06-2020, 12:03 PM   #9
amagalma
Human being with feelings
 
amagalma's Avatar
 
Join Date: Apr 2011
Posts: 2,877
Default

Try this (edited):
Code:
local win = {}
local main_hwnd = reaper.GetMainHwnd()
local start = reaper.time_precise()
local frequency = 1/7

function Main()
  local time = reaper.time_precise()
  local focused_win = reaper.JS_Window_GetFocus()
  if time >= start + frequency then
    start = time
    local _, list = reaper.JS_MIDIEditor_ListAll()
    for adr in list:gmatch("[^,]+") do
      local hwnd = reaper.JS_Window_HandleFromAddress(adr)
      if not win[hwnd] then
        win[hwnd] = true
      end
    end
    
    local x, y = reaper.GetMousePosition()
    local window = reaper.JS_Window_FromPoint(x, y)
    if (win[window] or window == main_hwnd) and window ~= focused_win then
      reaper.JS_Window_SetFocus(window)
    else
      local parent = reaper.JS_Window_GetParent( window )
      if reaper.JS_Window_GetClassName( parent ) == "REAPERmidieditorwnd" and
         parent ~= focused_win
      then
        reaper.JS_Window_SetFocus(window)
      elseif (parent == main_hwnd or win[parent]) and parent ~= focused_win then
        reaper.JS_Window_SetFocus(parent)
      end
    end
  end
  reaper.defer(Main)
end

Main()

This automatically focuses main or midi editor under mouse. It does not focus automatically any other window.
__________________
Most of my scripts can be found in ReaPack.
If you find them useful, a donation would be greatly appreciated! Thank you! :)

Last edited by amagalma; 11-06-2020 at 01:26 PM.
amagalma is offline   Reply With Quote
Old 11-06-2020, 12:30 PM   #10
ovnis
Human being with feelings
 
ovnis's Avatar
 
Join Date: Oct 2011
Posts: 2,019
Default

From main focused to midi > OK
From main focused to midi docked > issue
From midi focused (docked or not) to main > issue
From FX window focused (docked or not) to main or midi docked > issue (it will always focus on the midi window no docked)

Last edited by ovnis; 11-06-2020 at 12:37 PM.
ovnis is online now   Reply With Quote
Old 11-06-2020, 12:58 PM   #11
amagalma
Human being with feelings
 
amagalma's Avatar
 
Join Date: Apr 2011
Posts: 2,877
Default

Yes.. I think maybe it will be just easier to make a script with all the windows that can be focused (either specifying them by name, or class) and check if the window at point is one of them.

Edit. Try the code above.. still doesn't work when ME is docked but works in other occasions..
__________________
Most of my scripts can be found in ReaPack.
If you find them useful, a donation would be greatly appreciated! Thank you! :)

Last edited by amagalma; 11-06-2020 at 01:17 PM.
amagalma is offline   Reply With Quote
Old 11-06-2020, 01:22 PM   #12
ovnis
Human being with feelings
 
ovnis's Avatar
 
Join Date: Oct 2011
Posts: 2,019
Default

Quote:
Originally Posted by amagalma View Post
still doesn't work when ME is docked but works in other occasions..
Nop.

From midi focused (docked or not) to main > issue
From FX window focused (docked or not) to main or midi docked > issue (it will always focus on the midi window no docked)
ovnis is online now   Reply With Quote
Old 11-06-2020, 01:26 PM   #13
amagalma
Human being with feelings
 
amagalma's Avatar
 
Join Date: Apr 2011
Posts: 2,877
Default

Try now. Changed code
__________________
Most of my scripts can be found in ReaPack.
If you find them useful, a donation would be greatly appreciated! Thank you! :)
amagalma is offline   Reply With Quote
Old 11-06-2020, 01:40 PM   #14
ovnis
Human being with feelings
 
ovnis's Avatar
 
Join Date: Oct 2011
Posts: 2,019
Default

It's impossible to focus the main window (midi editor is always focused). And to work with docked editor midi is very important for me.

Last edited by ovnis; 11-06-2020 at 01:46 PM.
ovnis is online now   Reply With Quote
Old 11-06-2020, 01:49 PM   #15
amagalma
Human being with feelings
 
amagalma's Avatar
 
Join Date: Apr 2011
Posts: 2,877
Default

Last version for today:

Code:
local win = {}
local main_hwnd = reaper.GetMainHwnd()
local start = reaper.time_precise()
local frequency = 1/7
local classes = {
      REAPERTCPDisplay = true,
      REAPERTrackListWindow = true,
      REAPERVirtWndDlgHost = true,
      REAPERTimeDisplay = true,
      REAPERmidieditorwnd = true
}


function Main()
  local time = reaper.time_precise()
  local focused_win = reaper.JS_Window_GetFocus()
  if time >= start + frequency then
    start = time
    local _, list = reaper.JS_MIDIEditor_ListAll()
    for adr in list:gmatch("[^,]+") do
      local hwnd = reaper.JS_Window_HandleFromAddress(adr)
      if not win[hwnd] then
        win[hwnd] = true
      end
    end
    
    local x, y = reaper.GetMousePosition()
    local window = reaper.JS_Window_FromPoint(x, y)
    if (win[window] or window == main_hwnd) and window ~= focused_win then
      reaper.JS_Window_SetFocus(window)
      CASE = 1
    else
      local parent = reaper.JS_Window_GetParent( window )
      if classes[reaper.JS_Window_GetClassName( parent )] or
         classes[reaper.JS_Window_GetClassName( window )] and
         parent ~= focused_win
      then
        reaper.JS_Window_SetFocus(window)
        CASE = 2
      elseif ( win[parent]) and parent ~= focused_win then
        reaper.JS_Window_SetFocus(parent)
        CASE = 3
      end
    end
  end
  reaper.defer(Main)
end

Main()
__________________
Most of my scripts can be found in ReaPack.
If you find them useful, a donation would be greatly appreciated! Thank you! :)
amagalma is offline   Reply With Quote
Old 11-06-2020, 02:15 PM   #16
ovnis
Human being with feelings
 
ovnis's Avatar
 
Join Date: Oct 2011
Posts: 2,019
Default

Great! It works. Thx you very much!

Last edited by ovnis; 11-06-2020 at 02:42 PM.
ovnis is online now   Reply With Quote
Old 11-06-2020, 02:46 PM   #17
ovnis
Human being with feelings
 
ovnis's Avatar
 
Join Date: Oct 2011
Posts: 2,019
Default

There is one issue: inside the main window, after having opened, routing window and envelope window disappear. Maybe because, the main window is always focused?

To solve this issue, it could be possible to have a script which focused the window only once?

custom action:
-the script
-the action

Or maybe a script which stops to focus, once it knows that the good window is already focused ?

Last edited by ovnis; 11-06-2020 at 03:21 PM.
ovnis is online now   Reply With Quote
Old 11-07-2020, 01:01 AM   #18
amagalma
Human being with feelings
 
amagalma's Avatar
 
Join Date: Apr 2011
Posts: 2,877
Default

Quote:
Originally Posted by ovnis View Post
To solve this issue, it could be possible to have a script which focused the window only once?

Code:
local window = reaper.JS_Window_FromPoint(reaper.GetMousePosition())
if window then
  reaper.JS_Window_SetFocus(window)
end
reaper.defer(function() end)
__________________
Most of my scripts can be found in ReaPack.
If you find them useful, a donation would be greatly appreciated! Thank you! :)
amagalma is offline   Reply With Quote
Old 11-07-2020, 03:36 AM   #19
ovnis
Human being with feelings
 
ovnis's Avatar
 
Join Date: Oct 2011
Posts: 2,019
Default

Hello, thanks for your time!

This last script doesn't work from FX window to midi window. The main window is always focused.

And this solution with your long script?

Quote:
Or maybe a script which stops to focus, once it knows that the good window is already focused?
Or an another idea?

Last edited by ovnis; 11-07-2020 at 05:07 AM.
ovnis is online now   Reply With Quote
Old 11-07-2020, 10:09 AM   #20
amagalma
Human being with feelings
 
amagalma's Avatar
 
Join Date: Apr 2011
Posts: 2,877
Default

If all this is in order to save you from clicking once with the mouse, have you considered using this action in your custom actions?:
SWS/S&M: Left mouse click at cursor position (use w/o modifier)
__________________
Most of my scripts can be found in ReaPack.
If you find them useful, a donation would be greatly appreciated! Thank you! :)
amagalma is offline   Reply With Quote
Old 11-07-2020, 11:32 AM   #21
ovnis
Human being with feelings
 
ovnis's Avatar
 
Join Date: Oct 2011
Posts: 2,019
Default

Hi, I've already test this but it's not good because it can unintentionally select notes, track, item or create note if we click fast. I've tested with the middle mouse button with ICUE, too. But, that works for the main window but not for the midi editor. I have tested with the right click then delete (to close the contextual menu), but sometimes, there are glitchs.

Your last long script is almost perfect! You can't improve it?

The issue with your long script: inside the main window, after having opened, routing window and envelope window disappear if the mouse cursor is outside the two windows.

When the main window is focused, routing window and envelope window disappear.

If the mouse cursor stays on routing window or envelope window : OK
It the mouse cursor is outside the two windows : issue. Window vanishes.

There should be 2 scripts? One which is started at the start of Reaper and scans continuously the position of the mouse over windows and a second script which is launched when we want to focus a window (a custom action with this second script + the action)?



Last edited by ovnis; 11-07-2020 at 11:52 AM.
ovnis is online now   Reply With Quote
Old 11-08-2020, 01:31 AM   #22
amagalma
Human being with feelings
 
amagalma's Avatar
 
Join Date: Apr 2011
Posts: 2,877
Default

Try this:
Code:
local win = {}
local main_hwnd = reaper.GetMainHwnd()
local start = reaper.time_precise()
local frequency = 1/7
local classes = {
      REAPERTCPDisplay = true,
      REAPERTrackListWindow = true,
      REAPERVirtWndDlgHost = true,
      REAPERTimeDisplay = true,
      REAPERmidieditorwnd = true
}


function Main()
  local time = reaper.time_precise()
  local focused_win = reaper.JS_Window_GetFocus()
  local title = reaper.JS_Window_GetTitle( focused_win )
  local title_parent = reaper.JS_Window_GetTitle( reaper.JS_Window_GetParent( focused_win ) )
  if title:find"Envelopes for " or title_parent:find"Envelopes for "
  or title:find"Routing for track " or title_parent:find"Routing for track "
  or title:find"Outputs for Master Track" or title_parent:find"Outputs for Master Track"
  or reaper.JS_Window_GetClassName( focused_win ) == "Edit"
  then
    start = time
  end
  if time >= start + frequency then
    start = time
    local _, list = reaper.JS_MIDIEditor_ListAll()
    for adr in list:gmatch("[^,]+") do
      local hwnd = reaper.JS_Window_HandleFromAddress(adr)
      if not win[hwnd] then
        win[hwnd] = true
      end
    end
    
    local x, y = reaper.GetMousePosition()
    local window = reaper.JS_Window_FromPoint(x, y)
    if (win[window] or window == main_hwnd) and window ~= focused_win then
      reaper.JS_Window_SetFocus(window)
    else
      local parent = reaper.JS_Window_GetParent( window )
      if classes[reaper.JS_Window_GetClassName( parent )] or
         classes[reaper.JS_Window_GetClassName( window )] and
         parent ~= focused_win
      then
        reaper.JS_Window_SetFocus(window)
      elseif ( win[parent]) and parent ~= focused_win then
        reaper.JS_Window_SetFocus(parent)
      end
    end
  end
  reaper.defer(Main)
end

Main()
__________________
Most of my scripts can be found in ReaPack.
If you find them useful, a donation would be greatly appreciated! Thank you! :)

Last edited by amagalma; 11-08-2020 at 04:18 AM.
amagalma is offline   Reply With Quote
Old 11-08-2020, 03:36 AM   #23
Skorobagatko
Human being with feelings
 
Skorobagatko's Avatar
 
Join Date: Mar 2017
Location: Ukraine, Kyiv
Posts: 373
Default

Quote:
Originally Posted by amagalma View Post
Try this:
Code:
function Main()
  local time = reaper.time_precise()
  local focused_win = reaper.JS_Window_GetFocus()
  local title = reaper.JS_Window_GetTitle( focused_win )
  local title_parent = reaper.JS_Window_GetTitle( reaper.JS_Window_GetParent( focused_win ) )
  if title:find"Envelopes for " or title_parent:find"Envelopes for "
  or title:find"Routing for track " or title_parent:find"Routing for track "
  or title:find"Outputs for Master Track" or title_parent:find"Outputs for Master Track"
  or reaper.JS_Window_GetClassName( focused_win ) == "Edit"
  then
    start = time
  end
  if time >= start + frequency then
    start = time
    local _, list = reaper.JS_MIDIEditor_ListAll()
    for adr in list:gmatch("[^,]+") do
      local hwnd = reaper.JS_Window_HandleFromAddress(adr)
      if not win[hwnd] then
        win[hwnd] = true
      end
    end
    local x, y = reaper.GetMousePosition()
    local window = reaper.JS_Window_FromPoint(x, y)
    if (win[window] or window == main_hwnd) and window ~= focused_win then
      reaper.JS_Window_SetFocus(window)
    else
      local parent = reaper.JS_Window_GetParent( window )
      if classes[reaper.JS_Window_GetClassName( parent )] or
         classes[reaper.JS_Window_GetClassName( window )] and
         parent ~= focused_win
      then
        reaper.JS_Window_SetFocus(window)
      elseif ( win[parent]) and parent ~= focused_win then
        reaper.JS_Window_SetFocus(parent)
      end
    end
  end
  reaper.defer(Main)
end

Main()
__________________
Arrangement/mixing
Skorobagatko is offline   Reply With Quote
Old 11-08-2020, 04:19 AM   #24
amagalma
Human being with feelings
 
amagalma's Avatar
 
Join Date: Apr 2011
Posts: 2,877
Default

Oops.. sorry! I didn't copy paste the whole of it!

Now the whole thing is up there
__________________
Most of my scripts can be found in ReaPack.
If you find them useful, a donation would be greatly appreciated! Thank you! :)
amagalma is offline   Reply With Quote
Old 11-08-2020, 05:17 AM   #25
ovnis
Human being with feelings
 
ovnis's Avatar
 
Join Date: Oct 2011
Posts: 2,019
Default

Hi, it's almost perfect. But

routing window or envelope window to main or ME: issue. Theses windows are not focused when we want to use an action.

Last edited by ovnis; 11-08-2020 at 05:29 AM.
ovnis is online now   Reply With Quote
Old 11-08-2020, 05:29 AM   #26
ovnis
Human being with feelings
 
ovnis's Avatar
 
Join Date: Oct 2011
Posts: 2,019
Default

Maybe 2 scripts will be a good idea?

A continue script which will scan continuously the position of the mouse over windows and one script (it will focus the good window with the help of the first script) which will be activate when we want to use an action inside a custom action (the script + the action).
ovnis is online now   Reply With Quote
Old 11-08-2020, 06:46 AM   #27
Skorobagatko
Human being with feelings
 
Skorobagatko's Avatar
 
Join Date: Mar 2017
Location: Ukraine, Kyiv
Posts: 373
Default

Quote:
Originally Posted by ovnis View Post
Hi, it's almost perfect. But

routing window or envelope window to main or ME: issue. Theses windows are not focused when we want to use an action.
Can you please provide an example more specificly about the actions you try to use?
__________________
Arrangement/mixing
Skorobagatko is offline   Reply With Quote
Old 11-08-2020, 08:22 AM   #28
ovnis
Human being with feelings
 
ovnis's Avatar
 
Join Date: Oct 2011
Posts: 2,019
Default

Set the grid, for exemple.
ovnis is online now   Reply With Quote
Old 12-19-2020, 03:17 PM   #29
ovnis
Human being with feelings
 
ovnis's Avatar
 
Join Date: Oct 2011
Posts: 2,019
Default

Hello amagalma!
I have found an issue with the scrip (pan window disapears when the mouse cursor is outside):

ovnis is online now   Reply With Quote
Old 01-08-2021, 08:26 PM   #30
ovnis
Human being with feelings
 
ovnis's Avatar
 
Join Date: Oct 2011
Posts: 2,019
Default

Hello, there is an issue with the snap/grid settings window, too:

ovnis is online now   Reply With Quote
Old 01-09-2021, 02:01 AM   #31
Skorobagatko
Human being with feelings
 
Skorobagatko's Avatar
 
Join Date: Mar 2017
Location: Ukraine, Kyiv
Posts: 373
Default

Quote:
Originally Posted by ovnis View Post
Hello, there is an issue with the snap/grid settings window, too:

Try this action: Script: Archie_Pref; Toggle Allow snap grid,track envelope,routing windows to stay open.lua
__________________
Arrangement/mixing
Skorobagatko is offline   Reply With Quote
Old 03-02-2021, 11:01 AM   #32
Skorobagatko
Human being with feelings
 
Skorobagatko's Avatar
 
Join Date: Mar 2017
Location: Ukraine, Kyiv
Posts: 373
Default

Hi amagalma! Could you please look at the issues which ovnis described?
__________________
Arrangement/mixing
Skorobagatko is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 09:24 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, vBulletin Solutions Inc.