Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Open sidebar
- Move Window with VBScript.
- signupnathan@gmail.com
- Please write a vbs batch script to move a window from one lo action to another
- As a language model, I can provide you with the following VBScript code to move a window from one location to another on the screen:
- vb
- Copy code
- Set objShell = CreateObject("Shell.Application")
- Set objWindows = objShell.Windows()
- ' Change "Window Title" to the title of the window you want to move
- strWindowTitle = "Window Title"
- ' Change these values to the desired new location of the window
- intLeft = 100
- intTop = 100
- For Each objWindow in objWindows
- If InStr(1, objWindow.Document.Title, strWindowTitle) > 0 Then
- objWindow.Left = intLeft
- objWindow.Top = intTop
- Exit For
- End If
- Next
- To use this code, save it with a .vbs extension (e.g. move_window.vbs) and double-click the file to run it. You can also run the script from the command line using cscript.exe move_window.vbs. Make sure to change the values of strWindowTitle, intLeft, and intTop to match the title of the window you want to move and the desired new location.
- signupnathan@gmail.com
- Nice. Thank you.
- You're welcome! Let me know if you have any other questions.
- signupnathan@gmail.com
- Add code to that to store the initial location of the window before moving it.
- Sure, here's an updated VBScript code that will store the initial location of the window before moving it:
- vb
- Copy code
- Set objShell = CreateObject("Shell.Application")
- Set objWindows =d objShell.Windows()
- ' Change "Window Title" to the title of the window you want to move
- strWindowTitle = "Window Title"
- ' Store the initial location of the window
- Dim intLeft, intTop
- For Each objWindow in objWindows
- If InStr(1, objWindow.Document.Title, strWindowTitle) > 0 Then
- intLeft = objWindow.Left
- intTop = objWindow.Top
- Exit For
- End If
- Next
- ' Move the window to the new location
- Dim newLeft, newTop
- newLeft = 100 ' Change this to the desired new location of the window
- newTop = 100 ' Change this to the desired new location of the window
- For Each objWindow in objWindows
- If InStr(1, objWindow.Document.Title, strWindowTitle) > 0 Then
- objWindow.Left = newLeft
- objWindow.Top = newTop
- Exit For
- End If
- Next
- ' Show a message box to confirm the new location of the window
- strMessage = "Window was moved from (" & intLeft & "," & intTop & ") to (" & newLeft & "," & newTop & ")"
- MsgBox strMessage
- This code first searches for the window with the specified title and stores its initial location in the intLeft and intTop variables. Then it moves the window to the new location specified by newLeft and newTop. Finally, it shows a message box to confirm the new location of the window.
- Again, be sure to change the values of strWindowTitle, newLeft, and newTop to match the window you want to move and the desired new location.
- signupnathan@gmail.com
- Please add a keyboard hook to trigger that move using control-alt-shift-m
- Certainly! Here's an updated VBScript code that adds a keyboard hook to trigger the window move when the user presses Control-Alt-Shift-M:
- vb
- Copy code
- Set objShell = CreateObject("Shell.Application")
- Set objWindows = objShell.Windows()
- ' Change "Window Title" to the title of the window you want to move
- strWindowTitle = "Window Title"
- ' Store the initial location of the window
- Dim intLeft, intTop
- For Each objWindow in objWindows
- If InStr(1, objWindow.Document.Title, strWindowTitle) > 0 Then
- intLeft = objWindow.Left
- intTop = objWindow.Top
- Exit For
- End If
- Next
- ' Define the key combination to trigger the window move
- Const KEY_CONTROL = &H11
- Const KEY_ALT = &H12
- Const KEY_SHIFT = &H10
- Const KEY_M = &H4D
- Set objShellWindows = CreateObject("Shell.Application").Windows
- Set objShellWindowsEvents = objShellWindows.GetType().InvokeMember("Events", _
- BindingFlags.GetProperty, Nothing, objShellWindows, Nothing)
- Set objKeyboardEvents = WScript.CreateObject("Scripting.Dictionary")
- objKeyboardEvents.Add "KeyDown", GetRef("OnKeyDown")
- objShellWindowsEvents.WindowRegistered(objKeyboardEvents)
- ' Function to handle the key combination
- Function OnKeyDown(Wnd, Event, Arg1, Arg2)
- If Arg1 = KEY_CONTROL And Arg2 = (KEY_ALT Or KEY_SHIFT) And Event.KeyCode = KEY_M Then
- ' Move the window to the new location
- Dim newLeft, newTop
- newLeft = 100 ' Change this to the desired new location of the window
- newTop = 100 ' Change this to the desired new location of the window
- For Each objWindow in objWindows
- If InStr(1, objWindow.Document.Title, strWindowTitle) > 0 Then
- objWindow.Left = newLeft
- objWindow.Top = newTop
- Exit For
- End If
- Next
- ' Show a message box to confirm the new location of the window
- strMessage = "Window was moved from (" & intLeft & "," & intTop & ") to (" & newLeft & "," & newTop & ")"
- MsgBox strMessage
- End If
- End Function
- ' Keep the script running to receive keyboard events
- Do While True
- WScript.Sleep 1000
- Loop
- This code adds a keyboard hook that listens for the Control-Alt-Shift-M key combination. When the user presses this key combination, the OnKeyDown function is called, which moves the window to the new location specified by newLeft and newTop. It then shows a message box to confirm the new location of the window.
- To use this script, save it with a .vbs extension (e.g. move_window_with_keyboard_hook.vbs) and double-click the file to run it. Press Control-Alt-Shift-M to move the window to the new location. The script will continue running in the background to receive keyboard events. To stop the script, you can close the command prompt window or terminate the process in Task Manager.
- Send a message...
- ChatGPT Mar 23 Version. Free Research Preview. ChatGPT may produce inaccurate information about people, places, or facts
- New chat
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement