It is currently April 26th, 2024, 10:15 pm

Writing a measure to a text file using Lua

Get help with creating, editing & fixing problems with skins
User avatar
boozlepuzzle
Posts: 9
Joined: August 22nd, 2021, 9:08 pm
Location: Uruguay

Writing a measure to a text file using Lua

Post by boozlepuzzle »

Hey everyone, I have a skin that displays the current Spotify/Youtube Music song title and artist on the desktop, I wanted to write it to a text file too so I can use it with Autohotkey

This is my skin:

Code: Select all

[Rainmeter]
Update=500

[Variables]
Color=255,255,255,255
Font=Product Sans
Player=
Web=

[MeasureLuaScript]
Measure=Script
ScriptFile="Script.lua"

[Title]
Meter=String
MeasureName=Measure#Player#Title
X=200
Y=75
W=1200
H=300
FontSize=16
FontFace=#Font#
AntiAlias=1
FontColor=#Color#
StringAlign=CenterCenter
InlineSetting=Shadow|2|2|3|35,35,35,110
DynamicVariables=1

[Artist]
Meter=String
MeasureName=Measure#Player#Artist
X=200
Y=110
W=800
H=200
FontSize=14
FontFace=#Font#
AntiAlias=1
FontColor=#Color#
StringAlign=CenterCenter
InlineSetting=Shadow|2|2|3|35,35,35,110
DynamicVariables=1

[MeasureSpotifyTitle]
Measure=NowPlaying
PlayerName=Spotify
PlayerType=Title
OnChangeAction=[!ShowFade][!SetVariable "Player" "Spotify"]
RegExpSubstitute=1
Substitute="(.*)\s-.*":"\1","(.*)\s\(.*":"\1"

[MeasureSpotifyArtist]
Measure=NowPlaying
PlayerName=Spotify
PlayerType=Artist

[MeasureWebTitle]
Measure=Plugin
Plugin=WebNowPlaying
PlayerType=Title
RegExpSubstitute=1
Substitute="(.*)\s-.*":"\1","(.*)\s\(.*":"\1"
OnChangeAction=[!CommandMeasure "MeasureLuaScript" "Update()"]

[MeasureWebArtist]
Measure=Plugin
Plugin=WebNowPlaying
PlayerType=Artist

[MeasureWebPlayer]
Measure=Plugin
Plugin=WebNowPlaying
PlayerType=Player
IfMatch=Youtube Music|Deezer
IfMatchAction=[!SetVariable "Web" "1"]
IfNotMatchAction=[!SetVariable "Web" "0"]

[MeasureWebState]
Measure=Plugin
Plugin=WebNowPlaying
PlayerType=State
IfCondition=(MeasureWebState=1) && (#Web# = 1)
IfTrueAction=[!ShowFade][!SetVariable "Player" "Web"]
IfCondition2=(MeasureWebState=2)
IfTrueAction2=[!HideFade]
DynamicVariables=1
and this is the Lua file:

Code: Select all

function Update()
    FilePath = 'C:\Users\MyName\Documents\Rainmeter\Skins\Music Visualizer\Song.txt'
    MeasureArtist = SKIN:GetMeasure('MeasureWebArtist')
    Artist = MeasureArtist:GetStringValue()
    MeasureTitle = SKIN:GetMeasure('MeasureWebTitle')
    Title = MeasureTitle:GetStringValue()
  
    OutputFile = io.open(FilePath, "a")
    OutputFile:write(Artist .. ' - ' .. Title .. '\n')
    OutputFile:close()
end
by now I just want it to function with WebNowPlaying, but I must be doing something wrong because the text file remains blank, if you can spot the error or point me in the right direction, I'd be more than grateful, thanks!
User avatar
death.crafter
Rainmeter Sage
Posts: 1399
Joined: April 24th, 2021, 8:13 pm

Re: Writing a measure to a text file using Lua

Post by death.crafter »

boozlepuzzle wrote: September 22nd, 2021, 1:14 am Hey everyone, I have a skin that displays the current Spotify/Youtube Music song title and artist on the desktop, I wanted to write it to a text file too so I can use it with Autohotkey

This is my skin:

Code: Select all

[Rainmeter]
Update=500

[Variables]
Color=255,255,255,255
Font=Product Sans
Player=
Web=

[MeasureLuaScript]
Measure=Script
ScriptFile="Script.lua"

[Title]
Meter=String
MeasureName=Measure#Player#Title
X=200
Y=75
W=1200
H=300
FontSize=16
FontFace=#Font#
AntiAlias=1
FontColor=#Color#
StringAlign=CenterCenter
InlineSetting=Shadow|2|2|3|35,35,35,110
DynamicVariables=1

[Artist]
Meter=String
MeasureName=Measure#Player#Artist
X=200
Y=110
W=800
H=200
FontSize=14
FontFace=#Font#
AntiAlias=1
FontColor=#Color#
StringAlign=CenterCenter
InlineSetting=Shadow|2|2|3|35,35,35,110
DynamicVariables=1

[MeasureSpotifyTitle]
Measure=NowPlaying
PlayerName=Spotify
PlayerType=Title
OnChangeAction=[!ShowFade][!SetVariable "Player" "Spotify"]
RegExpSubstitute=1
Substitute="(.*)\s-.*":"\1","(.*)\s\(.*":"\1"

[MeasureSpotifyArtist]
Measure=NowPlaying
PlayerName=Spotify
PlayerType=Artist

[MeasureWebTitle]
Measure=Plugin
Plugin=WebNowPlaying
PlayerType=Title
RegExpSubstitute=1
Substitute="(.*)\s-.*":"\1","(.*)\s\(.*":"\1"
OnChangeAction=[!CommandMeasure "MeasureLuaScript" "Update()"]

[MeasureWebArtist]
Measure=Plugin
Plugin=WebNowPlaying
PlayerType=Artist

[MeasureWebPlayer]
Measure=Plugin
Plugin=WebNowPlaying
PlayerType=Player
IfMatch=Youtube Music|Deezer
IfMatchAction=[!SetVariable "Web" "1"]
IfNotMatchAction=[!SetVariable "Web" "0"]

[MeasureWebState]
Measure=Plugin
Plugin=WebNowPlaying
PlayerType=State
IfCondition=(MeasureWebState=1) && (#Web# = 1)
IfTrueAction=[!ShowFade][!SetVariable "Player" "Web"]
IfCondition2=(MeasureWebState=2)
IfTrueAction2=[!HideFade]
DynamicVariables=1
and this is the Lua file:

Code: Select all

function Update()
    FilePath = 'C:\Users\MyName\Documents\Rainmeter\Skins\Music Visualizer\Song.txt'
    MeasureArtist = SKIN:GetMeasure('MeasureWebArtist')
    Artist = MeasureArtist:GetStringValue()
    MeasureTitle = SKIN:GetMeasure('MeasureWebTitle')
    Title = MeasureTitle:GetStringValue()
  
    OutputFile = io.open(FilePath, "a")
    OutputFile:write(Artist .. ' - ' .. Title .. '\n')
    OutputFile:close()
end
by now I just want it to function with WebNowPlaying, but I must be doing something wrong because the text file remains blank, if you can spot the error or point me in the right direction, I'd be more than grateful, thanks!
Using the Update fun to do this is not recommended.

Use a custom function instead, since you are using OnChangeAction it will be written correctly.

I don't see why it wouldn't work tho. May be try printing the values to see if the values are correct.
from the Realm of Death
User avatar
balala
Rainmeter Sage
Posts: 16174
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Writing a measure to a text file using Lua

Post by balala »

boozlepuzzle wrote: September 22nd, 2021, 1:14 am Hey everyone, I have a skin that displays the current Spotify/Youtube Music song title and artist on the desktop, I wanted to write it to a text file too so I can use it with Autohotkey
I'm not using the WebNowPlaying plugin, so am not sure about the question, however there is something you should take care of. As death.crafter said in his reply, instead of using the Update() function in the lua script to write the needed information, would be a much better idea to use an Inlue Lua function to do the same thing. The reason why am I recommending such a thing is that if you're writing the title and the artist as had been in your code, you get the written on each update cycle, so practically twice a second (due to the Update=500 option of the [Rainmeter] section). If on the other hand you'd switch an inline lua function, you'd get the same operation only when you're applying the function.
If you are interested, here is how this should be done:
Replace the code of the Script.lua script file with the following one:

Code: Select all

function WriteToFile(File, Text1, Text2)
	OutputFile = io.open(File, "a")
	OutputFile:write(Text1 .. ' - ' .. Text2 .. '\n')
	OutputFile:close()
end
Note that here are neither Update(), nor Initialize() functions. Only a WriteToFile function, with three parameters (the path and name of the file to write to, and the two strings to be written).
Now, when you want to call this function to write the required information to the file, you have to call it in the skin. You can do this for instance by replacing the existing OnChangeAction option of the [MeasureSpotifyTitle] measure with the following one: OnChangeAction=[&MeasureLuaScript:WriteToFile("C:\Users\MyName\Documents\Rainmeter\Skins\Music Visualizer\Song.txt","[MeasureSpotifyArtist]","[MeasureSpotifyTitle]")]. The previously existing OnChangeAction seems a little bit useless, that's why I recommended you to replace it. If it has to stay, just add the new bang posted above, to the existing OnChangeAction option, besides the existing !CommandMeasure bang.
And one more besides the above things: I definitely wouldn't use the whole path (C:\Users\MyName\Documents\Rainmeter\Skins\Music Visualizer\Song.txt) of the file to write to it. Instead you should use something like this: #CURRENTPATH#Song.txt. But even better I'd move the Song.txt file to the @Resources folder (a much better place for it) and would use the #@#Song.txt path.

Again this reply doesn't solve the question of using the WebNowPlaying plugin, what I currently not using, have not installed. I'd try to remove the above OnchangeAction (from the [MeasureSpotifyTitle] measure) and would try to add the following one to the [MeasureWebTitle] measure: OnChangeAction=[&MeasureLuaScript:WriteToFile("C:\Users\MyName\Documents\Rainmeter\Skins\Music Visualizer\Song.txt","[MeasureWebArtist]","[MeasureWebTitle]")]. However this has not been tested, because as said I have not installed the WebNowPlaying plugin, so am not sure if it does work.
User avatar
death.crafter
Rainmeter Sage
Posts: 1399
Joined: April 24th, 2021, 8:13 pm

Re: Writing a measure to a text file using Lua

Post by death.crafter »

balala wrote: September 22nd, 2021, 2:52 pm OnChangeAction=[&MeasureLuaScript:WriteToFile("C:\Users\MyName\Documents\Rainmeter\Skins\Music Visualizer\Song.txt","[MeasureSpotifyArtist]","[MeasureSpotifyTitle]")]
I don't think this I'd th right way to do it.

Code: Select all

OnChangeAction=[!CommandMeasure MeasureLuaScript "WriteToFile([[C:\Users\MyName\Documents\Rainmeter\Skins\Music Visualizer\Song.txt]], [[[&MeasureSpotifyArtist]]], [[[&MeasureSpotifyTitle]]])"]
Why because the inline function would return something and OnChangeAction would try to execute what is returned.

It works because bangs are parsed before calling and hence the function is executed but I think it's not right to be used like this.
from the Realm of Death
User avatar
balala
Rainmeter Sage
Posts: 16174
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Writing a measure to a text file using Lua

Post by balala »

death.crafter wrote: September 22nd, 2021, 3:59 pm I don't think this I'd th right way to do it.

Code: Select all

OnChangeAction=[!CommandMeasure MeasureLuaScript "WriteToFile([[C:\Users\MyName\Documents\Rainmeter\Skins\Music Visualizer\Song.txt]], [[[&MeasureSpotifyArtist]]], [[[&MeasureSpotifyTitle]]])"]
Why because the inline function would return something and OnChangeAction would try to execute what is returned.

It works because bangs are parsed before calling and hence the function is executed but I think it's not right to be used like this.
Have you tried your solution? Because I doubt it does work. For me it definitely doesn't. Does it for you?
User avatar
death.crafter
Rainmeter Sage
Posts: 1399
Joined: April 24th, 2021, 8:13 pm

Re: Writing a measure to a text file using Lua

Post by death.crafter »

balala wrote: September 22nd, 2021, 5:22 pm Have you tried your solution? Because I doubt it does work. For me it definitely doesn't. Does it for you?
Ofc I can't test it but if you are asking about the method then yes, very much.

Can't really cook up an example now but I use it here:
https://github.com/deathcrafter/ColorPickerUI#activation
from the Realm of Death
User avatar
balala
Rainmeter Sage
Posts: 16174
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Writing a measure to a text file using Lua

Post by balala »

death.crafter wrote: September 22nd, 2021, 6:42 pm Can't really cook up an example now but I use it here:
https://github.com/deathcrafter/ColorPickerUI#activation
Not very sure what this has to do with the above question, but when tried out your suggestion couldn't get it to work.
Also not very sure what does mean the following:
death.crafter wrote: September 22nd, 2021, 6:42 pm Ofc I can't test it
Now have you tested the proposed method or not? "Ofc" means in my view that you did, but "can't test it" means you didn't. :confused: