 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
ezuk
Joined: 04 Jun 2005 Posts: 129
|
Posted: Sat Jun 04, 2005 1:01 am Post subject: Trying to build a util to move windows between screens |
|
|
Hi,
I'm trying to code a very simple util. I have a 3-screen system, and I want to move a window (the active window) from one monitor to another, using a hotkey.
The algorithm goes something like this:
1) Determine size of current window
2) Determine size [resolution] of the current monitor for this window
3) Calculate percentage in height/width current window is taking up on current monitor
3) Determine size of destination monitor
4) Resize current window so that it would take up the same screen percentage on destination monitor.
5) Move current window to destination monitor.
This is complicated by:
1) I have a three-screen system. Not just two.
2) I run using two different resolutions -- 1024x768 for my secondaries, and 1280x1024 for my primary.
3) My primary (1) is in the middle. So physically, the order on my desk is monitor 2 --> Monitor 1 --> Monitor 3
4) I'm trying to build a script which other people could also use, as such a util is sorely lacking.
I'm currently stuck on step (2), where it says "current monitor". I'm trying to figure out the current monitor for a given window, and it's turning out to be quite a trick. I'm doing something wrong, for sure, just not sure what.
Below is my script. I would very _much_ appreciate any and all help you may give me with this.
| Code: |
SysGet, MonitorCount, MonitorCount
Loop, %MonitorCount%
{
SysGet, Mon%A_Index%, MonitorWorkArea, %A_Index%
}
; So I have: Mon1Right,Left,Top,Bottom. And Mon2 and Mon3 the same.
;Resize as needed and move to monitor 2:
;If (CurWinWidth > Mon2Right-Mon2Left)
; CurWinWidth:=(Mon2Right-Mon2Left)
;If (CurWinHeight > Mon2Bottom-Mon2Top)
; CurWinHeight:=(Mon2Bottom-Mon2Top)
;WinMove, PSPad,,(CurWinX + Mon2Left), (CurWinY+Mon2Top)
Loop, %MonitorCount%
{
WinGetPos, CurWinX, CurWinY, CurWinWidth, CurWinHeight, PSPad
MsgBox % "Monitor "%A_Index% " Left is " Mon%A_Index%Left " CurWinX is " CurWinX " Monitor Right is " Mon%A_Index%Right
If CurWinX between %Mon%A_Index%Left% and %Mon%A_Index%Right%
CurWinMon=%A_Index%
MsgBox %CurWinMon%
}
|
|
|
| Back to top |
|
 |
ezuk
Joined: 04 Jun 2005 Posts: 129
|
Posted: Sat Jun 04, 2005 12:22 pm Post subject: Anyone? |
|
|
For some reason this is getting many fewer views than other threads. Did I choose an uninteresting name?  |
|
| Back to top |
|
 |
TeknoMusicMan
Joined: 14 Apr 2005 Posts: 188 Location: Wisconsin, USA
|
Posted: Sat Jun 04, 2005 9:30 pm Post subject: |
|
|
no it's probably just because most people arn't using multi monitor setups. I on the other hand would be interested in such a tool for when I turn on multi monitor on my laptop for outputing stuff to my tv. _________________
"Make it idiot-proof, and someone will make a better idiot." |
|
| Back to top |
|
 |
corrupt
Joined: 29 Dec 2004 Posts: 2436
|
Posted: Sat Jun 04, 2005 10:07 pm Post subject: Re: Anyone? |
|
|
| ezuk wrote: | For some reason this is getting many fewer views than other threads. Did I choose an uninteresting name?  | I'm interested also but I'm currently only using a single monitor. I may be able to help test/contribute a bit later with a 3 monitor setup though... |
|
| Back to top |
|
 |
jonny
Joined: 13 Nov 2004 Posts: 3004 Location: Minnesota
|
Posted: Sun Jun 05, 2005 12:10 am Post subject: |
|
|
| It's not uncommon to get no replies in the first twelve hours, but this forum is getting more populated lately. Anyway, I like the idea, but my two-monitor setup is on my Linux box. (My Windows one is a decrepit old laptop with a broken screen and upgrades up the yin-yang) |
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10480
|
Posted: Sun Jun 05, 2005 12:29 am Post subject: |
|
|
I think one problem with the script is this line:
If CurWinX between %Mon%A_Index%Left% and %Mon%A_Index%Right%
The "between" keyword is an old-style operator that cannot resolve arrays on its right side. Try using the following:
if (CurWinX >= Mon%A_Index%Left AND CurWinX <= Mon%A_Index%Right) |
|
| Back to top |
|
 |
ezuk
Joined: 04 Jun 2005 Posts: 129
|
Posted: Fri Jun 10, 2005 1:13 am Post subject: Some progress, and stuck again |
|
|
Hi,
OK... This is what I have so far:
| Code: |
SysGet, MonitorCount, MonitorCount
Loop, %MonitorCount%
{
SysGet, Mon%A_Index%, MonitorWorkArea, %A_Index%
}
; So I have: Mon1Right,Left,Top,Bottom. And Mon2 and Mon3 the same.
WinGetActiveTitle,CurWinTitle
WinGetPos, CurWinX, CurWinY, CurWinWidth, CurWinHeight, %CurWinTitle%
MoveToMon(2)
MoveToMon(num)
{
msgbox moving to window %num%, right is mon%num%right
If (CurWinWidth > Mon%num%Right-Mon%num%Left)
CurWinWidth:=(Mon%num%Right-Mon%num%Left)
If (CurWinHeight > Mon%num%Bottom-Mon%num%Top)
CurWinHeight:=(Mon2%num%Bottom-Mon%num%Top)
WinMove, %CurWinTitle%,,(CurWinX + Mon%num%Left), (CurWinY+Mon%num%Top)
}
Loop, %MonitorCount%
{
MsgBox % "Monitor " A_Index " Left is " Mon%A_Index%Left " CurWinX is " CurWinX " Monitor Right is " Mon%A_Index%Right
if (CurWinX >= Mon%A_Index%Left AND CurWinX <= Mon%A_Index%Right)
CurWinMon=%a_index%
MsgBox Current monitor is %CurWinMon%
}
|
However, I get stuck on "MoveToMon". I can't get the var name nesting to work. I get the strong "Mon2Right", which is correct, but it is actually the _Variable name_ that I want -- I want Autohotkey to treat it as a var name, not just as a string (as if it were surrounded by % marks).
How do I do this? Do you have any other suggestions?
The good news is that the active window now knows which screen it is on, which is pretty cool! [/code] |
|
| Back to top |
|
 |
corrupt
Joined: 29 Dec 2004 Posts: 2436
|
Posted: Fri Jun 10, 2005 6:46 am Post subject: |
|
|
| One problem might be that the MoveToMon function probably needs to be outside of the subroutine. Maybe try placing the MoveToMon function at the end of the code. The MoveToMon function should also have a Return before the last } |
|
| Back to top |
|
 |
ezuk
Joined: 04 Jun 2005 Posts: 129
|
Posted: Fri Jun 10, 2005 9:52 am Post subject: |
|
|
| corrupt wrote: | | One problem might be that the MoveToMon function probably needs to be outside of the subroutine. Maybe try placing the MoveToMon function at the end of the code. The MoveToMon function should also have a Return before the last } |
I am going to try this soon, but this is exactly the type of issue I thought it would be. I guessed it would be some kind of intricate syntax issue which I wouldn't find anywhere in the docs.
I'm a technical writer by trade, and these things get me thinking. AutoHotkey is _so_ powerful -- but are the docs we have really adequate to educate newbies (such as myself) about its power?
I think there's a whole field of arcana and 'secret' knowledge about AutoIt syntax rules, function exceptions and other eccentricities. This should be written up somewhere -- maybe I'll even do it myself some day.
What do you think? Am I right about this?
(I still need help with the above script, though, if anyone has any other good suggestions or even cares to donate some code for the cause.) |
|
| Back to top |
|
 |
toralf
Joined: 31 Jan 2005 Posts: 3841 Location: Bremen, Germany
|
Posted: Fri Jun 10, 2005 12:09 pm Post subject: |
|
|
Hi,
I have three monitors, but not connected (UNIX,PC,Notebook). So I have a problem to understand this, not even test it. Could you please run the following and copy the text from the messag box into a post. Thanks | Code: | SysGet, MonitorCount, MonitorCount
Loop, %MonitorCount%
SysGet, Mon%A_Index%, MonitorWorkArea, %A_Index%
WinGetActiveTitle,CurWinTitle
WinGetPos, CurWinX, CurWinY, CurWinWidth, CurWinHeight, %CurWinTitle%
Text =
Loop, %MonitorCount%
{
Text = %Text%Monitor %A_Index%:`n
var := Mon%A_Index%Left
Text = %Text%Left: %var%`n
var := Mon%A_Index%Right
Text = %Text%Right: %var%`n
var := Mon%A_Index%Top
Text = %Text%Top: %var%`n
var := Mon%A_Index%Bottom
Text = %Text%Bottom: %var%`n`n
if (CurWinX >= Mon%A_Index%Left AND CurWinX <= Mon%A_Index%Right)
CurWinMon=%a_index%
}
Text = %Text%`nWindow:`n
Text = %Text%Title: %CurWinTitle%`n
Text = %Text%X: %CurWinX%`n
Text = %Text%Y: %CurWinY%`n
Text = %Text%W: %CurWinWidth%`n
Text = %Text%H: %CurWinHeight%
MsgBox Current monitor is %CurWinMon%`n`n%Text% |
Am I right, that the starting cooridnate of the second left most monitor is the width of the first? So that total screen resolution is the sum of all three monitors? _________________ Ciao
toralf 
Last edited by toralf on Fri Jun 10, 2005 1:05 pm; edited 1 time in total |
|
| Back to top |
|
 |
toralf
Joined: 31 Jan 2005 Posts: 3841 Location: Bremen, Germany
|
Posted: Fri Jun 10, 2005 12:17 pm Post subject: |
|
|
If my previous assumtions are correct, you could try this: | Code: | | code has been removed. A better version follows on next page |
_________________ Ciao
toralf 
Last edited by toralf on Fri Jun 10, 2005 2:48 pm; edited 6 times in total |
|
| Back to top |
|
 |
ezuk
Joined: 04 Jun 2005 Posts: 129
|
Posted: Fri Jun 10, 2005 12:58 pm Post subject: |
|
|
| toralf wrote: | Hi,
I have three monitors, but not connected (UNIX,PC,Notebook). So I have a problem to understand this, not even test it. Could you please run the following and copy the text from the messag box into a post.
Am I right, that the starting cooridnate of the second left most monitor is the width of the first? So that total screen resolution is the sum of all three monitors? |
First of all, thank you for taking the time to code something!
Secondly, I think your code didn't do what you mean it to do. It sort of worked, but the text I got was this (ran it from Total Commander, Total Commander was on monitor #1):
"Current monitor is 1
Monitor 3:
Left: 1280
Right: 2304
Top: 32
Bottom: 768
Window:
Title: Total Commander 6.51
X: 240
Y: 244
W: 800
H: 600
So... I think everything went fine except the part where it says "Monitor 3", I think you meant it to be for all monitors. Right?
1) I would have fixed it, but I don't really understand what you did with the %TEXT% variable. Is this documented somewhere? It looks like you set the variable to a different value every time, so I don't understand how the messagebox ended up containing all the text together.
2) Please note that my system is built with monitors like this:
Mon 2 --- Mon 1 --- Mon 3
I mean, Mon 1 (primary) is in the middle. So Mon 2 actually has negative coordinates. Just for your info.
And thank you again!!  |
|
| Back to top |
|
 |
ezuk
Joined: 04 Jun 2005 Posts: 129
|
Posted: Fri Jun 10, 2005 1:06 pm Post subject: |
|
|
| toralf wrote: | If my previous assumtions are correct, you could try
| Code: |
[......]
If (CurWinWidth > Mon%num%Right-Mon%num%Left)
CurWinWidth:=(Mon%num%Right-Mon%num%Left)
If (CurWinHeight > Mon%num%Bottom-Mon%num%Top)
CurWinHeight:=(Mon2%num%Bottom-Mon%num%Top)
|
|
I got an error on | Code: | | If (CurWinWidth > Mon%num%Right-Mon%num%Left) | (line 21 in your script).
I know you copied this from my script, and I didn't get the error, but this was probably becase my code was so bad it didn't even try to run this line.
The error is:
| Quote: |
Error: This variable or function name contains an illegal character. The current thread will exit.
Specifically: MonRight - Mon
|
So... It actually parses the line wrong. Instead of thinking the var is Mon1Left, it thinks the var is %MonRight-Mon%. It doesn't read the percent signs correctly. I mean... Not correctly for us, anyway.
I tried putting it in parens, and brackets, and all sorts of things (like ((Mon%num%right) - (Mon%num%lefT)) but it kept giving me the same error.
Any ideas? (Once again, this is a syntax thing.) |
|
| Back to top |
|
 |
toralf
Joined: 31 Jan 2005 Posts: 3841 Location: Bremen, Germany
|
Posted: Fri Jun 10, 2005 1:11 pm Post subject: |
|
|
Hi Ezuk,
| ezuk wrote: | | So... I think everything went fine except the part where it says "Monitor 3", I think you meant it to be for all monitors. Right? | Yes, your are right. I have updated my first post. Could you please run it again and update this post. Thanks.
| ezuk wrote: | | 1) I would have fixed it, but I don't really understand what you did with the %TEXT% variable. Is this documented somewhere? It looks like you set the variable to a different value every time, so I don't understand how the messagebox ended up containing all the text together. | Yes, it is documented (Introduction to Variables). But it is so simple that you can find it out yourself. Try this
| Code: | Text = Hi,
Text = %Text% my name is
Text = %Text% Ezuk.
MsgBox, %Text% |
_________________ Ciao
toralf  |
|
| Back to top |
|
 |
ezuk
Joined: 04 Jun 2005 Posts: 129
|
Posted: Fri Jun 10, 2005 1:16 pm Post subject: |
|
|
| toralf wrote: | Hi Ezuk,
Yes, your are right. I have updated my first post. Could you please run it again and update this post. Thanks. |
Hi Toralf
Wow, you're fast!
Sure thing: I ran the script again and it worked well. Now the messagebox text is:
| Quote: |
Current monitor is 1
Monitor 1:
Left: 0
Right: 1280
Top: 32
Bottom: 1024
Monitor 2:
Left: -1024
Right: 0
Top: 0
Bottom: 768
Monitor 3:
Left: 1280
Right: 2304
Top: 32
Bottom: 768
Window:
Title: Total Commander
X: 240
Y: 244
W: 800
H: 600
|
And re Text := %text%bla -- now I understand what you did! Cool!  |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|