It is currently April 20th, 2024, 4:01 am

Lua Scripting - How to include variables.inc to script.lua

Discuss the use of Lua in Script measures.
User avatar
Kurou
Posts: 41
Joined: January 24th, 2022, 2:54 pm
Location: World Wide Web

Lua Scripting - How to include variables.inc to script.lua

Post by Kurou »

Hi, there. I have problem with importing variables.inc to script.lua. I want to read them and operate on variables which are inside variables.inc file but couldnt find anything on rainmeter lua docs or here on forum (on form becouse im too lazy to scroll this many pages :/). I've came up with idea to convert file variables.inc to format that lua can read without errors. With some help of ChatGPT becouse im too lazy (i just fixed some minor bugs from what ChatGPT gave me). It's designed to operate on files only from #@# and the variables values are converted into strings so in order to operate on some of them you would need to convert them to number or anything you need for.

If you want to tinker with what i was doing, here is the zip with the skin:
Lua_Test_Env.zip
In short this function converts this:

Code: Select all

[Variables]
color=255, 0, 0
background=20, 20, 20
font=250, 250, 250
font2=hello
test-test=2
To this:

Code: Select all

color="255,0,0"
background="20,20,20"
font="250,250,250"
font2="hello"
test-test="2"
On saving its doing the opposite.

I want to know if its good way to get those variables or is there some other very easy way of doing that. Let me know.

Function to convert *.inc file contents to table for lua to read:

Code: Select all

function ConvertVariablesList(file_path, output_variable)
    -- Check if output_variable exists and is a table
    if not output_variable or type(output_variable) ~= "table" then
        print("Lua Error | ConvertVariablesList("..file_path..") Output variable is not a valid table.")
        return
    end

    --  Open the selected file
    local file = io.open(SKIN:GetVariable('@')..file_path, "r")
    if not file then
        print("Lua Error | ConvertVariablesList("..file_path..") couldn't open the file!")
        return
    end

    --  Remove section indicator
    local sectionIndicator = file:read("*line")

    -- Convert rainmeter syntax into lua syntax
    local line = file:read("*line")
    while line do
        -- Extract variable and value
        local variable, value = line:match("^([^=]+)=(.+)$")
        if variable and value then
            value = value:gsub("%s*", "")  -- Remove whitespace
            output_variable[variable] = value
        else
            print("Lua Error | ConvertVariablesList("..file_path..") couldn't parse the variable '"..(variable or "").."'.")
            file:close()
            return
        end
        line = file:read("*line")
    end

    --  Close the file
    file:close()
end
Example usage:

Code: Select all

function Initialize()
    --  Set array for the variables
    styles = {}

    --  Collect data from the file
    ConvertVariablesList('variables\\styles.inc', styles)
    
    --	Example (color and background variables are from styles.inc file)
    print(styles["color"], styles["background"])
end
About Output

Code: Select all

255,0,0	20,20,20
Function to save table to *.inc file:

Code: Select all

function SaveVariablesList(file_path, input_variable)
     -- Check if input_variable is a table
    if type(input_variable) ~= "table" then
        print("Lua Error | SaveVariablesList("..file_path..") is not table type variable!")
        return
    end

    -- Open the file in write mode
    local file = io.open(SKIN:GetVariable('@')..file_path, "w")
    if not file then
        print("Lua Error | SaveVariablesList("..file_path..") couldn't open the file!")
        return
    end

    -- Write each variable and its value in Rainmeter syntax
    for variable, value in pairs(input_variable) do
        -- Handle errors during concatenation
        local success, line = pcall(function()
            return variable .. "=" .. value:gsub('"', '') .. "\n"
        end)

        if success then
            file:write(line)
        else
            print("Lua Error | SaveVariablesList("..file_path..") Error occurred while processing variable '"..variable.."'.")
        end
    end

    -- Close the file
    file:close()
end
The SaveVariableList function saves the variables but on every refresh, the order of the saved variables will vary.

Example usage:

Code: Select all

function change4()
    --	Specify the location and table which should be saved in *.inc file
    SaveVariablesList('variables\\styles.inc', styles)
end
You do not have the required permissions to view the files attached to this post.
Last edited by Kurou on May 22nd, 2023, 8:56 pm, edited 5 times in total.
Brought to you by: https://kurou.dev/
User avatar
Yincognito
Rainmeter Sage
Posts: 7128
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Lua Scripting - How to include variables.inc to script.lua

Post by Yincognito »

Kurou wrote: May 22nd, 2023, 6:07 pm Hi, there. I have problem with importing variables.inc to script.lua. I want to read them and operate on variables which are inside variables.inc file but couldnt find anything on rainmeter lua docs or here on forum (on form becouse im too lazy to scroll this many pages :/). I've came up with idea to convert file variables.inc to format that lua can read without errors. With some help of ChatGPT becouse im too lazy (i just fixed some minor bugs from what ChatGPT gave me). It's designed to operate on files only from #@#.

I want to know if its good way to get those variables or is there some other very easy way of doing that. Let me know.
ChatGPT already answered your question though: SKIN:GetVariable(). What you wanted - and what the code does - is create a list of them all.
Last edited by Yincognito on May 22nd, 2023, 6:59 pm, edited 1 time in total.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
balala
Rainmeter Sage
Posts: 16147
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Lua Scripting - How to include variables.inc to script.lua

Post by balala »

Kurou wrote: May 22nd, 2023, 6:07 pm I have problem with importing variables.inc to script.lua. I want to read them and operate on variables which are inside variables.inc file but couldnt find anything on rainmeter lua docs or here on forum (on form becouse im too lazy to scroll this many pages :/). I've came up with idea to convert file variables.inc to format that lua can read without errors.
You shouldn't import the variables directly into the lua script file, but into the skin, using a @Include=#@#Variables.inc (obviously the file name is just an example) option into the [Variables] section of your skin. If you do this, you can get the variables into the .lua script, using a GetVariable function (for instance with the following command in the .lua script file: MyVariable = tonumber(SKIN:GetVariable('MyVariableIntoSkin')).
An example: let's assume you have a MyVariable variable into the @Resources\Variables.inc file, which you want to get into the Script.lua file. You could do the followings:
  • Add the following option to the [Variables] section of your skin:

    Code: Select all

    [Variables]
    ...
    @Include=#@#Variables.inc
  • To get this variable into the script file, add the following command to the Initialize() function of the Script.lua script file:

    Code: Select all

    function Initialize()
    	MyVar = tonumber(SKIN:GetVariable('MyVariable'))
    end
    When you did this, you can use the MyVar variable into the Script.lua script file. This variable is taking the value of the MyVariable variable from the skin.
User avatar
Kurou
Posts: 41
Joined: January 24th, 2022, 2:54 pm
Location: World Wide Web

Re: Lua Scripting - How to include variables.inc to script.lua

Post by Kurou »

Right, i didnt understand that one. Thanks for info.

But regarding code that i have, isnt that a good way too if i want all variables from the file in lua? And why its bad to include variables directly in lua?

With SKIN:GetVariable could i do something to get very variable from the file? Maybe loop would work?
Brought to you by: https://kurou.dev/
User avatar
balala
Rainmeter Sage
Posts: 16147
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Lua Scripting - How to include variables.inc to script.lua

Post by balala »

Kurou wrote: May 22nd, 2023, 7:31 pm But regarding code that i have, isnt that a good way too if i want all variables from the file in lua? And why its bad to include variables directly in lua?
Makes things much more complicated, I think. Passing them through the skin is much more easier.
Kurou wrote: May 22nd, 2023, 7:31 pm With SKIN:GetVariable could i do something to get very variable from the file? Maybe loop would work?
Depends on the names of those variable. If they are named for instance as Var1, Var2, Var3 and so on, it might be easy. But if those variables are let's say FirstVar, SecondVar, ThirdVar and so on, the things get starting being much more complicated (however not impossible this way either). If there are lot of variables, might worth, but if there are only a few, definitely doesn't worth.
User avatar
Yincognito
Rainmeter Sage
Posts: 7128
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Lua Scripting - How to include variables.inc to script.lua

Post by Yincognito »

Kurou wrote: May 22nd, 2023, 7:31 pmBut regarding code that i have, isnt that a good way too if i want all variables from the file in lua?
[...]
With SKIN:GetVariable could i do something to get very variable from the file? Maybe loop would work?
If you actually wanted to have a string list of all the variables from the .inc file in Lua (though I can't imagine why you'd want that, since you can "pick" whatever variable you need individually and do your thing with it), then your code is a perfectly good way to do it. I don't think there's an (obvious) way to enumerate all variables in an .inc other than parsing the .inc file like you did (unless their names are similar, like balala mentioned).

By the way, since we're at it, this code does pretty much the same, whether it's an .ini or .inc file that is parsed - and it's not limited to a single section (in your case, [Variables]). Just in case it helps...
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
Kurou
Posts: 41
Joined: January 24th, 2022, 2:54 pm
Location: World Wide Web

Re: Lua Scripting - How to include variables.inc to script.lua

Post by Kurou »

So... if somebody wants for some reason like me a list of string of the variables from one file this code is way to go but in most cases the SKIN:GetVariable is enough to do the work.

Thanks for letting me know :).
Brought to you by: https://kurou.dev/
User avatar
balala
Rainmeter Sage
Posts: 16147
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Lua Scripting - How to include variables.inc to script.lua

Post by balala »

Kurou wrote: May 22nd, 2023, 7:54 pm but in most cases the SKIN:GetVariable is enough to do the work.
You have to apply a SKIN:GetVariable function to all variables, one by one. How those variables are named?
User avatar
Kurou
Posts: 41
Joined: January 24th, 2022, 2:54 pm
Location: World Wide Web

Re: Lua Scripting - How to include variables.inc to script.lua

Post by Kurou »

For now im just tinkering with lua so variable names are not that complicated for now but i guess in my skin which im planning to do the revamp becouse i use in it only rainmeter stuff without lua (the skin is slow).

My skin: Ashuramaru Bundle

In the new update, names of the variables will be more accurate to identify what they are for. So in short SKIN:GetVariable would be pain in the a** to write.

Here is the half of the variables from variables.inc:

Code: Select all

[Variables]

;------------
;	Global
;------------

MainLanguage=EN
IllustroFont=Bahnschrift
NotificationsContent=error
ErrorCode=101

;	Information about the bundle

Version=1.1.0
Build=1207221507
LicenseType=CC BY-NC-SA 4.0
Released=-

;	Multiple bangs combined into one variable

Refreshes=[!RefreshGroup "Ashuramaru-Bundle Group"][!RefreshGroup "Ashuramaru-Bundle Statistics"][!RefreshGroup "Ashuramaru-Bundle Clock"][!RefreshGroup "Ashuramaru-Bundle Top-Processes"][!RefreshGroup "Ashuramaru-Bundle CyberSearch"][!RefreshGroup "Ashuramaru-Bundle Dock"][!RefreshGroup "Ashuramaru-Bundle Dock-Single"][!RefreshGroup "Ashuramaru-Bundle-Visualizers-Group"][!RefreshGroup "Ashuramaru-Bundle Setup"][!RefreshGroup "Ashuramaru-Bundle Statistics-Detailed"][!RefreshGroup "ColorPicker"]

Deactivates=[!DeactivateConfigGroup "Ashuramaru-Bundle Group"][!DeactivateConfigGroup "Ashuramaru-Bundle Statistics"][!DeactivateConfigGroup "Ashuramaru-Bundle Clock"][!DeactivateConfigGroup "Ashuramaru-Bundle Top-Processes"][!DeactivateConfigGroup "Ashuramaru-Bundle CyberSearch"][!DeactivateConfigGroup "Ashuramaru-Bundle Dock"][!DeactivateConfigGroup "Ashuramaru-Bundle Dock-Single"][!DeactivateConfigGroup "Ashuramaru-Bundle-Visualizers-Group"][!DeactivateConfigGroup "Ashuramaru-Bundle Setup"][!DeactivateConfigGroup "Ashuramaru-Bundle Statistics-Detailed"]

;	Frost Glass Plugin

OpaqueBG=None

;------------
;	Panel
;------------

CurrentPage=home
PageColor=200,200,200
PageColor1=255,0,0
PageColor2=200,200,200
PageColor3=200,200,200
PageColor4=200,200,200
PageColor5=200,200,200
PageColor6=200,200,200
SidebarInfoState=1

State-Statistics-Combined=0
State-Statistics-Minimal=1
State-Statistics-Individual=1
State-Statistics-Detailed=1
State-Processes-Combined=1
State-Processes-Individual=1
State-Elegant-Clock=0
State-Translucent-Taskbar=1
State-TaskbarX=1
State-VectorCopy=1
State-Battery=1
State-Recycle-Bin=1
State-Hardware-Info=1
State-Dock=1
State-CyberSearch=1
State-Notes=1
State-Volume-Control=1
State-System-Control=1
State-Clipboard=1

PopUpsUninstall=1

;	Appearance

BackgroundImageToChange=Solid
Background-Image=#@#\Images\Backgrounds\#BackgroundImageToChange#.png
Background-Image-Blank=#@#\Images\Backgrounds\#BackgroundImageToChange#-Blank.png
Background-Image-Header=#@#\Images\Backgrounds\#BackgroundImageToChange#-Header.png
Background-Image-Header-Top=#@#\Images\Backgrounds\#BackgroundImageToChange#-Header-Top.png
Panel-Background=#@#\Images\Backgrounds\Panel-Background.png
Notyfications-Background=#@#\Images\Backgrounds\Panel-Background.png

PBSA=0
PBSB=0

BTOUA=0
BTO=1
BTOUA2=0
BTO2=1
BTOUA3=0
BTO3=1
BTOUA4=0
BTO4=1
BTOUA5=0
BTO5=1
BTOUA6=0
BTO6=1
BTOUA7=0
BTO7=1
BTOUA8=0
BTO8=1
BTOUA9=1
BTO9=0

ASIUA=0
ASI=1
ASIUA2=0
ASI2=1
ASIUA3=0
ASI3=1
ASIUA4=1
ASI4=0

;	ColorPicker

Color-To-Paint=ISenaryColor

;	Illustro Colors

IPrimaryColor=255,255,255
ISecondaryColor=255,255,255
ITertiaryColor=255,255,255
IQuaternaryColor=255,0,0
IQuinaryColor=255,0,0
ISenaryColor=255,0,0
ISeptenaryColor=255,255,255

;	VectorCopy Colors

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Copy

SVCTUA=1
SVCTA=0
SVCTUA2=0
SVCTA2=1

SVCCTUA=1
SVCCTA=0
SVCCTUA2=0
SVCCTA2=1

SVCSUA=0
SVCSA=1
SVCSUA2=1
SVCSA2=0
SVCSUA3=0
SVCSA3=1

SVCAUA1=0
SVCA1=1
SVCAUA2=0
SVCA2=1
SVCAUA3=0
SVCA3=1
SVCAUA4=0
SVCA4=1
SVCAUA5=0
SVCA5=1
SVCAUA6=0
SVCA6=1
SVCAUA7=0
SVCA7=1
SVCAUA8=0
SVCA8=1
SVCAUA9=1
SVCA9=0
Brought to you by: https://kurou.dev/
User avatar
Yincognito
Rainmeter Sage
Posts: 7128
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Lua Scripting - How to include variables.inc to script.lua

Post by Yincognito »

Kurou wrote: May 22nd, 2023, 7:54 pm So... if somebody wants for some reason like me a list of string of the variables from one file this code is way to go but in most cases the SKIN:GetVariable is enough to do the work.
Pretty much, yeah. Here are some snippets on that, along the same lines as your code. In the extreme (and probably scratching your left ear with your right hand type) case where you'd want to handle variables exclusively in Lua, you could just skip using .incs altogether and declare them in the Initialize() function. :confused:
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth