Help me improve this code Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
xmachinery
Posts: 15
Joined: 14 May 2016, 21:25

Help me improve this code

21 Jan 2018, 13:45

Hello,

I've created a script which automates my work greatly. The main function of it is sending out email to users.

This is the code:

Code: Select all

SetTitleMatchMode, 2

!b::
SendInput, ^c					; Copy the whole text which would be parsed later
Sleep, 500						; Waits for 500 ms
ReadFullName := "Full Name+"		; Reads the line "Full Name+" from the clipboard and save it in ReadFullName variable
ReadTicketID := "Ticket ID+"		; Reads the line "Ticket ID+" from the clipboard and save it in ReadTicketID variable
ReadSummary := "Summary"		; Reads the line "Summary" from the clipboard and save it in ReadSummary variable
Status := 0					; Initialize Status variable to 0
Loop, Parse, Clipboard, `n, `r		; Reads the Clipboard
{
	if Status					; If Status variable is 1, read that line and save it in a variable (ReadFullName variable)
    {
		Name := A_LoopField
		Status := 0
		break
    }
	else if InStr(A_LoopField, ReadFullName)
	Status := 1
}

Loop, Parse, Clipboard, `n, `r
{
	if Status					; If Status variable is 1, read that line and save it in a variable (ReadTicketID variable)
    {
		Ticket := A_LoopField
		Status := 0
		break
    }
	else if InStr(A_LoopField, ReadTicketID)
	Status := 1
}

Loop, Parse, Clipboard, `n, `r
{
	if Status					; If Status variable is 1, read that line and save it in a variable (ReadSummary variable)
    {
		Summary := A_LoopField
		Status := 0
		break
    }
	else if InStr(A_LoopField, ReadSummary)
	Status := 1
}

; The below command does these things:
; - Activate Outlook - Untitled Message Window
; - Perform hotkey commands to navigate around the email's body and paste the copied variables to their respective places

WinActivate, Untitled - Message
SendInput, ^{Home}{End}^+{Left}%Ticket%{Down}{Left}^+{Left}%Name%{Down 2}^{Left}^+{Right}%Ticket%{Space}^{Right}^+{Right}%Summary%^{Home}{Down}+{Up}^x+{Tab}^v
return
This is the sample Clipboard data:

Code: Select all

Current mode: ModifyHide toolbar
 Save	 New search New search  New request New request	  Searches  My Reports  Advanced search  Clear  Status history	 Logout  Home
 

Ticket ID+
USIM100234567989
Resolve Before
1/22/2018 5:00:00 PM
Editor for Resolve BeforeStatus
Work In Progress
Arrival Time
1/19/2018 3:29:17 AM
Editor for Arrival TimeSubmitted By
me


708
Current Site
FIELD BASED
Menu for Current SiteProfileLocation
FIELDLOCATION
Special:
WIN7
Editor for Special:Notes
Site/ExtBusiness Unit:
BUSINESSUNIT

2050
Telephone
+1800123456789
ExternalLogin Name+
LOGINNAME
Full Name+
AUTOHOTKEY USER

 
General
 
 
Asset Info
 
 
Requester Info
 
 
Similar Cases
 
 
RTPA/Pandora
 
 
History Info
 
 
Assignment
 
 
Detail
 
 
Ticket History
 

Case Type
Incident
CBS HelpPriority
High
Source
Phone
Summary
AUTOHOTKEY - SCRIPTING HELP
Menu for SummaryEditor for SummaryDescription
- User wants help in scripting AUTOHOTKEY
Editor for DescriptionCode Lookup+
LOOKUP
Type+
STANDARD APPS
Menu for Type+Category+
CATEGORY
Menu for Category+Sub Code+
CODE
Menu for Sub Code+Group+
GROUP
Menu for Group+Individual+
INDIVIDUAL
Menu for Individual++FindDesc.Show AllSubmitter Grp
Menu for Submitter GrpCreate Problem Mgmt EntrySearch Existing Problems
This is what it will look like:
Image

It's all working fine and dandy, but I would like to ask if the code can be somewhat improved in a way?

Thank you!
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Help me improve this code  Topic is solved

21 Jan 2018, 15:01

Three possible ways:

Code: Select all

status := 0
Loop, Parse, Clipboard, `n, `r
{
   if (Status > 0 ) {
      dummy := status = 1 ? Name := A_LoopField : status = 2 ? Ticket := A_LoopField : status = 3 ? Summary := A_LoopField
      Status := 0
   }
   if ( RegExMatch(A_LoopField, "Full Name+"))
      status := 1
   if ( RegExMatch(A_LoopField, "Ticket ID+") )
      status := 2
   if ( RegExMatch(A_LoopField, "Summary") )
      Status := 3
}
MsgBox % "Ticket -->" Ticket "`nName -->" Name "`nSummary -->"  Summary
... or ...

Code: Select all

Arr := StrSplit(Clipboard,"`n")
Loop, % Arr.Length()
{
   dmy := RegExMatch(Arr[a_index], "Full Name+") ? name := Arr[a_index+1] : RegExMatch(Arr[a_index], "Ticket ID+")  ? ticket := Arr[a_index+1] : RegExMatch(Arr[a_index], "Summary") ? summary := Arr[a_index+1]
}
MsgBox % "Ticket -->" Ticket "`nName -->" Name "`nSummary -->"  Summary
... or also ..

Code: Select all

for i, v in Arr := StrSplit(Clipboard,"`n") {      
   if ( regexmatch(v, "O)Name+|TicketID+|Subject",m)  ) {
      x:= SubStr(m.value,1,1),dmy:=x="N"?name:=Arr[i+1]:x="T"?ticket:=Arr[i+1]:x="S"?subject:=Arr[i+1]:""
   }
}
MsgBox % "Ticket -->" Ticket "`nName -->" Name "`nSummary -->"  Summary
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Dewi Morgan and 385 guests