CopyWink Posted October 19, 2024 Posted October 19, 2024 I am trying ty create a a script to toggle my PC screen DPi resolution with the app "SetDpi.exe", which sets the DPI by simply entering the command: SetDpi.exe 125. If I run the command "setdpi.exe get" I get a printout of the current screen resolution, say, "Current Resolution: 150". How do I write a script that can toggle between that value and 100 dpi? I looked at the AutoIt function "$gToggleState" and I do not understand it at all. So, yes, I tried to do my research but failed.
argumentum Posted October 20, 2024 Posted October 20, 2024 Welcome to the forum. Here is some help (StdoutRead). Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
CopyWink Posted October 20, 2024 Author Posted October 20, 2024 OK. I managed to come up with the script, below, after reading StdoutRead function. But I'm getting the error "subscript used on non accessible variable". in line 24 even though it checks out in Autoit's syntax check function. expandcollapse popup#RequireAdmin #include <AutoItConstants.au3> #include <MsgBoxConstants.au3> Local $setdpiPath = "C:\Bin\Scripts\AU3\SetDpi.exe" Local $currentDPI = 100 Local $toggleDPI = 150 While 1 $currentDPI = ToggleDPI($setdpiPath, $currentDPI, $toggleDPI) MsgBox($MB_ICONINFORMATION, "DPI Changed", "Current DPI: " & $currentDPI) Sleep(3000) WEnd Func ToggleDPI($exePath, $current, $toggle) Local $cmd, $pid, $output ; Get current DPI $pid = Run(@ComSpec & " /c " & $exePath & " get", "", @SW_HIDE, $STDOUT_CHILD) ProcessWaitClose($pid) $output = StdoutRead($pid) ; Extract current DPI from output $current = Number(StringRegExp($output, "Current Resolution: (\d+)", 1)[0] ? StringRegExp($output, "Current Resolution: (\d+)", 1)[0] : $current) ; Toggle DPI If $current == 100 Then $cmd = $exePath & " " & $toggle $current = $toggle Else $cmd = $exePath & " 100" $current = 100 EndIf $pid = Run(@ComSpec & " /c " & $cmd, "", @SW_HIDE, $STDOUT_CHILD) ProcessWaitClose($pid) Return $current EndFunc
argumentum Posted October 20, 2024 Posted October 20, 2024 (edited) "D:\Utilities\snmp_UDF-v1.7.4\New AutoIt v3 Script.au3" (24) : ==> Subscript used on non-accessible variable.: $current = Number(StringRegExp($output, "Current Resolution: (\d+)", 1)[0] ? StringRegExp($output, "Current Resolution: (\d+)", 1)[0] : $current) $current = Number(StringRegExp($output, "Current Resolution: (\d+)", 1)^ ERROR ..skip doing one-liners while you explore coding. Once you've got it working, add error catching then finish your script Return Value Flag = $STR_REGEXPMATCH (0) : @error: Meaning 2: Bad pattern. @extended = offset of error in pattern. Flag = $STR_REGEXPARRAYMATCH (1) or $STR_REGEXPARRAYFULLMATCH (2) : @error: Meaning 0: Array is valid. Check @extended for next offset 1: Array is invalid. No matches. 2: Bad pattern, array is invalid. @extended = offset of error in pattern. Flag = $STR_REGEXPARRAYGLOBALMATCH (3) or $STR_REGEXPARRAYGLOBALFULLMATCH (4) : @error: Meaning 0: Array is valid. 1: Array is invalid. No matches. 2: Bad pattern, array is invalid. @extended = offset of error in pattern. Read the help file in regards to StringRegExp() Edited October 20, 2024 by argumentum Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
Werty Posted October 20, 2024 Posted October 20, 2024 1 hour ago, CopyWink said: If $current == 100 Then In AutoIt "==" is only for comparing strings, with case-sensitivity, use single "=" for numbers. https://www.autoitscript.com/autoit3/docs/intro/lang_operators.htm Some guy's script + some other guy's script = my script!
Nine Posted October 20, 2024 Posted October 20, 2024 (edited) Use value instead of get, you won't need to SRE the result. And I think you should use a HotKeySet to make it toggle (not sure what this loop is for). Something like this : #include <Constants.au3> Global Const $SETDPI_PATH = @ScriptDir & "\SetDpi.exe" Global Const $SETDPI_VALUE = [100, 125] If Not FileExists($SETDPI_PATH) Then Exit MsgBox($MB_OK, "Error", "Invalid path to SetDpi.exe") HotKeySet("{F7}", ToggleDPI) HotKeySet("{ESC}", Terminate) While Sleep(1000) WEnd Func ToggleDPI() Local $iPID = Run($SETDPI_PATH & " value", "", @SW_HIDE, $STDOUT_CHILD) ProcessWaitClose($iPID) Local $iNew = Number(StdoutRead($iPID)) = $SETDPI_VALUE[0] ? $SETDPI_VALUE[1] : $SETDPI_VALUE[0] ConsoleWrite("New DPI set to " & $iNew & @CRLF) RunWait($SETDPI_PATH & " " & $iNew, "", @SW_HIDE) EndFunc Func Terminate() Exit EndFunc Edited October 21, 2024 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
CopyWink Posted October 21, 2024 Author Posted October 21, 2024 @Nine. Your code didn't work for me but thanks for the reply. But your code made me realize that I was overthinking this toggle issue from the beginning: I think the easiest way to toggle is just to assign a hotkey with the proper DPI command rather than put it in the context menu as a selection as I originally planned. Thanks again.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now