Jump to content

Recommended Posts

Posted

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.

Posted

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.

#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

 

Posted (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 by argumentum

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted (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 by Nine
Posted

@NineYour 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.

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...