Text Alignment

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
DataLife
Posts: 447
Joined: 29 Sep 2013, 19:52

Text Alignment

03 May 2020, 10:45

I need to take a list of variables and position them in notepad in 2 columns and keep them in order as demostrated below.
page1.JPG
page1.JPG (21.3 KiB) Viewed 1650 times
page2.JPG
page2.JPG (16.1 KiB) Viewed 1650 times

The below code does not work. It only records 66 variables in 2 columns.

Code: Select all

NumberOfRecords = 177 ;number of records will change

loop %NumberOfRecords%  ;this creates the variables for this example. I pull the variables from mulitiple locations
 Var%a_index% = %a_index%
 
if NumberOfRecords > 33
 {
  loop 33 ;33 rows per page
   {
    ;these 4 lines creates the variables for each row from my list of variables. Example row 1 on page 1 would be Var1 and Var34
    tVar1++  
    tVar2 := (tVar1 + 33)
    mVar1 := var%tVar1% 
    mVar2 := var%tVar2%	
    	
    StringLen,Len,mVar1               ; These 5 lines are for proper spacing between columns
    NumberOfSpaces := (43 - Len) 
    Spaces =                                 
    Loop %NumberOfSpaces%        
     Spaces := (Spaces a_space)    
 
    if tVar2 > %NumberOfRecords% ;allows the last page to have only 1 variable in the first column and no variable in the second column when the total number of records have been positioned
     NewList := (Newlist "`n" mVar1 Spaces "|")
    else
     NewList := (Newlist "`n" mVar1 Spaces "|" a_space mVar2)
  }
}
FileDelete,%a_temp%\NewList.txt
FileAppend,%NewList%,%a_temp%\NewList.txt
run notepad.exe %a_temp%\NewList.txt
ExitApp
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
Smile_
Posts: 858
Joined: 03 May 2020, 00:51

Re: Text Alignment

03 May 2020, 11:04

Waht you mean with next page? means below the first page or in right of it?
User avatar
DataLife
Posts: 447
Joined: 29 Sep 2013, 19:52

Re: Text Alignment

03 May 2020, 11:32

Smile_ wrote:
03 May 2020, 11:04
Waht you mean with next page? means below the first page or in right of it?
Below page 1 is page 2
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
ahk7
Posts: 575
Joined: 06 Nov 2013, 16:35

Re: Text Alignment

03 May 2020, 11:42

Code: Select all

NumberOfRecords:=177
LinesPerPage:=33
ColBreak:=0

loop % NumberOfRecords  ;this creates the variables for this example. I pull the variables from mulitiple locations
 Var%a_index% = %a_index%

Loop, % NumberOfRecords
	{
     col1:=A_Index+ColBreak
     col2:=col1+LinesPerPage
     output .= Format("{:-45}", var%col1%) "| " var%col2% "`n"
     if (mod(A_Index,LinesPerPage) = 0)
         {
          output .= "-------------------------`n" ; Chr(12) ; Chr(12) pagefeed
          colbreak+=LinesPerPage
         }
     }
FileDelete,%a_scriptDir%\NewList.txt
FileAppend,% RTrim(output," -|`n"),%a_scriptDir%\NewList.txt
run notepad.exe %a_scriptDir%\NewList.txt
ExitApp
Last edited by ahk7 on 03 May 2020, 12:46, edited 1 time in total.
User avatar
DataLife
Posts: 447
Joined: 29 Sep 2013, 19:52

Re: Text Alignment

03 May 2020, 12:25

ahk7 wrote:
03 May 2020, 11:42

Code: Select all

NumberOfRecords:=177
LinesPerPage:=33
ColBreak:=0

loop % NumberOfRecords  ;this creates the variables for this example. I pull the variables from mulitiple locations
 Var%a_index% = %a_index%

Loop, % NumberOfRecords
	{
     col1:=A_Index+ColBreak
     col2:=col1+LinesPerPage
     output .= Format("{:-45}", var%col1%) "| " var%col2% "`n"
     if (col2 = NumberOfRecords)
          break
     if (mod(A_Index,LinesPerPage) = 0)
         {
          output .= "-------------------------`n" ; Chr(12) ; Chr(12) pagefeed
          colbreak+=LinesPerPage
         }
     }
FileDelete,%a_scriptDir%\NewList.txt
FileAppend,%output%,%a_scriptDir%\NewList.txt
run notepad.exe %a_scriptDir%\NewList.txt
ExitApp
This looks really good except variables 145 thru 165 are missing.
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
ahk7
Posts: 575
Joined: 06 Nov 2013, 16:35

Re: Text Alignment

03 May 2020, 12:49

Right you are, Updated code above - it now loops until the end, which does of course add lots of empty lines to the output, so these are trimmed using RTrim, which means the last line may be missing the | if there is no value in column 2.
164 |
165 ; misses |
garry
Posts: 3758
Joined: 22 Dec 2013, 12:50

Re: Text Alignment

03 May 2020, 13:53

@ahk7, thank you , works fine , I'll never find out ...
- Is it possible to send FormFeed / PageFeed FF chr(12) to the printer ? ( text1 + FF + text2 + FF ) PCL
- How to add leading zeros ? 001--099
ahk7
Posts: 575
Joined: 06 Nov 2013, 16:35

Re: Text Alignment

03 May 2020, 14:40

@garry you can use another format, see the "0" (zero) flag in the docs https://www.autohotkey.com/docs/commands/Format.htm
cols is now an array that is passed on to the next format which pads with spaces and inserts a | between the two columns.
If Chr(12) is present in a file and it is printed it should indeed print a new page, you can easily try with a PDF ...

Code: Select all

NumberOfRecords:=177
LinesPerPage:=33
ColBreak:=0

loop % NumberOfRecords  ;this creates the variables for this example. I pull the variables from mulitiple locations
 Var%a_index% = %a_index%

Loop, % NumberOfRecords
	{
     col1:=A_Index+ColBreak
     col2:=col1+LinesPerPage
     cols:=[Format("{:03}",var%col1%),Format("{:03}",var%col2% ? var%col2% : "   ")] ; if variable is empty pass on three spaces to avoid 000
	 if (var%col1% = "") ; we break here as there are no more variables it seems
		break
     output .= Format("{1:-45}| {2:}", cols*) "`n"
     if (mod(A_Index,LinesPerPage) = 0)
         {
          output .= "-------------------------`n" ; Chr(12) ; Chr(12) pagefeed
          colbreak+=LinesPerPage
         }
     }
FileDelete,%a_scriptDir%\NewList.txt
FileAppend,% RTrim(output,"-`n"),%a_scriptDir%\NewList.txt ; trim better now as we break when done 
run notepad.exe %a_scriptDir%\NewList.txt
ExitApp
garry
Posts: 3758
Joined: 22 Dec 2013, 12:50

Re: Text Alignment

03 May 2020, 15:35

@ahk7, thank you for the solutions
chr(12) FF FormFeed , with text-file I had no success , converted text-file with txt2pdf.exe to pdf-file , then was OK

Code: Select all

;- converted text-file with txt2pdf.exe  to pdf-file
transform,s,chr,12              ;- chr(12) FF 
;....
output .= "-------------------------`n" . s . "`n"   ; added Chr(12) FF / pagefeed
;....

Code: Select all

;============ TXT2PDF.exe ==============================
setworkingdir,%a_scriptdir%
converttxt2pdf:
fl1=%a_scriptdir%\newlist8.txt
PR1=%a_scriptdir%\txt2pdf.exe
loop,%PR1%                                               ;- create program shortpath
   SPR1= %A_loopFileShortPath%
loop,%FL1%                                               ;- create file    shortpath
   SFL1= %A_loopFileShortPath%

ifexist,%FL1%
 {
 Fileappend,`r`n,%sfl1%
 ;runwait,%comspec% /k %spr1% "%SFL1%" /dt,,             ;- /k DOS window keeps open to see what happens , so see also errors
 runwait,%comspec% /c %spr1% "%SFL1%" /dt,,hide
 SplitPath,SFL1,name, dir, ext, name_no_ext, drive      ;- and then open new created pdf-file in same folder like source .txt
 cnew=%dir%\%name_no_ext%.pdf
 ifexist,%cnew%
    run,%cnew%
 }
return
;=======================================================
User avatar
Smile_
Posts: 858
Joined: 03 May 2020, 00:51

Re: Text Alignment

03 May 2020, 16:34

I wanted to add to your script but I didn't get a solution so I had to remove somethings and add others.
I noticed that the second column is always first column +33 and when you move to new page it is also add +33 to last value of first column.
Once you initialize with +33 difference it keeps it since you add +1 or any value to each column of the page.
What is left some conditions in the end to make sure the value do not pass the number of records chosen in start.
I tried to make it as clearest as possible, hope it helps.

Code: Select all

NumberOfRecords = 999 ; Number of records will change.
LoopTimes = % NumberOfRecords 
mVar1 = 0
mVar2 = 33
if NumberOfRecords > 0
   {
    loop % LoopTimes // 2 + 33 ; Number of times of repeating the process.
    {
		mVar1 += 1 ; Add +1 to the number of the first column.
		mVar2 += 1 ; Add +1 to the number of the second column.
		If (Mod(mVar1-1, 33) = 0) and (mVar1<NumberOfRecords) { ; Check if we pass to new page or no.
			PageNumber += 1 ; Add +1 to page number (passing to new page)
			If PageNumber = 1 ; Check page number (to optimize the look of the output)
				NewList := (NewList "`nThis is how page " PageNumber " would look`n") ; Write page title.
			else
				NewList := (NewList "`n`nThis is how page " PageNumber " would look`n") ; Write page title.
		}
		StringLen,Len , mVar1               ; These 5 lines are for proper spacing between columns.
		NumberOfSpaces := (43 - Len)        ;
		Spaces =                            ;
		Loop %NumberOfSpaces%               ;
			Spaces := (Spaces a_space)      ;
		; Writing into NewList variable.
		If (mVar2<=NumberOfRecords) { ; If we still did not reached the end in both columns.
		NewList := (Newlist "`n" mVar1 Spaces "|" a_space mVar2) ; Write numbers in both columns.
		}
		If ((mVar2>NumberOfRecords) and  (mVar1<=NumberOfRecords)) { ; If we reached the end in second column but first column not yet.
		NewList := (Newlist "`n" mVar1 Spaces "|") ; Write left numbers in first column only.
		}
		If (Mod(mVar1, 33) = 0) { ; Check if we need to add +33 instead of +1 or no.
			mVar1 += 33 ; Add +33 to mVar1 to start writing in the new page.
		}
		mVar2 := % mVar1 + 33 ; mVar2 is always mVar1 + 33.
	}
}
ExitLoop:
FileDelete, %A_ScriptDir%\NewList.txt
FileAppend, %NewList%, %A_ScriptDir%\NewList.txt
run notepad.exe %A_ScriptDir%\NewList.txt
ExitApp
User avatar
DataLife
Posts: 447
Joined: 29 Sep 2013, 19:52

Re: Text Alignment

03 May 2020, 20:32

ahk7 wrote:
03 May 2020, 12:49
Right you are, Updated code above - it now loops until the end, which does of course add lots of empty lines to the output, so these are trimmed using RTrim, which means the last line may be missing the | if there is no value in column 2.
164 |
165 ; misses |
thank you very much
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
DataLife
Posts: 447
Joined: 29 Sep 2013, 19:52

Re: Text Alignment

04 May 2020, 14:18

Smile_ wrote:
03 May 2020, 16:34
I wanted to add to your script but I didn't get a solution so I had to remove somethings and add others.
I noticed that the second column is always first column +33 and when you move to new page it is also add +33 to last value of first column.
Once you initialize with +33 difference it keeps it since you add +1 or any value to each column of the page.
What is left some conditions in the end to make sure the value do not pass the number of records chosen in start.
I tried to make it as clearest as possible, hope it helps.

Code: Select all

NumberOfRecords = 999 ; Number of records will change.
LoopTimes = % NumberOfRecords 
mVar1 = 0
mVar2 = 33
if NumberOfRecords > 0
   {
    loop % LoopTimes // 2 + 33 ; Number of times of repeating the process.
    {
		mVar1 += 1 ; Add +1 to the number of the first column.
		mVar2 += 1 ; Add +1 to the number of the second column.
		If (Mod(mVar1-1, 33) = 0) and (mVar1<NumberOfRecords) { ; Check if we pass to new page or no.
			PageNumber += 1 ; Add +1 to page number (passing to new page)
			If PageNumber = 1 ; Check page number (to optimize the look of the output)
				NewList := (NewList "`nThis is how page " PageNumber " would look`n") ; Write page title.
			else
				NewList := (NewList "`n`nThis is how page " PageNumber " would look`n") ; Write page title.
		}
		StringLen,Len , mVar1               ; These 5 lines are for proper spacing between columns.
		NumberOfSpaces := (43 - Len)        ;
		Spaces =                            ;
		Loop %NumberOfSpaces%               ;
			Spaces := (Spaces a_space)      ;
		; Writing into NewList variable.
		If (mVar2<=NumberOfRecords) { ; If we still did not reached the end in both columns.
		NewList := (Newlist "`n" mVar1 Spaces "|" a_space mVar2) ; Write numbers in both columns.
		}
		If ((mVar2>NumberOfRecords) and  (mVar1<=NumberOfRecords)) { ; If we reached the end in second column but first column not yet.
		NewList := (Newlist "`n" mVar1 Spaces "|") ; Write left numbers in first column only.
		}
		If (Mod(mVar1, 33) = 0) { ; Check if we need to add +33 instead of +1 or no.
			mVar1 += 33 ; Add +33 to mVar1 to start writing in the new page.
		}
		mVar2 := % mVar1 + 33 ; mVar2 is always mVar1 + 33.
	}
}
ExitLoop:
FileDelete, %A_ScriptDir%\NewList.txt
FileAppend, %NewList%, %A_ScriptDir%\NewList.txt
run notepad.exe %A_ScriptDir%\NewList.txt
ExitApp
thanks for your suggestion. It also works.
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
garry
Posts: 3758
Joined: 22 Dec 2013, 12:50

Re: Text Alignment

04 May 2020, 14:36

@ahk7 , thank you again for solutions
a small script again with command format

Code: Select all

f1=%a_scriptdir%\testfile.txt  ;- result here / use nonproportional script in notepad/text-editor
e1=
(join`r`n
col1, col         2, collllllllllllllll3
c1, col  2, coll3
)
;- 3rd column is right alignment 
Loop, parse,e1, `n,`r
   {
    a:=StrSplit(A_LoopField,",")
     newline .= Format("{:-20}|{:-20}|{:20}`r`n",a*)
   }

if newline<>
  {
  ifexist,%f1%
       FileDelete,%f1%
  FileAppend, %newline%,%f1%
  run,%f1%
  }
e1=
exitapp
ahk7
Posts: 575
Joined: 06 Nov 2013, 16:35

Re: Text Alignment

05 May 2020, 02:47

@garry You can also use it to draw lines (repeat chars) - here it creates 40 zeros which we then replace with a nice line character

Code: Select all

MsgBox % "SomeText `n" StrReplace( Format( "{:040}", "" ), 0, Chr(0x2014) ) "`nmore text"
this makes it easier to adjust when it needs to be wider...
;)
garry
Posts: 3758
Joined: 22 Dec 2013, 12:50

Re: Text Alignment

05 May 2020, 03:35

@ahk7, thank you for your scripts
here an example from user 'BoBo'
idea , check maximum lenght from columns (T%a_index) , also possible with ahk-command 'Format'

Code: Select all

;-- columnwidth  alignment linepadding right or left 
;-- use nonproportional font like 'lucida console' etc 
#warn
#noenv
setworkingdir,%a_scriptdir%
F2=%A_scriptdir%\test22.txt      ;- result
dlm = `;                         ;- delimiter
  e=
  (Ltrim Join`r`n
  Tom McIntire;Atlanta Andersen Avenue;aaaaaa
  Vitor Mateus Teixeira;Catford SE Culverley rd 109;bbb
  Mike Lewis;Lewisham Cleveland Mews;cccc
  aaa;bbb;CC
  )
;-  get number of columns ------------------
Loop, Parse,e, `n, `r
{
stringsplit,k,a_loopfield,%dlm%
           total:=k0
break
}
loop,%total%
  T%a_index%:=""

;------ check columns max width ------
for each, Line in StrSplit(e, "`n", "`r")
{
if line=
  continue
stringsplit,A,line,%dlm%
loop,%total%
  {
  stringlen,L%a_index%,A%a_index%
  if (T%a_index%<L%a_index%)
      T%a_index%:=L%a_index%+1          ;-maximal lenght column-x
  }
}
e3:=""
;--------------------------------------------------------
Loop, Parse,e, `n, `r
     {
     x:= A_LoopField
     if x=
        continue
     i=0
     e2:=""
     loop,%total%
       {
       i++
       StringSplit,u,x,%dlm%
       if (i=3)                     ;-- example define alignment(R) for column-3
         ax:=LP(u%i%,T%i%," ","R")  ;-- right alignment
       else
         ax:=LP(u%i%,T%i%," ","L")  ;-- left  alignment
       e2 .= ax . dlm        
       }
     stringtrimright,e2,e2,1
     e3 .= e2 . "|`r`n"
     }
if e3<>
 {
 ifexist,%f2%
   filedelete,%f2%
 fileappend,%e3%,%f2%
 run,%f2%
 }
e3=
exitapp
esc::exitapp

;------------- functionx von BoBo Linepaddingx alignmentx -------------
LP(String,FieldLen,ToAppend,Justification)
 {
   appended:=""
   StringLen, StringLen, String
   LCnt := FieldLen-StringLen
   Loop, % LCnt
     Appended .=  ToAppend
   If Justification = R
      Return (Appended . String)
   If Justification = L
         Return (String . Appended)
 }
;=============== end script ============================================

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 302 guests