Using built-in variables in an INI file

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
XJDHDR
Posts: 15
Joined: 30 Nov 2014, 23:44

Using built-in variables in an INI file

26 Feb 2015, 14:34

Greetings

I have just noticed that if you put a built in variable into an INI file's key then use IniRead to read that key's value (in my case, %A_ScriptDir%), it gets treated as a literal string rather than a variable. Is it possible to put a variable into an INI file and have it get treated as a variable by any script that reads that key? If so, can anyone tell me how?

My script is the following:
Spoiler
The INI I'm using is:
Spoiler
The problem I'm having with this particular script is that when execution reaches "Run, %MOLocation%\nxmhandler.exe %1%", I get an error stating that AHK couldn't find the file located at "%A_ScriptDir%\Skyrim\nxmhandler.exe". That's how I know the built-in variable is being treated as a literal string; the error said "%A_ScriptDir%" rather than the path to the script I'm running.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Using built-in variables in an INI file

26 Feb 2015, 15:16

If you remove the spaces from the keys in your ini file you could do something like this:

Code: Select all

Gui, New, +AlwaysOnTop +Border -MaximizeBox -Resize -SysMenu, Game chooser
Gui, Add, Text, , Please select the game you are downloading the mod for
Gui, Add, Button, Default -Wrap gSkyrim, &Skyrim
Gui, Add, Button, -Wrap gFalloutNewVegas, Fallout &New Vegas
Gui, Add, Button, -Wrap gFallout3, Fallout &3
Gui, Add, Button, -Wrap gOblivion, &Oblivion 
Gui, Show, AutoSize Center, 
Return

Skyrim:
FalloutNewVegas:
Fallout3:
Oblivion:
Gui, Destroy
IniRead, MOLocation, %A_ScriptDir%\Mod Organizer install locations.ini, MO locations, %A_ThisLabel%, Nothing
If MOLocation = Nothing
{
    MsgBox, Error: MO install location could not be found. This is caused by either not placing "Mod Organizer install locations.ini" in the same folder as "Start Mod Organizer NMM downloader.exe" or by the INI file being damaged to the point of being unusable. Please fix the problem before trying another mod download.
    ExitApp
}
if (SubStr(MOLocation, 1, 13) = "%A_ScriptDir%") ; check if the first 13 characters are the literal text "%A_ScriptDir%"
	MOLocation := A_ScriptDir . SubStr(MOLocation, 14)
;Run, %MOLocation%\nxmhandler.exe %1%
MsgBox, %MOLocation%\nxmhandler.exe %1%
ExitApp

Code: Select all

[MO locations]
Skyrim=%A_ScriptDir%\Skyrim
FalloutNewVegas=%A_ScriptDir%\Fallout New Vegas
Fallout3=%A_ScriptDir%\Fallout 3
Oblivion=%A_ScriptDir%\Oblivion
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: Using built-in variables in an INI file

26 Feb 2015, 17:42

if kon's suggestion isn't an option, then you need a special treatment for the keys in the ini file, since there is no way for autohotkey to treat a part of string as a variable from a variable name, and since you can get the value of a variable using the value stored in another variable name like this:

Code: Select all

a := "the value of variable a called using the variable b, since b content is a"
b := "a"
c := %b%
MsgBox, %c%
and the fact that all built in variable starts by A_ (that helpful for our RegEx needle), and that has one limitation, your keys shouldn't have this form "%A_" "letters" "%"

Code: Select all

if RegExMatch(MOLocation, "%(?<_Value>A_[a-zA-Z]+)%", A_BuiltInVariable)
	MOLocation:=RegExReplace(MOLocation, A_BuiltInVariable, %A_BuiltInVariable_Value%)
XJDHDR
Posts: 15
Joined: 30 Nov 2014, 23:44

Re: Using built-in variables in an INI file

26 Feb 2015, 23:08

Thanks for the assistance.

I decided to go with Kon's suggestions, though MJs' solution would be very useful if I ever encounter a scenario where I have to account for more than one built-in variable being in an INI file.
User avatar
berban
Posts: 97
Joined: 14 Apr 2014, 03:20

Re: Using built-in variables in an INI file

27 Feb 2015, 22:56

I'm surprised no one suggested using Transform, , Deref. That will dereference any strings formatted like %this%, including the built-in variable %A_ScriptDir%.

I also have a function that incorporates that functionality here http://www.autohotkey.com/board/topic/6 ... -ini-file/

If you were to use it you'd call it like this:

Code: Select all

IniRead(A_ScriptDir "\Mod Organizer install locations.ini") ; reads all the keys in that file.
MsgBox, % FalloutNewVegas ; will show the path you want for Fallout New Vegas
MsgBox, % Fallout3 ; will show the path you want for Fallout 3
The data is stored in a variable named after the key. Sorry for the crummy documentation.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ht55cd3, OrangeCat, Panaku and 311 guests