d3k/func.ahk
2023-02-13 16:20:22 -06:00

169 lines
4.8 KiB
AutoHotkey

#SingleInstance force ; Only one instance at a time
SendMode, Input
FileEncoding, UTF-8 ; Makes sure the special characters dont break stuff
SetWorkingDir, %A_MyDocuments%\D3K ; Make sure we can find the .ahks linked below
SetTitleMatchMode, 2
d3k_version := 3 ; The current version
GetDateTime(lang)
{
FormatStr := "yyyy-MM-dd" ; The Date
FormatTime, DateStr, %A_NowUTC%, %FormatStr% ; format it to make it look like the Date we want
FormatStr := "H:mm'Z'" ; The time
FormatTime, TimeStr, %A_NowUTC%, %FormatStr% ; Format the string to be better
if (lang = "en")
{
Return DateStr " at " TimeStr
}
if (lang = "de")
{
Return DateStr " um " TimeStr
}
if (lang = "ISO8601")
{
Return DateStr "T" TimeStr
}
if (lang = "date")
{
Return DateStr
}
}
GetInfo(xID)
{
if (xID)
{
File := FileOpen(A_MyDocuments "\emails.csv", "r")
Loop
{
Line := File.ReadLine()
Array := StrSplit(Line,",") ; split the line at its separating commas to get separate "columns" AKA elements
if InStr(Array[1], xID) ; if at the specified element (1) the specified content (dealers xID) has been found
{
Test := {xid: Array[1], email: Array[2], name: Array[3], user: Array[4], org: Array[5]} ; Return the array so we can do {variable}.email and so on
File.Close() ; Close the file
Return Test ; Return the array with the deets that we found
}
} Until (File.AtEOF)
File.Close()
Return false
}
if !(xID)
{
Return false
}
}
;------------------------------------------------------------------------------
; INI Manipulation that supports UTF-8
;------------------------------------------------------------------------------
ini_load(location)
{
out := {}
File := FileOpen(location, "r")
cleaner := File.Read()
File.Close()
cleaner := RegExReplace(cleaner, "`am)^[\s\R]*") ; Remove empty lines
cleaner := RegExReplace(cleaner, "m)[ \t]+$") ; Get rid of extra space at the end of values (NEEDS FIXED)
File := FileOpen(location, "w")
File.Write(cleaner)
File.Close()
File := FileOpen(location, "r")
Loop
{
Line := File.ReadLine()
RegExMatch(Line, "(?<Section>(?<=\[).*(?=\]))|(?<Key>.*)=(?<Value>.*)", r) ; Key = rKey, Value = rValue, Section = rSection
If (rSection)
{
rSection := Trim(rSection, " `t`r`n") ; Remove pesky newlines and extra space
rSection := StrReplace(rSection, " ", "§") ; AHK can't handle spaces in nested array names, so they're substituted with §
out[rSection]:={}
c_Section := rSection
} Else
{
rKey := Trim(rKey, " `t`r`n")
rValue := Trim(rValue, " `t`r`n")
out[c_Section, rKey] := rValue
}
} Until (File.AtEOF)
File.Close()
Return out
}
ini_write(location, inKey, inValue) ; Don't need to specify section because you can only have one hotstring anyway
{
Critical, On
file := FileOpen(location, "r")
temp := file.Read()
file.Close()
inKey := "(?<=" inKey "=)\S*" ; Regex to make sure we're matching only the value for the requested key
temp := RegExReplace(temp, inKey, inValue)
; msgbox, % temp "`n`nRegex:`t" inKey "`nReplacing match with:`t" inValue
file := FileOpen(location, "w")
file.Write(temp)
file.Close()
}
;------------------------------------------------------------------------------
; Make sure the settings file exists, if not, create it
;------------------------------------------------------------------------------
sett_ini := A_MyDocuments "\D3Ksettings.ini" ; Where the settings ini is
If !FileExist(sett_ini) ; Check if the sett_ini doesn't exist
{
file := FileOpen(sett_ini, "w")
writethis := "
( LTrim
[USpec]
Email=LastnameFirstname@JohnDeere.com
Name=Firstname
DocFile=Select File
[Main]
Mansol=1
MJDPaste=1
CaseFormatter=1
CFmail=0
[Replacement]
JDProductNames=1
GloVar=0
[Language]
German=0
)"
file.Write(writethis)
file.Close()
Run Settings.ahk ; Runs the GUI for changing the settings, accessible with Alt+Shift+\ (also easy way to restart this script)
}
;------------------------------------------------------------------------------
; Read the sett_ini
;------------------------------------------------------------------------------
; This is the part that reads the file to see what your settings are. Variables in the sett_ini file are in all CamelCase, variables here are lowercase for more distinction
settings := {}
settings := ini_load(sett_ini)
; User Specific things:
email := settings["USpec", "Email"]
name := settings["USpec", "Name"]
docfile := settings["USpec", "DocFile"]
; General Settings
mansol := settings["Main", "Mansol"]
supadmin := settings["Main", "Supadmin"]
mjdpaste := settings["Main", "MJDPaste"]
caseformatter := settings["Main", "CaseFormatter"]
cfmail := settings["Main", "CFmail"]
; Replacement
jdpn := settings["Replacement", "JDProductNames"]
glovar := settings["Replacement", "GloVar"]
; Language
ger := settings["Language", "German"]