Why are my global variables not global?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
morxy49
Posts: 21
Joined: 04 Aug 2016, 03:44

Why are my global variables not global?

16 Apr 2018, 13:16

I'm having severe issues getting my various functions for user settings to work.
I have declared global variables in the beginning of the code, then i have a couple of functions like userSettings, saveSettings, loadSettings, setDefaultSettings etc that should access those global variables. But the can't. For some reason the variables remain untouched and everything the functions does is local and then discarded once it returns.

Why??

Code: Select all

#SingleInstance force

;initial variables
settingsfile = %A_MyDocuments%\csc_helper-user_settings.ini
paused := false
global FirstName
global LastName
global TeliaID
global PersonalCompanyEmail
global PersonalCompanyPhone
global CarrierCSCemail
global NCMemail
global PrimaryBrowserProcess
global OutlookProcess
global INCApassword
global INCApath
global INCAprocess
global CallguideProcess
global CallguidePath



checkForSettingsFile(settingsfile)

;************************************************************************************************************************ 

checkForSettingsFile(settingsfile){
	IfExist, %settingsfile% 
		loadSettings(settingsfile)
	else {
		MsgBox,,CSC Helper Scripts - Settings not found, No user settings found. Click OK to create and open settings file.
		FileAppend,, %settingsfile%
		sleep, 500
		setDefaultSettings(settingsfile)
		sleep, 100
		userSettings(settingsfile)
	}
	return
}

mainMenu(settingsfile){
	global
	Gui, New,, CSC Helper Scripts - Menu
	Gui, Add, Button, x12 y140 w140 h40 , User Settings
	Gui, Add, Button, x12 y190 w140 h40 , Help
	Gui, Add, Button, x12 y240 w140 h40 , Exit CSC Helper Scripts
	Gui, Show
	Return

	GuiClose:
	Gui, Cancel
	return
	
	ButtonUserSettings:
	Gui, Cancel
	userSettings(settingsfile)
	return
	
	ButtonHelp:
	Gui, Cancel
	help()
	return
	
	ButtonExitCSCHelperScripts:
	ExitApp
	return
}

userSettings(settingsfile){
	global
	Gui, New,, CSC Helper Scripts - User settings
	Gui, Add, Text,, Firstname
	Gui, Add, Text,, Lastname
	Gui, Add, Text,, Telia ID/Username
	Gui, Add, Text,, Personal email address (Telia)
	Gui, Add, Text,, Personal phone number (Telia)
	Gui, Add, Text,, INCA-password
	Gui, Add, Text,, CSC email address
	Gui, Add, Text,, NCM email address
	Gui, Add, Text,, Primary browser process
	Gui, Add, Text, w200 0x10  ;Horizontal Line > Etched Gray
	Gui, Add, Text,, Local path to INCA (Advanced setting)
	Gui, Add, Text,, INCA process name (Advanced setting)
	Gui, Add, Text,, Outlook process name (Advanced setting)
	Gui, Add, Text,, Callguide process name (Advanced setting)
	Gui, Add, Text,, Local path to Callguide (Advanced setting)
	Gui, Add, Edit, ym w350 vFirstName, %FirstName%
	Gui, Add, Edit, w350 vLastName, %LastName%
	Gui, Add, Edit, w350 vTeliaID, %TeliaID%
	Gui, Add, Edit, w350 vPersonalCompanyEmail, %PersonalCompanyEmail%
	Gui, Add, Edit, w350 vPersonalCompanyPhone, %PersonalCompanyPhone%
	Gui, Add, Edit, Password w350 vINCApassword, %INCApassword%
	Gui, Add, Edit, w350 vCarrierCSCemail, %CarrierCSCemail%
	Gui, Add, Edit, w350 vNCMemail, %NCMemail%
	Gui, Add, ComboBox, w350 vPrimaryBrowserProcess, chrome.exe|firefox.exe|iexplore.exe|microsoftedge.exe|%PrimaryBrowserProcess%||
	Gui, Add, Text, w350 0x10  ;Horizontal Line > Etched Gray
	Gui, Add, Edit, w350 vINCApath, %INCApath%
	Gui, Add, Edit, w350 vINCAprocess, %INCAprocess%
	Gui, Add, Edit, w350 vOutlookProcess, %OutlookProcess%
	Gui, Add, Edit, w350 vCallguideProcess, %CallguideProcess%
	Gui, Add, Edit, w350 vCallguidePath, %CallguidePath%
	
	Gui, Add, Button, Default, Apply
	Gui, Add, Button,, Cancel
	Gui, Show
	Return
	
	ButtonApply:
	saveSettings(settingsfile)
	Gui, Submit
	Return
	
	ButtonCancel:
	Gui, Cancel
	Return
}


loadSettings(settingsfile){
	global
	IniRead, FirstName, %settingsfile%, standard, FirstName
	IniRead, LastName, %settingsfile%, standard, LastName
	IniRead, TeliaID, %settingsfile%, standard, TeliaID
	IniRead, PersonalCompanyEmail, %settingsfile%, standard, PersonalCompanyEmail
	IniRead, PersonalCompanyPhone, %settingsfile%, standard, PersonalCompanyPhone
	IniRead, INCApassword, %settingsfile%, standard, INCApassword
	IniRead, PrimaryBrowserProcess, %settingsfile%, standard, PrimaryBrowserProcess
	IniRead, CarrierCSCemail, %settingsfile%, standard, CarrierCSCemail
	IniRead, NCMemail, %settingsfile%, standard, NCMemail
	IniRead, INCApath, %settingsfile%, advanced, INCApath
	IniRead, INCAprocess, %settingsfile%, advanced, INCAprocess
	IniRead, OutlookProcess, %settingsfile%, advanced, OutlookProcess
	IniRead, CallguideProcess, %settingsfile%, advanced, CallguideProcess
	IniRead, CallguidePath, %settingsfile%, advanced, CallguidePath
	TrayTip, CSC Helper Scripts, User settings loaded., 6, 1
	return
}

saveSettings(settingsfile){
	global
	IniWrite, %FirstName%, %settingsfile%, standard, FirstName
	IniWrite, %LastName%, %settingsfile%, standard, LastName
	IniWrite, %TeliaID%, %settingsfile%, standard, TeliaID
	IniWrite, %PersonalCompanyEmail%, %settingsfile%, standard, PersonalCompanyEmail
	IniWrite, %PersonalCompanyPhone%, %settingsfile%, standard, PersonalCompanyPhone
	IniWrite, %INCApassword%, %settingsfile%, standard, INCApassword
	IniWrite, %PrimaryBrowserProcess%, %settingsfile%, standard, PrimaryBrowserProcess
	IniWrite, %CarrierCSCemail%, %settingsfile%, standard, CarrierCSCemail
	IniWrite, %NCMemail%, %settingsfile%, standard, NCMemail
	IniWrite, %INCApath%, %settingsfile%, advanced, INCApath
	IniWrite, %INCAprocess%, %settingsfile%, advanced, INCAprocess
	IniWrite, %OutlookProcess%, %settingsfile%, advanced, OutlookProcess
	IniWrite, %CallguideProcess%, %settingsfile%, advanced, CallguideProcess
	IniWrite, %CallguidePath%, %settingsfile%, advanced, CallguidePath
	MsgBox,,CSC Helper Scripts, User settings saved
	Return
}

help(){
	Msgbox, Help is not available right now. Please try another time, maybe next year.
	return
}
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Why are my global variables not global?

16 Apr 2018, 14:07

tested with:

Code: Select all

#SingleInstance, Force
SetWorkingDir, %A_ScriptDir%

;initial variables
settingsfile := "csc_helper-user_settings.ini"
paused := false
global FirstName
global LastName
global TeliaID
global PersonalCompanyEmail
global PersonalCompanyPhone
global CarrierCSCemail
global NCMemail
global PrimaryBrowserProcess
global OutlookProcess
global INCApassword
global INCApath
global INCAprocess
global CallguideProcess
global CallguidePath



checkForSettingsFile(settingsfile)
mainMenu(settingsfile)

;************************************************************************************************************************

checkForSettingsFile(settingsfile){
	IfExist, %settingsfile%
		loadSettings(settingsfile)
	else {
		MsgBox,,CSC Helper Scripts - Settings not found, No user settings found. Click OK to create and open settings file.
		FileAppend,, %settingsfile%
		sleep, 500
		; setDefaultSettings(settingsfile)
		sleep, 100
		userSettings(settingsfile)
	}
	return
}

mainMenu(settingsfile){
	global
	Gui, New,, CSC Helper Scripts - Menu
	Gui, Add, Button, x12 y140 w140 h40 , User Settings
	Gui, Add, Button, x12 y190 w140 h40 , Help
	Gui, Add, Button, x12 y240 w140 h40 , Exit CSC Helper Scripts
	Gui, Show
	Return

	GuiClose:
	Gui, Cancel
	return

	ButtonUserSettings:
	Gui, Cancel
	userSettings(settingsfile)
	return

	ButtonHelp:
	Gui, Cancel
	help()
	return

	ButtonExitCSCHelperScripts:
	ExitApp
	return
}

userSettings(settingsfile){
	global
	Gui, New,, CSC Helper Scripts - User settings
	Gui, Add, Text,, Firstname
	Gui, Add, Text,, Lastname
	Gui, Add, Text,, Telia ID/Username
	Gui, Add, Text,, Personal email address (Telia)
	Gui, Add, Text,, Personal phone number (Telia)
	Gui, Add, Text,, INCA-password
	Gui, Add, Text,, CSC email address
	Gui, Add, Text,, NCM email address
	Gui, Add, Text,, Primary browser process
	Gui, Add, Text, w200 0x10  ;Horizontal Line > Etched Gray
	Gui, Add, Text,, Local path to INCA (Advanced setting)
	Gui, Add, Text,, INCA process name (Advanced setting)
	Gui, Add, Text,, Outlook process name (Advanced setting)
	Gui, Add, Text,, Callguide process name (Advanced setting)
	Gui, Add, Text,, Local path to Callguide (Advanced setting)
	Gui, Add, Edit, ym w350 vFirstName, %FirstName%
	Gui, Add, Edit, w350 vLastName, %LastName%
	Gui, Add, Edit, w350 vTeliaID, %TeliaID%
	Gui, Add, Edit, w350 vPersonalCompanyEmail, %PersonalCompanyEmail%
	Gui, Add, Edit, w350 vPersonalCompanyPhone, %PersonalCompanyPhone%
	Gui, Add, Edit, Password w350 vINCApassword, %INCApassword%
	Gui, Add, Edit, w350 vCarrierCSCemail, %CarrierCSCemail%
	Gui, Add, Edit, w350 vNCMemail, %NCMemail%
	Gui, Add, ComboBox, w350 vPrimaryBrowserProcess, chrome.exe|firefox.exe|iexplore.exe|microsoftedge.exe|%PrimaryBrowserProcess%||
	Gui, Add, Text, w350 0x10  ;Horizontal Line > Etched Gray
	Gui, Add, Edit, w350 vINCApath, %INCApath%
	Gui, Add, Edit, w350 vINCAprocess, %INCAprocess%
	Gui, Add, Edit, w350 vOutlookProcess, %OutlookProcess%
	Gui, Add, Edit, w350 vCallguideProcess, %CallguideProcess%
	Gui, Add, Edit, w350 vCallguidePath, %CallguidePath%

	Gui, Add, Button, Default, Apply
	Gui, Add, Button,, Cancel
	Gui, Show
	Return

	ButtonApply:
	saveSettings(settingsfile)
	Gui, Submit
	Return

	ButtonCancel:
	Gui, Cancel
	Return
}


loadSettings(settingsfile){
	global
	IniRead, FirstName, %settingsfile%, standard, FirstName
	IniRead, LastName, %settingsfile%, standard, LastName
	IniRead, TeliaID, %settingsfile%, standard, TeliaID
	IniRead, PersonalCompanyEmail, %settingsfile%, standard, PersonalCompanyEmail
	IniRead, PersonalCompanyPhone, %settingsfile%, standard, PersonalCompanyPhone
	IniRead, INCApassword, %settingsfile%, standard, INCApassword
	IniRead, PrimaryBrowserProcess, %settingsfile%, standard, PrimaryBrowserProcess
	IniRead, CarrierCSCemail, %settingsfile%, standard, CarrierCSCemail
	IniRead, NCMemail, %settingsfile%, standard, NCMemail
	IniRead, INCApath, %settingsfile%, advanced, INCApath
	IniRead, INCAprocess, %settingsfile%, advanced, INCAprocess
	IniRead, OutlookProcess, %settingsfile%, advanced, OutlookProcess
	IniRead, CallguideProcess, %settingsfile%, advanced, CallguideProcess
	IniRead, CallguidePath, %settingsfile%, advanced, CallguidePath
	TrayTip, CSC Helper Scripts, User settings loaded., 6, 1
	return
}

saveSettings(settingsfile){
	global
	IniWrite, %FirstName%, %settingsfile%, standard, FirstName
	IniWrite, %LastName%, %settingsfile%, standard, LastName
	IniWrite, %TeliaID%, %settingsfile%, standard, TeliaID
	IniWrite, %PersonalCompanyEmail%, %settingsfile%, standard, PersonalCompanyEmail
	IniWrite, %PersonalCompanyPhone%, %settingsfile%, standard, PersonalCompanyPhone
	IniWrite, %INCApassword%, %settingsfile%, standard, INCApassword
	IniWrite, %PrimaryBrowserProcess%, %settingsfile%, standard, PrimaryBrowserProcess
	IniWrite, %CarrierCSCemail%, %settingsfile%, standard, CarrierCSCemail
	IniWrite, %NCMemail%, %settingsfile%, standard, NCMemail
	IniWrite, %INCApath%, %settingsfile%, advanced, INCApath
	IniWrite, %INCAprocess%, %settingsfile%, advanced, INCAprocess
	IniWrite, %OutlookProcess%, %settingsfile%, advanced, OutlookProcess
	IniWrite, %CallguideProcess%, %settingsfile%, advanced, CallguideProcess
	IniWrite, %CallguidePath%, %settingsfile%, advanced, CallguidePath
	MsgBox,,CSC Helper Scripts, User settings saved
	Return
}

help(){
	Msgbox, Help is not available right now. Please try another time, maybe next year.
	return
}
and

Code: Select all

[standard]
FirstName=BBBBBBBB
variables are loaded correctly
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Why are my global variables not global?

16 Apr 2018, 14:11

This is odd, but since you are declaring the variables as super-global, you might try removing the unneeded global tag from your functions. No idea if it will make a difference, but it could.
morxy49
Posts: 21
Joined: 04 Aug 2016, 03:44

Re: Why are my global variables not global?

16 Apr 2018, 14:18

MaxAstro wrote:This is odd, but since you are declaring the variables as super-global, you might try removing the unneeded global tag from your functions. No idea if it will make a difference, but it could.
Already tried that. No difference...
morxy49
Posts: 21
Joined: 04 Aug 2016, 03:44

Re: Why are my global variables not global?

16 Apr 2018, 14:21

swagfag wrote:tested with:

Code: Select all

#SingleInstance, Force
SetWorkingDir, %A_ScriptDir%

;initial variables
settingsfile := "csc_helper-user_settings.ini"
paused := false
global FirstName
global LastName
global TeliaID
global PersonalCompanyEmail
global PersonalCompanyPhone
global CarrierCSCemail
global NCMemail
global PrimaryBrowserProcess
global OutlookProcess
global INCApassword
global INCApath
global INCAprocess
global CallguideProcess
global CallguidePath



checkForSettingsFile(settingsfile)
mainMenu(settingsfile)

;************************************************************************************************************************

checkForSettingsFile(settingsfile){
	IfExist, %settingsfile%
		loadSettings(settingsfile)
	else {
		MsgBox,,CSC Helper Scripts - Settings not found, No user settings found. Click OK to create and open settings file.
		FileAppend,, %settingsfile%
		sleep, 500
		; setDefaultSettings(settingsfile)
		sleep, 100
		userSettings(settingsfile)
	}
	return
}

mainMenu(settingsfile){
	global
	Gui, New,, CSC Helper Scripts - Menu
	Gui, Add, Button, x12 y140 w140 h40 , User Settings
	Gui, Add, Button, x12 y190 w140 h40 , Help
	Gui, Add, Button, x12 y240 w140 h40 , Exit CSC Helper Scripts
	Gui, Show
	Return

	GuiClose:
	Gui, Cancel
	return

	ButtonUserSettings:
	Gui, Cancel
	userSettings(settingsfile)
	return

	ButtonHelp:
	Gui, Cancel
	help()
	return

	ButtonExitCSCHelperScripts:
	ExitApp
	return
}

userSettings(settingsfile){
	global
	Gui, New,, CSC Helper Scripts - User settings
	Gui, Add, Text,, Firstname
	Gui, Add, Text,, Lastname
	Gui, Add, Text,, Telia ID/Username
	Gui, Add, Text,, Personal email address (Telia)
	Gui, Add, Text,, Personal phone number (Telia)
	Gui, Add, Text,, INCA-password
	Gui, Add, Text,, CSC email address
	Gui, Add, Text,, NCM email address
	Gui, Add, Text,, Primary browser process
	Gui, Add, Text, w200 0x10  ;Horizontal Line > Etched Gray
	Gui, Add, Text,, Local path to INCA (Advanced setting)
	Gui, Add, Text,, INCA process name (Advanced setting)
	Gui, Add, Text,, Outlook process name (Advanced setting)
	Gui, Add, Text,, Callguide process name (Advanced setting)
	Gui, Add, Text,, Local path to Callguide (Advanced setting)
	Gui, Add, Edit, ym w350 vFirstName, %FirstName%
	Gui, Add, Edit, w350 vLastName, %LastName%
	Gui, Add, Edit, w350 vTeliaID, %TeliaID%
	Gui, Add, Edit, w350 vPersonalCompanyEmail, %PersonalCompanyEmail%
	Gui, Add, Edit, w350 vPersonalCompanyPhone, %PersonalCompanyPhone%
	Gui, Add, Edit, Password w350 vINCApassword, %INCApassword%
	Gui, Add, Edit, w350 vCarrierCSCemail, %CarrierCSCemail%
	Gui, Add, Edit, w350 vNCMemail, %NCMemail%
	Gui, Add, ComboBox, w350 vPrimaryBrowserProcess, chrome.exe|firefox.exe|iexplore.exe|microsoftedge.exe|%PrimaryBrowserProcess%||
	Gui, Add, Text, w350 0x10  ;Horizontal Line > Etched Gray
	Gui, Add, Edit, w350 vINCApath, %INCApath%
	Gui, Add, Edit, w350 vINCAprocess, %INCAprocess%
	Gui, Add, Edit, w350 vOutlookProcess, %OutlookProcess%
	Gui, Add, Edit, w350 vCallguideProcess, %CallguideProcess%
	Gui, Add, Edit, w350 vCallguidePath, %CallguidePath%

	Gui, Add, Button, Default, Apply
	Gui, Add, Button,, Cancel
	Gui, Show
	Return

	ButtonApply:
	saveSettings(settingsfile)
	Gui, Submit
	Return

	ButtonCancel:
	Gui, Cancel
	Return
}


loadSettings(settingsfile){
	global
	IniRead, FirstName, %settingsfile%, standard, FirstName
	IniRead, LastName, %settingsfile%, standard, LastName
	IniRead, TeliaID, %settingsfile%, standard, TeliaID
	IniRead, PersonalCompanyEmail, %settingsfile%, standard, PersonalCompanyEmail
	IniRead, PersonalCompanyPhone, %settingsfile%, standard, PersonalCompanyPhone
	IniRead, INCApassword, %settingsfile%, standard, INCApassword
	IniRead, PrimaryBrowserProcess, %settingsfile%, standard, PrimaryBrowserProcess
	IniRead, CarrierCSCemail, %settingsfile%, standard, CarrierCSCemail
	IniRead, NCMemail, %settingsfile%, standard, NCMemail
	IniRead, INCApath, %settingsfile%, advanced, INCApath
	IniRead, INCAprocess, %settingsfile%, advanced, INCAprocess
	IniRead, OutlookProcess, %settingsfile%, advanced, OutlookProcess
	IniRead, CallguideProcess, %settingsfile%, advanced, CallguideProcess
	IniRead, CallguidePath, %settingsfile%, advanced, CallguidePath
	TrayTip, CSC Helper Scripts, User settings loaded., 6, 1
	return
}

saveSettings(settingsfile){
	global
	IniWrite, %FirstName%, %settingsfile%, standard, FirstName
	IniWrite, %LastName%, %settingsfile%, standard, LastName
	IniWrite, %TeliaID%, %settingsfile%, standard, TeliaID
	IniWrite, %PersonalCompanyEmail%, %settingsfile%, standard, PersonalCompanyEmail
	IniWrite, %PersonalCompanyPhone%, %settingsfile%, standard, PersonalCompanyPhone
	IniWrite, %INCApassword%, %settingsfile%, standard, INCApassword
	IniWrite, %PrimaryBrowserProcess%, %settingsfile%, standard, PrimaryBrowserProcess
	IniWrite, %CarrierCSCemail%, %settingsfile%, standard, CarrierCSCemail
	IniWrite, %NCMemail%, %settingsfile%, standard, NCMemail
	IniWrite, %INCApath%, %settingsfile%, advanced, INCApath
	IniWrite, %INCAprocess%, %settingsfile%, advanced, INCAprocess
	IniWrite, %OutlookProcess%, %settingsfile%, advanced, OutlookProcess
	IniWrite, %CallguideProcess%, %settingsfile%, advanced, CallguideProcess
	IniWrite, %CallguidePath%, %settingsfile%, advanced, CallguidePath
	MsgBox,,CSC Helper Scripts, User settings saved
	Return
}

help(){
	Msgbox, Help is not available right now. Please try another time, maybe next year.
	return
}
and

Code: Select all

[standard]
FirstName=BBBBBBBB
variables are loaded correctly
are you sure about that? did you try open the .ini file and see if it was there? For me the .ini file is just empty...
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Why are my global variables not global?

16 Apr 2018, 14:49

no, i created my own .ini consisting of see my post above, since you hadnt provided a sample .ini or the function that was apparently supposed to generate it
morxy49
Posts: 21
Joined: 04 Aug 2016, 03:44

Re: Why are my global variables not global?

16 Apr 2018, 14:50

Ah! i finally made it work!!!

first of all, i had to make the variable settingsfile a global variable, which i wasn't able to do in one step, because that threw an error, so i did it like this:

Code: Select all

global settingsfile
settingsfile = %A_MyDocuments%\csc_helper-user_settings.ini
then i had to remove all the variables in the function calls, for example userSettings(settingsfile) should be userSettings(), otherwise it just operates locally.

that solved it!
morxy49
Posts: 21
Joined: 04 Aug 2016, 03:44

Re: Why are my global variables not global?

16 Apr 2018, 15:22

However, now i've encountered an even stranger issue...
i have to save my settings TWICE in order to write the changes to the .ini file.
The first time i save, it updates the variables locally, but doesn't update the .ini file until i save once more. wtf?

This is my current code (P.S, now i included all code, so you should be able to run it if you want. Press F12 to open menu.) :

Code: Select all

#SingleInstance force

;initial variables
global settingsfile
settingsfile = %A_MyDocuments%\csc_helper-user_settings.ini
paused := false
global FirstName
global LastName
global TeliaID
global PersonalCompanyEmail
global PersonalCompanyPhone
global CarrierCSCemail
global NCMemail
global PrimaryBrowserProcess
global OutlookProcess
global INCApassword
global INCApath
global INCAprocess
global CallguideProcess
global CallguidePath



checkForSettingsFile()

;************************************************************************************************************************ 
                                                   

checkForSettingsFile(){
	IfExist, %settingsfile% 
		loadSettings()
	else {
		MsgBox,,CSC Helper Scripts - Settings not found, No user settings found. Click OK to create and open settings file.
		FileAppend,, %settingsfile%
		sleep, 500
		setDefaultSettings()
		loadSettings()
		openUserSettings()
	}
	return
}

mainMenu(){
	global
	Gui, New,, Menu
	Gui, Add, Picture, x22 y10 w120 h120 , C:\Users\uly918\Desktop\CSC Helper Scripts\pebble.ico
	Gui, Add, Button, x12 y150 w140 h40 , User Settings
	Gui, Add, Button, x12 y200 w140 h40 , Help
	Gui, Add, Button, x12 y250 w140 h40 , Open Case Assigner
	Gui, Add, Button, x12 y300 w140 h40 , Pause CSC Helper Scripts
	Gui, Add, Button, x12 y350 w140 h40 , Exit CSC Helper Scripts
	Gui, Show
	Return

	GuiClose:
	Gui, Cancel
	return
	
	ButtonUserSettings:
	Gui, Cancel
	openUserSettings()
	return
	
	ButtonHelp:
	Gui, Cancel
	help()
	return
	
	ButtonOpenCaseAssigner:
	Gui, Cancel
	caseAssigner()
	return
	
	ButtonPauseCSCHelperScripts:
	paused := true
	suspend, on
	Gui, Cancel
	MsgBox,, CSC Helper Scripts, All scripts paused. Press F12 to resume.
	return
	
	ButtonExitCSCHelperScripts:
	ExitApp
	return
}

openUserSettings(){
	global
	Gui, New,, CSC Helper Scripts - User settings
	Gui, Add, Text,, Firstname
	Gui, Add, Text,, Lastname
	Gui, Add, Text,, Telia ID/Username
	Gui, Add, Text,, Personal email address (Telia)
	Gui, Add, Text,, Personal phone number (Telia)
	Gui, Add, Text,, INCA-password
	Gui, Add, Text,, CSC email address
	Gui, Add, Text,, NCM email address
	Gui, Add, Text,, Primary browser process
	Gui, Add, Text, w200 0x10  ;Horizontal Line > Etched Gray
	Gui, Add, Text,, Local path to INCA (Advanced setting)
	Gui, Add, Text,, INCA process name (Advanced setting)
	Gui, Add, Text,, Outlook process name (Advanced setting)
	Gui, Add, Text,, Callguide process name (Advanced setting)
	Gui, Add, Text,, Local path to Callguide (Advanced setting)
	Gui, Add, Edit, ym w350 vFirstName, %FirstName%
	Gui, Add, Edit, w350 vLastName, %LastName%
	Gui, Add, Edit, w350 vTeliaID, %TeliaID%
	Gui, Add, Edit, w350 vPersonalCompanyEmail, %PersonalCompanyEmail%
	Gui, Add, Edit, w350 vPersonalCompanyPhone, %PersonalCompanyPhone%
	Gui, Add, Edit, Password w350 vINCApassword, %INCApassword%
	Gui, Add, Edit, w350 vCarrierCSCemail, %CarrierCSCemail%
	Gui, Add, Edit, w350 vNCMemail, %NCMemail%
	Gui, Add, ComboBox, w350 vPrimaryBrowserProcess, chrome.exe|firefox.exe|iexplore.exe|microsoftedge.exe|%PrimaryBrowserProcess%||
	Gui, Add, Text, w350 0x10  ;Horizontal Line > Etched Gray
	Gui, Add, Edit, w350 vINCApath, %INCApath%
	Gui, Add, Edit, w350 vINCAprocess, %INCAprocess%
	Gui, Add, Edit, w350 vOutlookProcess, %OutlookProcess%
	Gui, Add, Edit, w350 vCallguideProcess, %CallguideProcess%
	Gui, Add, Edit, w350 vCallguidePath, %CallguidePath%
	
	Gui, Add, Button, Default, Apply
	Gui, Add, Button,, Cancel
	Gui, Show
	Return
	
	ButtonApply:
	saveSettings()
	Gui, Submit
	Return
	
	ButtonCancel:
	Gui, Cancel
	Return
}

setDefaultSettings(){
	global
	IniWrite, %FirstName%, %settingsfile%, standard, FirstName
	IniWrite, %LastName%, %settingsfile%, standard, LastName
	IniWrite, %TeliaID%, %settingsfile%, standard, TeliaID
	IniWrite, %PersonalCompanyEmail%, %settingsfile%, standard, PersonalCompanyEmail
	IniWrite, %PersonalCompanyPhone%, %settingsfile%, standard, PersonalCompanyPhone
	IniWrite, %INCApassword%, %settingsfile%, standard, INCApassword
	IniWrite, %PrimaryBrowserProcess%, %settingsfile%, standard, PrimaryBrowserProcess
	IniWrite, [email protected], %settingsfile%, standard, CarrierCSCemail
	IniWrite, [email protected], %settingsfile%, standard, NCMemail
	IniWrite, C:\ProgramData\Microsoft\Windows\Start Menu\Programs\INCA.lnk, %settingsfile%, advanced, INCApath
	IniWrite, Inca.exe, %settingsfile%, advanced, INCAprocess
	IniWrite, OUTLOOK.EXE, %settingsfile%, advanced, OutlookProcess
	IniWrite, CGAgent.exe, %settingsfile%, advanced, CallguideProcess
	IniWrite, C:\ProgramData\Microsoft\Windows\Start Menu\Programs\CallGuide\CallGuide Agent 9.0.1.lnk, %settingsfile%, advanced, CallguidePath
	return
}

loadSettings(){
	global
	IniRead, FirstName, %settingsfile%, standard, FirstName
	IniRead, LastName, %settingsfile%, standard, LastName
	IniRead, TeliaID, %settingsfile%, standard, TeliaID
	IniRead, PersonalCompanyEmail, %settingsfile%, standard, PersonalCompanyEmail
	IniRead, PersonalCompanyPhone, %settingsfile%, standard, PersonalCompanyPhone
	IniRead, INCApassword, %settingsfile%, standard, INCApassword
	IniRead, PrimaryBrowserProcess, %settingsfile%, standard, PrimaryBrowserProcess
	IniRead, CarrierCSCemail, %settingsfile%, standard, CarrierCSCemail
	IniRead, NCMemail, %settingsfile%, standard, NCMemail
	IniRead, INCApath, %settingsfile%, advanced, INCApath
	IniRead, INCAprocess, %settingsfile%, advanced, INCAprocess
	IniRead, OutlookProcess, %settingsfile%, advanced, OutlookProcess
	IniRead, CallguideProcess, %settingsfile%, advanced, CallguideProcess
	IniRead, CallguidePath, %settingsfile%, advanced, CallguidePath
	TrayTip, CSC Helper Scripts, User settings loaded., 6, 1
	return
}

saveSettings(){
	global
	IniWrite, %FirstName%, %settingsfile%, standard, FirstName
	IniWrite, %LastName%, %settingsfile%, standard, LastName
	IniWrite, %TeliaID%, %settingsfile%, standard, TeliaID
	IniWrite, %PersonalCompanyEmail%, %settingsfile%, standard, PersonalCompanyEmail
	IniWrite, %PersonalCompanyPhone%, %settingsfile%, standard, PersonalCompanyPhone
	IniWrite, %INCApassword%, %settingsfile%, standard, INCApassword
	IniWrite, %PrimaryBrowserProcess%, %settingsfile%, standard, PrimaryBrowserProcess
	IniWrite, %CarrierCSCemail%, %settingsfile%, standard, CarrierCSCemail
	IniWrite, %NCMemail%, %settingsfile%, standard, NCMemail
	IniWrite, %INCApath%, %settingsfile%, advanced, INCApath
	IniWrite, %INCAprocess%, %settingsfile%, advanced, INCAprocess
	IniWrite, %OutlookProcess%, %settingsfile%, advanced, OutlookProcess
	IniWrite, %CallguideProcess%, %settingsfile%, advanced, CallguideProcess
	IniWrite, %CallguidePath%, %settingsfile%, advanced, CallguidePath
	MsgBox,,CSC Helper Scripts, User settings saved
	Return
}

help(){
	Msgbox, Help is not available right now. Please try another time, maybe next year.
	return
}


caseAssigner(){
	MsgBox, This function is not completed yet. Come back later.
	return
}


;************************************************************************************************************************ 

;F12 un-pauses or opens menu
F12::
	suspend, off
	if paused{
		suspend, off
		paused := false
		TrayTip, CSC Helper Scripts, All scripts active., 4, 1
		return
	}
	else{
		mainMenu()
		return
	}
	return
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Why are my global variables not global?

16 Apr 2018, 15:46

So this might be more refactoring than you want to do, but since I've been using it successfully for a while now, here is the script I use for saving and loading user settings. It's nice because it's easy to add new things to be saved (just add them to the appropriate global variable), and it lets you split the ini file into sections.

Code: Select all

global UsersDataDir := A_WorkingDir . "\Data\Users\"	; Location of the shared users data directory
global Username	; This variable sets the name of the saved ini file

; Create a two-dimensional array for reading and writing the user config file
global HotkeyOptions := {BasicPromosEnabled : false
,AdvancedPromosEnabled : false
,PromoStyle : 1
,BasicSignsEnabled : false
,AdvancedSignsEnabled : false
,MiscHotkeysEnabled : false}

global OtherOptions := {PowerUser : false
,Passcrypt : ""
,PromoDefaultMargin : 6
,BarcodeAssist : false
,QuickLogin : false
,AutoCapslock : false
,SignDelay : 250}

global UserVars := {Hotkeys : HotkeyOptions	; The key names here will be used as sections in the ini file
,Other : OtherOptions}

SaveUserData()
{
	FileDelete, % UsersDataDir . UserName . ".ini"
	for section, array in UserVars
		for key, value in array
			IniWrite, % value, % UsersDataDir . UserName . ".ini", % section, % key
	return
}

LoadUserData()
{
	UserConfigFile := UsersDataDir . UserName . ".ini"
	
	IfExist, % UserConfigFile
	{
		Section := ""
		Key := ""
		Value := ""
		Loop, Read, % UserConfigFile
		{
			if (InStr(A_LoopReadLine, "["))
				Section := StrReplace(StrReplace(A_LoopReadLine, "["), "]")
			else if (Pos := InStr(A_LoopReadLine, "="))
			{
				Key := SubStr(A_LoopReadLine, 1, Pos-1)
				Value := SubStr(A_LoopReadLine, Pos+1)
				UserVars[Section,Key] := Value
			}
		}
	}
	return
}
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Why are my global variables not global?

16 Apr 2018, 16:17

Code: Select all

ButtonApply:
saveSettings()
Gui, Submit
return
gui submit after calling the save function

save is called with uninitialized/old data
morxy49
Posts: 21
Joined: 04 Aug 2016, 03:44

Re: Why are my global variables not global?

17 Apr 2018, 08:23

MaxAstro wrote:So this might be more refactoring than you want to do, but since I've been using it successfully for a while now, here is the script I use for saving and loading user settings. It's nice because it's easy to add new things to be saved (just add them to the appropriate global variable), and it lets you split the ini file into sections.

Code: Select all

global UsersDataDir := A_WorkingDir . "\Data\Users\"	; Location of the shared users data directory
global Username	; This variable sets the name of the saved ini file

; Create a two-dimensional array for reading and writing the user config file
global HotkeyOptions := {BasicPromosEnabled : false
,AdvancedPromosEnabled : false
,PromoStyle : 1
,BasicSignsEnabled : false
,AdvancedSignsEnabled : false
,MiscHotkeysEnabled : false}

global OtherOptions := {PowerUser : false
,Passcrypt : ""
,PromoDefaultMargin : 6
,BarcodeAssist : false
,QuickLogin : false
,AutoCapslock : false
,SignDelay : 250}

global UserVars := {Hotkeys : HotkeyOptions	; The key names here will be used as sections in the ini file
,Other : OtherOptions}

SaveUserData()
{
	FileDelete, % UsersDataDir . UserName . ".ini"
	for section, array in UserVars
		for key, value in array
			IniWrite, % value, % UsersDataDir . UserName . ".ini", % section, % key
	return
}

LoadUserData()
{
	UserConfigFile := UsersDataDir . UserName . ".ini"
	
	IfExist, % UserConfigFile
	{
		Section := ""
		Key := ""
		Value := ""
		Loop, Read, % UserConfigFile
		{
			if (InStr(A_LoopReadLine, "["))
				Section := StrReplace(StrReplace(A_LoopReadLine, "["), "]")
			else if (Pos := InStr(A_LoopReadLine, "="))
			{
				Key := SubStr(A_LoopReadLine, 1, Pos-1)
				Value := SubStr(A_LoopReadLine, Pos+1)
				UserVars[Section,Key] := Value
			}
		}
	}
	return
}
Ah, that looks nice!
For now i will keep using what i already built, but i might use this in the future if i make something else. Thanks! :)
morxy49
Posts: 21
Joined: 04 Aug 2016, 03:44

Re: Why are my global variables not global?

17 Apr 2018, 08:24

swagfag wrote:

Code: Select all

ButtonApply:
saveSettings()
Gui, Submit
return
gui submit after calling the save function

save is called with uninitialized/old data
Awesome! Thanks, i will try to submit before calling save.
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Why are my global variables not global?

17 Apr 2018, 13:48

If you are using objects to hold your data (ie somevar := { this: "that" }) and you want to store that data on disk, then splitting it up into individual vars and passing those to IniRead / IniWrite is needlessly labor-intensive.
Encode the objects to JSON, then dump that out to disk. Use Coco's JSON library to convert the object to JSON. eg:

Code: Select all

somevar := { this: "that" }
str := JSON.Dump(somevar)
str would now hold { "this": "that" } - a textual representation of the data and structure of the object.

You can either pass the JSON text to IniWrite, or just dump it to a file using FileAppend (I build the whole settings as one object, do a filedelete on the settings file, then just do a fileappend to write out the new data).
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Why are my global variables not global?

17 Apr 2018, 14:10

How does one pass a JSON to IniWrite? I actually switched from using JSON to using my current format because I wanted my settings file to be more human-readable than a JSON string.
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Why are my global variables not global?

17 Apr 2018, 15:22

BTW, the way it normally writes the JSON is not very readable as it's all on one line.
There's an option you can set to make it readable - see the "space" parameter of JSON.Dump.
Then it would look more like this:

Code: Select all

{
	"this": "that",
	"someNode": {
		"nodeProp": 1
	}
}
I am not sure how INIWrite would handle that though
Then again, my preconceptions may be wrong, dumping JSON into INIWrite keys may not work.
If not, maybe dumping JSON as a whole INI section might work?
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Why are my global variables not global?

17 Apr 2018, 16:14

Mm... yeah, dumping it as a key doesn't seem to work, and kinda the whole point is to be able to have multiple sections, so I think I'll stick with my nested for loop solution. :)
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Why are my global variables not global?

17 Apr 2018, 17:03

what?

Code: Select all

{
	"someNode1": {
		"nodeProp1": 1, 
		"nodeProp2": 2, 
		"nodeProp3": 3, 
		"nodeProp4": 4 
	},
	"someNode2": {
		"nodeProp1": 1, 
		"nodeProp2": 2, 
		"nodeProp3": 3, 
		"nodeProp4": 4 
	}, 
	"someNode3": {
		"nodeProp1": 1, 
		"nodeProp2": 2, 
		"nodeProp3": 3, 
		"nodeProp4": 4 
	}
}
is the exact same as having sections, with the added benefit of being able to nest them if need be
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Why are my global variables not global?

18 Apr 2018, 08:03

True, but using an ini file also has the added benefit that I can overwrite individual keys without having to overwrite the entire file. Both approaches have their advantages... I don't think I'm likely to need nested sections in my use case, so I'll stick with an ini file.
Ivan

Re: Why are my global variables not global?

26 Apr 2018, 17:31

MaxAstro wrote:True, but using an ini file also has the added benefit that I can overwrite individual keys without having to overwrite the entire file. Both approaches have their advantages... I don't think I'm likely to need nested sections in my use case, so I'll stick with an ini file.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: CrowexBR, doodles333, vysmaty and 248 guests