Improve Task Manager script (no image recognition, etc.)

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Improve Task Manager script (no image recognition, etc.)

Post by JRL » Wed Oct 25, 2017 12:49 am

Probably a parsing error. Different OS may write the results differently. Take a look at your temp directory and view the CPUandMEM.txt file.

Mine looks like this:

LoadPercentage
17


FreePhysicalMemory=5643716


If the output is different and parsing is the issue, the following amended script may help.

Code: Select all

Let>vRAM_Limit=85
Let>vCPU_Limit=50
Dialog>Dialog1
object Dialog1: TForm
  Left = 284
  Top = 178
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  BorderStyle = bsNone
  Caption = 'Memory and CPU'
  ClientHeight = 53
  ClientWidth = 422
  Color = 16777200
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  FormStyle = fsStayOnTop
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 8
    Top = 0
    Width = 96
    Height = 16
    Caption = 'Total Memory'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label2: TLabel
    Left = 120
    Top = 0
    Width = 104
    Height = 16
    Caption = 'Memory in Use'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label3: TLabel
    Left = 248
    Top = 0
    Width = 164
    Height = 16
    Caption = 'CPU Percentage in Use'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label4: TLabel
    Left = 8
    Top = 24
    Width = 323
    Height = 16
    Caption = 'First run sometimes takes a while.  Be patient...'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label5: TLabel
    Left = 120
    Top = 24
    Width = 5
    Height = 16
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label6: TLabel
    Left = 248
    Top = 24
    Width = 5
    Height = 16
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
end
EndDialog>Dialog1
AddDialogHandler>Dialog1,,OnClose,Quit
Let>WIN_USEHANDLE=1
  MoveWindow>Dialog1.Handle,0,0
Let>WIN_USEHANDLE=0
SetDialogObjectFont>Dialog1,Label4,MS Sans Serif,11,1,111111
Show>dialog1

Let>RP_Wait=1
Let>RP_Windowmode=0
//Let>RP_Admin=1

SRT>Quit
  Exit>0
END>Quit

RunProgram>cmd /c wmic computersystem get TotalPhysicalMemory > %temp_dir%CPUandMEM.txt
ReadLn>%temp_dir%CPUandMEM.txt,2,vTotalMem
Trim>vTotalMem,vTotalMem
Let>vTotalMem={(round((%vTotalMem%/1024^3)*100)/100)}
SetDialogObjectFont>Dialog1,Label4,MS Sans Serif,11,1,0
SetDialogProperty>Dialog1,Label4,Caption,vTotalMem
Label>Start
  GetDate>today
  GetTime>now
  RunProgram>cmd /c wmic cpu get loadpercentage > %temp_dir%CPUandMEM.txt
  RunProgram>cmd /c wmic OS get FreePhysicalMemory /Value >> %temp_dir%CPUandMEM.txt
 
  ReadLn>%temp_dir%CPUandMEM.txt,2,vCPUres
  Trim>vCPUres,vCPUres
 
    If>{%vCPUres%>%vCPU_Limit%}
      SetDialogObjectFont>Dialog1,Label6,MS Sans Serif,11,1,255
    Else
      SetDialogObjectFont>Dialog1,Label6,MS Sans Serif,11,1,0
    EndIf
 
  ReadFile>%temp_dir%CPUandMEM.txt,vData
  Separate>vData,FreePhysicalMemory=,VItem
  StringReplace>vItem_2,crlf,,vMEMres
  Trim>vMEMres,vMEMres
  StringReplace>vMEMres,,,vMEMres
  Let>vMEMres={%vTotalMem%-(round((%vMEMres%/1024^2)*100)/100)}
 
    SetDialogProperty>Dialog1,,Caption,Memory and CPU %today% %now%
    SetDialogProperty>Dialog1,Label6,Caption,vCPUres
    If>{((%vMEMres%/%vTotalMem%)*100)>%vRAM_Limit%}
      SetDialogObjectFont>Dialog1,Label5,MS Sans Serif,11,1,255
    Else
      SetDialogObjectFont>Dialog1,Label5,MS Sans Serif,11,1,0
    EndIf
    SetDialogProperty>Dialog1,Label5,Caption,vMEMres
    SetDialogObjectFont>Dialog1,Label4,MS Sans Serif,11,1,0
    SetDialogProperty>Dialog1,Label4,Caption,vTotalMem
   
 
  Let>WIN_USEHANDLE=1
    GetWindowPos>Dialog1.Handle,DiaX,DiaY
    MoveWindow>Dialog1.Handle,DiaX,DiaY
  Let>WIN_USEHANDLE=0
  Wait>0.01
Goto>Start

User avatar
Grovkillen
Automation Wizard
Posts: 1023
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Improve Task Manager script (no image recognition, etc.)

Post by Grovkillen » Tue Sep 26, 2023 5:19 pm

Is it possible to use this Kernel32 function? https://learn.microsoft.com/en-us/windo ... memoryinfo
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Improve Task Manager script (no image recognition, etc.)

Post by JRL » Wed Sep 27, 2023 1:34 pm

The function requires pointers to a memory location which is the first indicator that it will not work in Macro Scheduler.
[out] ppsmemCounters

A pointer to the PROCESS_MEMORY_COUNTERS or PROCESS_MEMORY_COUNTERS_EX structure that receives information about the memory usage of the process.

User avatar
Grovkillen
Automation Wizard
Posts: 1023
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Improve Task Manager script (no image recognition, etc.)

Post by Grovkillen » Wed Sep 27, 2023 3:31 pm

Thanks, got it
Let>ME=%Script%

Running: 15.0.24
version history

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts