It is currently March 28th, 2024, 10:02 pm

Write to a file ?

Discuss the use of Lua in Script measures.
User avatar
AlC
Posts: 329
Joined: June 9th, 2011, 6:46 pm

Write to a file ?

Post by AlC »

Hey Guys,

I'm working on a project at the moment, that contains smurfiers LuaCalendar and the ability to write notes.
Similar like this. The Input measure works and I get all the variables like day, month and year. Now I must write these variables to the new Notes.hol file. And here is my problem :)

This is my current lua script.

Code: Select all

function Initialize()
end

function Update()

--Retrieve Measure Settings
Month=SELF:GetOption('Month','');
Year=SELF:GetOption('Year','');
UserInput=SELF:GetOption('UserInput','');

MDay=SELF:GetOption('Day','');
DayMeter=SKIN:GetMeter(MDay)
Day=DayMeter:GetOption("Text")

local file = assert(io.input(SKIN:MakePathAbsolute('Notes.hol'), 'a'))
file:write('<Event Month="'..Month..'" Day="'..Day..'" Year="'..Year..'"> '..UserInput..' </Event>')

print(file)
file:close()

SKIN:Bang('!DisableMeasure LuaNotes')
	
--print('<Event Month="'..Month..'" Day="'..Day..'" Year="'..Year..'"> '..UserInput..' </Event>')
return 
end
For "print(file)" I get in About "file (72353068)"
If I put "print(file)" under "file:close()" I get "file (closed)

And my problem is that I'm not good at lua, though I read the lua io. examples ...

(At the moment the file Notes.hol is in the same folder like the ini/lua. But I wanna put the file then into a subfolder. But I don't understand SKIN:MakePathAbsolute ... But that's the 2.problem :) )

Hope someone can help me.
Rainmeter - You are only limited by your imagination and creativity.
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Write to a file ?

Post by smurfier »

The variable named file is a handle to the file, not the actual contents of the file. I'm guessing that's what that question was about.

Basically, SKIN:MakePathAbsolute() turns "Note.hol" into "#SKINSPATH##CURRENTCONFIG#Note.hol", only it creates the actual path instead of using the variables. If you give it an absolute path, it's just left alone.
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
User avatar
AlC
Posts: 329
Joined: June 9th, 2011, 6:46 pm

Re: Write to a file ?

Post by AlC »

Ehm, okay I clarify the problem. Sorry.

I don't need the content of the file (this is luckily done by your script) . I want write '<Event Month="'..Month..'" Day="'..Day..'" Year="'..Year..'"> '..UserInput..' </Event>' to Notes.hol file. But it doesn't work ( and I get no error message). And as far as I know I must "open" the file in lua -> write("text...") to the file -> "close" the file. And Notes.hol is still empty.

-------------------------------

I thought that. Okey little example: Notes.hol is in a subfolder, like LuaCalendar\Holidays\Notes.hol
-> #SKINSPATH##CURRENTCONFIG#Holidays\Notes.hol
in Lua ??
SKIN:MakePathAbsolute('Holidays\Notes.hol') Is that right ?
Rainmeter - You are only limited by your imagination and creativity.
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Write to a file ?

Post by smurfier »

For MakePathAbsolute, yes, that would be correct.

Try this code:

Code: Select all

function Initialize()
	DayMeter=SKIN:GetMeter(MDay)
end

function Update()

	--Retrieve Measure Settings
	Month=SELF:GetOption('Month','')
	Year=SELF:GetOption('Year','')
	UserInput=SELF:GetOption('UserInput','')
	MDay=SELF:GetOption('Day','')
	Day=DayMeter:GetOption("Text")

	local file=io.open(SKIN:MakePathAbsolute('Notes.hol'), 'a+')
	if file then
		local Yr=tonumber(Year) and ' Year="'..Year..'"' or ''
		io.write('\n','<Event Month="',Month,'" Day="',Day,'"',Yr,'>',UserInput,'</Event>')
	else
		print('Error: Invalid File Path.')
	end

	io.close(file)

	SKIN:Bang('!DisableMeasure LuaNotes')
   
	--print('<Event Month="'..Month..'" Day="'..Day..'" Year="'..Year..'"> '..UserInput..' </Event>')
	return
end
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
User avatar
AlC
Posts: 329
Joined: June 9th, 2011, 6:46 pm

Re: Write to a file ?

Post by AlC »

Okey this line works:
local file=io.input(SKIN:MakePathAbsolute('Notes.hol'), 'a+')

because I named Notes.hol to NotesT.hol and then i get an error message, renamed to Notes.hol back -> fine again
+ added print('good') under io.wrote(...) and get a "good".

I tried some variations like change 'a+' to 'w' or tried file:write() instead of io.write() -> but Notes.hol is still empty.

I have to sleep now, but after work I'm back.

EDIT:
It woooorks :)

local file=io.output(SKIN:MakePathAbsolute('Notes.hol'), 'w')

but I have 1 problem with the 'mode', because every mode I try it changes only the previous data and don't add it.
"r": read mode (the default);
"w": write mode;
"a": append mode;
"r+": update mode, all previous data is preserved;
"w+": update mode, all previous data is erased;
"a+": append update mode, previous data is preserved, writing is only allowed at the end of file.
I try it tommorrow. But a should do it or a+ ...
Rainmeter - You are only limited by your imagination and creativity.
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Write to a file ?

Post by smurfier »

Actually... we need to use io.open

local file=io.open(SKIN:MakePathAbsolute('Notes.hol'), 'a+')
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
User avatar
AlC
Posts: 329
Joined: June 9th, 2011, 6:46 pm

Re: Write to a file ?

Post by AlC »

Okay, I done it.

Code: Select all

local file=io.open(SKIN:MakePathAbsolute('Holidays/Notes.hol'), 'a')
   if file then
      local Yr=tonumber(Year) and ' Year="'..Year..'"' or ''
      io.output(file)
      file:write('\n','<Event Month="',Month,'" Day="',Day,'"',Yr,'>',UserInput,'</Event>')   
   else
      print('Error: Invalid File Path.')
   end
   
   io.close(file)
and the result is:
Notes.hol wrote: <Event Month="4" Day="29" Year="2012">ghjghjgh</Event>
<Event Month="4" Day="28" Year="2012">rtztrz</Event>
<Event Month="4" Day="31" Year="2012">erterter</Event>
<Event Month="4" Day="29" Year="2012">dfgdf</Event>
<Event Month="4" Day="31" Year="2012">opäopä</Event>
So I've done another step forward on this project. Thanks smurfier !
Rainmeter - You are only limited by your imagination and creativity.
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Write to a file ?

Post by smurfier »

Just so you know...

We don't need to use io.output if the file has already been opened, we also don't need to specify the file to write to if we're only opening one file.

Code: Select all

local file=io.open(SKIN:MakePathAbsolute('Holidays/Notes.hol'), 'a')
   if file then
      local Yr=tonumber(Year) and ' Year="'..Year..'"' or ''
      io.write('\n','<Event Month="',Month,'" Day="',Day,'"',Yr,'>',UserInput,'</Event>')   
   else
      print('Error: Invalid File Path.')
   end
   
   io.close(file)
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
User avatar
AlC
Posts: 329
Joined: June 9th, 2011, 6:46 pm

Re: Write to a file ?

Post by AlC »

smurfier wrote:We don't need to use io.output if the file has already been opened, we also don't need to specify the file to write to if we're only opening one file.
Yes, sounds logical. And I saw your edited post but I have no idea why it didn't work.

So this work, too.

Code: Select all

local file=io.open(SKIN:MakePathAbsolute('Holidays/Notes.hol'), 'a')
   if file then
      local Yr=tonumber(Year) and ' Year="'..Year..'"' or ''
      file:write('\n','<Event Month="',Month,'" Day="',Day,'"',Yr,'>',UserInput,'</Event>')   
   else
      print('Error: Invalid File Path.')
   end
   
   io.close(file)
And with
(line 20 ) io.write('\n','<Event Month="',Month,'" Day="',Day,'"',Yr,'>',UserInput,'</Event>')
I get the error message: "Script: Notes.lua:20: standard output file is closed" but with file:Write() it works.
Rainmeter - You are only limited by your imagination and creativity.
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Write to a file ?

Post by smurfier »

That's strange as I get no error. On the other hand, it's probably good practice to specify which file to write to.
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .