It is currently October 3rd, 2024, 11:11 pm

I despair to find the error in LUA

Discuss the use of Lua in Script measures.
User avatar
⦿⽘⦿
Posts: 5
Joined: September 21st, 2024, 9:35 pm

I despair to find the error in LUA

Post by ⦿⽘⦿ »

Hello Guys,
Rainmeter gives "Script: File not valid" for the following LUA Script and I can't find an error.

The script should read a text file line by line in which data records are created line by line, similar to a CSV file, except that the separator is a "~" (see at the end).
When a skin element is clicked, the variable cng_Value is set with the new value and cng_Field with the field to be changed like "z2_s5" (z=Zeile/Row,s=Spalte/Column --> Row 2 - Column 5). The script should replace cng_Value in the row/column with the value of cng_Field (row/column). Afterwards, all data records in the array are written back to the file, including the changed data.
The skin should issue daily reminders for certain events.

Could someone find the error please?

alt_line.lua:

Code: Select all

local cng_Value = SKIN:GetVariable('cng_Value')
local z_s_cng= SKIN:GetVariable('cng_Field')
local z_cng = tonumber(string.match(z_s_cng, "z(.-)_"))
local s_cng = tonumber(string.match(z_s_cng, "s(.*)$"))

-- File path
local file = "dayly_todo.txt"
local base_dir = SKIN:GetVariable('path')
local file_path = base_dir .. file

-- Open file in read-write mode
local read_file = io.open(file_path, "w")
-- reads file in an array
local zeilen = {}
if read_file then
    for line in read_file:lines() do  -- Read file line by line and store the lines in an array
        local einzelwerte = {}
        for wert in line:gmatch("[^~]+") do
            table.insert(einzelwerte, wert)  -- Insert the individual values ​​into the inner array
        end
        table.insert(zeilen, einzelwerte)  -- Insert the array of single values ​​into the main array
    end
end

--Initialize data variable
local data = ""

-- write the lines including changed data into the variable data
for z, einzelwerte in ipairs(zeilen) do
    for s, wert in ipairs(einzelwerte) do
        if s == s_cng and z == z_cng then 
            data = data .. "~" .. cng_Value  -- writes the changed value it in the line
        else
            data = data .. "~" .. wert  --writes the previous value into the line
        end
    end
    data = string.sub(data, 1, -2)  -- Removes the last character ~
    data = data .. "\n" -- adds a line break at the end of the line
end

if write_file then
    write_file:write(data)  -- Write the variable data into the file
end
write_file:close() -- Close file
SKIN:Bang('!Refresh')
dayly_todo.txt:

Code: Select all

ASS 100~1~1~1~1~1~1~1~6:30
Rosuvastatin~1~1~1~1~1~1~1~6:30
Pantoprazol (1h v Essen)~1~1~1~1~1~1~1~17:00
Vit. D+K~0~0~0~0~0~0~1~10:00
Last edited by ⦿⽘⦿ on September 21st, 2024, 11:27 pm, edited 4 times in total.
User avatar
Yincognito
Rainmeter Sage
Posts: 8324
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: I despair to find the error in LUA

Post by Yincognito »

⦿⽘⦿ wrote: September 21st, 2024, 9:59 pmCould someone find the error please?
Not sure how much it helps, but Lua provides the line number and some details on where and why the error occurs. Without running anything from your script or trying to understand what it does, the "File not valid" error disappeared when including the whole contents of the file into the standard Initialize() function, e.g.:

Code: Select all

function Initialize()
...the contents here...
end
Now, I'm pretty sure that you don't want the whole thing in that function, so distributing things where they should be (or creating functions for various parts of your code) is up to you and the purpose of each part. Obviously, the script throws other errors too, since it doesn't have the skin's .ini with all the variables and such, but at least the error you mentioned seems to have gone away for me after the mentioned change.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
⦿⽘⦿
Posts: 5
Joined: September 21st, 2024, 9:35 pm

Re: I despair to find the error in LUA

Post by ⦿⽘⦿ »

Thank you very much for your answer.
The variables cng_Value and z_s_cng (changed the var name) exist from the beginning with senseless data in the ini file and will be changed on the click event to the necessary values.
I put the whole LUA Skript in the function Initialize but the error still appears but it is executed with these senseless data and delete all values in the db. I try researching furthermore
Last edited by ⦿⽘⦿ on September 21st, 2024, 11:53 pm, edited 1 time in total.
User avatar
Yincognito
Rainmeter Sage
Posts: 8324
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: I despair to find the error in LUA

Post by Yincognito »

⦿⽘⦿ wrote: September 21st, 2024, 11:37 pmDo I have may put the variables as "User defined options" into the Script measure or could LUA read all variables of the ini?
You can do either way, it depends on what you're most comfortable with and the particularities of your scenario:
https://docs.rainmeter.net/manual/lua-scripting/#Options
https://docs.rainmeter.net/manual/lua-scripting/#GetVariable
https://docs.rainmeter.net/manual/lua-scripting/#ReplaceVariables
https://docs.rainmeter.net/manual/lua-scripting/#ParseFormula
The last 3 links are all basically different ways of getting variables from your skin, depending on their complexity.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
⦿⽘⦿
Posts: 5
Joined: September 21st, 2024, 9:35 pm

Re: I despair to find the error in LUA

Post by ⦿⽘⦿ »

Thank you very much, I have studied these pages thoroughly and have also created complex skins.

By the way: I compile my skins with PHP in loops, because many skins have repeated meters and measures that only change in details. Are there others who do something like this, perhaps with other programming languages?
User avatar
Yincognito
Rainmeter Sage
Posts: 8324
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: I despair to find the error in LUA

Post by Yincognito »

⦿⽘⦿ wrote: September 21st, 2024, 11:58 pmBy the way: I compile my skins with PHP in loops, because many skins have repeated meters and measures that only change in details. Are there others who do something like this, perhaps with other programming languages?
Frankly, this is the first time I hear about a skin designer having to 'compile' skins, or do that in PHP, so I can't say that I know others who might do similar things. But then, if it works and is comfortable for you, there's obviously nothing wrong with it. The one thing I know that's close to what it seems you do are the "factory" scripts made in Lua, to create and mass write a greater number of repetitive measures, based on some adjustable 'parameters' embedded in a 'template' syntax. Not sure if that's what you meant by 'compiling', but I guess that could be done in any programming language via a couple of I/O calls (except that Lua has the advantage of specific objects and functions designed to interact with Rainmeter).
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
⦿⽘⦿
Posts: 5
Joined: September 21st, 2024, 9:35 pm

Re: I despair to find the error in LUA

Post by ⦿⽘⦿ »

last post on the subject, I don't want to take up too much of your time.
If you're interested, I can post my php script here. If not, that's it for now
User avatar
ikarus1969
Posts: 591
Joined: February 28th, 2011, 3:20 pm
Location: Vienna, Austria

Re: I despair to find the error in LUA

Post by ikarus1969 »

One question: the posted lua-code is within a function that is called from your skin? Or how do you call this lua-code?
Would you eventually post a little test skin, so we can see, how you are calling lua?

edit: the first part of your lua should read the current content of the file, right? Why dou you open it with mode "w"? (local read_file = io.open(file_path, "w"))

another question: i can't find a "close" after you've read the content; 2nd: i can't find a declaration of the "write"-variables in your lua code.

Are you sure you've posted the complete lua code?

regarding https://www.lua.org/manual/5.1/manual.html#5.7 there are various modes to open a file for in lua - can you check this please?
User avatar
⦿⽘⦿
Posts: 5
Joined: September 21st, 2024, 9:35 pm

Re: I despair to find the error in LUA

Post by ⦿⽘⦿ »

Thanks Ikarus, thanks Yincognito for your help,
in the end I got it with the following code.
alt_db.lua

Code: Select all


function alt_line(r_cng,c_cng,value_cng) --function called from ini
-- r_cng = row to change
-- c_cng = column to change
-- value_cng value to change

-- File path
local file = "dayly_todo.txt"
local base_dir = SKIN:GetVariable('path')
local file_path = base_dir .. file

-- Open file in read-mode
local read_file = io.open(file_path, "r")

-- ################ reads file in an array

local data_array = {}
if read_file then
    for line in read_file:lines() do  -- Read file line by line and store the lines in an array
        local column = {}
        for value in line:gmatch("[^~]+") do
            table.insert(column, value)  -- Insert the individual values ​​into the inner array
        end
        table.insert(data_array, column)  -- Insert the array of single values ​​into the main array
    end
end
read_file:close() -- Close file

-- ################ open file in empty-mode and write changes 
local write_file = io.open(file_path, "w")

-- write the file  including changed value_cng 
for r, column in ipairs(data_array) do
--Initialize data_row variable
	local data_row = ""
    for s, value in ipairs(column) do
        if s == c_cng and r == r_cng then 
            data_row = data_row .. "~" .. value_cng  -- writes the value to be changed in the line
        else
            data_row = data_row .. "~" .. value  -- writes the is-value into the line
        end
    end
    data_row = string.sub(data_row, 2)	-- deletes leading ~
    data_row = data_row .. "\n" 		-- adds a line break at the end of the line
    write_file:write(data_row)  		-- Writes the variable data_row into the file
end

write_file:close() -- Close file

SKIN:Bang('!Refresh')

end -- end function alt_line()
Excerpts from dayly_todo.ini

Code: Select all

...
[calc_invert_bin]
Measure=Calc
Formula=abs(#cng_data#-1)
dynamicVariables=1
...
[measure_z1_s2]
Measure=WebParser
URL=[measure_parse_line_1]
StringIndex=2
Group=1
Disabled=1
dynamicVariables=1
IfEqualValue=1
IfEqualAction=[!SetOption meter_z1_s2 SolidColor "#Color#"][!UpdateMeter "meter_z1_s2"]
...
[meter_z1_s2]
Meter=String
MeterStyle=Style_text | wd
Text=Mo
Group=1
Hidden=1
dynamicVariables=1
W=(#part_len#*1.5)
Y=r
X=(#margin#+#part_len#*0.75)R
LeftMouseDoubleClickAction=[!SetVariable cng_data "[measure_z1_s2]"][!Update][!CommandMeasure "command_alt_db" "alt_line(1,2,[calc_invert_bin])"]
https://forum.rainmeter.net/download/file.php?mode=view&id=31781
The LUA script MUST be placed in a function, even if it is not Initialize() as Yincognito suggested. This is how the call worked. I had to specify this function "alt_line()" when calling Measure=Script. In Rainmeter-LUA, functions can be given parameters (like in all other programming languages), which I entered with the necessary values. These parameter-values can easy be used in LUA.

Code: Select all

[!CommandMeasure "command_alt_db" "alt_line(1,2,[calc_invert_bin])"]
Thanks again - your suggestions have helped me a lot.
- close -
You do not have the required permissions to view the files attached to this post.
Last edited by ⦿⽘⦿ on September 22nd, 2024, 12:19 pm, edited 3 times in total.
User avatar
Yincognito
Rainmeter Sage
Posts: 8324
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: I despair to find the error in LUA

Post by Yincognito »

⦿⽘⦿ wrote: September 22nd, 2024, 11:25 am Thanks again - your suggestions have helped me a lot.
You're welcome, we're glad that they did - nice work! :thumbup:

P.S. Sorry for the delay in responding, been busy with my own Lua scripts in the meantime. :D
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth