It is currently March 28th, 2024, 11:03 am

Validate if a file exist otherwise set a default file

Get help with creating, editing & fixing problems with skins
django933
Posts: 12
Joined: June 18th, 2014, 12:15 am

Validate if a file exist otherwise set a default file

Post by django933 »

Hi everyone , i need help to check if an image from a given path by a measure is there or not .And if is not there then show a default image. Currently the measure mAlbum takes the path of the song being played and adds "image.png" to complete the image path, then the meter is only showing when the image is found. How can i check and set a default option if is not found ??

For the album cover im using this too:

PlayerType=COVER
Substitute="":"Default.png"

Thanks!!

Code: Select all


[Rainmeter]
Update=500

[mPlayer]
Measure=Plugin
Plugin=NowPlaying.dll
PlayerName=Winamp
PlayerType=TITLE
PlayerPath=
DisableLeadingZero=0

[mAlbum]
Measure=Plugin
UpdateDivider=-1
Plugin=NowPlaying.dll
PlayerType=FILE
PlayerName=[mPlayer]
DynamicVariables=1
RegExpSubstitute=1
Substitute="(.*\\).*":"\1image.png"
OnChangeAction=[!UpdateMeter MeterImage]

[MeterImage]
DynamicVariables=1
Meter=Image
MeasureName=mAlbum
X=1372              
Y=302
W=500
H=500

User avatar
balala
Rainmeter Sage
Posts: 16109
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Validate if a file exist otherwise set a default file

Post by balala »

django933 wrote: October 31st, 2020, 7:20 pm Hi everyone , i need help to check if an image from a given path by a measure is there or not .And if is not there then show a default image. Currently the measure mAlbum takes the path of the song being played and adds "image.png" to complete the image path, then the meter is only showing when the image is found. How can i check and set a default option if is not found ??
The simplest (the only?) solution is to use a .lua script, to check the existence of the appropriate file. Here is what you have to do: first add the following measure to your code:

Code: Select all

[MeasureImageLua]
Measure=Script
ScriptFile=#@#MyLua.lua
Disabled=1
Note that:
  • The above code requires the MyLua.lua file into the @Resources folder. We gonna add it immediatelly, just note that if you want to modify the name or place of the script file, you have to modify the above measure accordingly.
  • This measure is from the beginning disabled. This is done because we gonna use Inline Lua scripting, described here, which lets you to work with the disabled measure.
Now create the above required @Resources\MyLua.lua file and add the following code:

Code: Select all

function file_exists(name)
	local f=io.open(name,"r")
	if f~=nil then
		io.close(f)
		return 1
	else
		return 0
	end
end
Now in the Rainmeter code we can use a section variable to check if the file exists or not. This variable will be [&MeasureImageLua:file_exists('PATH-OF-THE-FILE-TO-BE-CHECKED')]. Here:
  • MeasureImageLua is the name of the Script measure added to the code.
  • file_exists is the function used into the .lua script file.
  • PATH-OF-THE-FILE-TO-BE-CHECKED is the complete path of the file you want to check if does exist.
So add the following IfCondition to the [mAlbum] measure:

Code: Select all

[mAlbum]
...
IfCondition=([&MeasureImageLua:file_exists('#CURRENTSECTION#')]=0)
IfTrueAction=[!ShowMeter "MeterDefaultImage"][!HideMeter "MeterImage"][!Redraw]
IfFalseAction=[!HideMeter "MeterDefaultImage"][!ShowMeter "MeterImage"][!Redraw]
See that the above IfCondition checks if the file exists (through the [&MeasureImageLua:file_exists('#CURRENTSECTION#')] section variable) and if it does, the IfFalseAction is executed (and it's gonna show the [MeterDefaultImage] meter, supposing there is such a meter, and hide [MeterImage], which this time has nothing to show up), while if it doesn't, IfTrueAction is executed (contrary hiding the [MeterDefaultImage] meter and showing [MeterImage], which this time has to show up the image detected as exsting).
Obviously as usually, there are other solutions as well to show the default image. For instance you could modify with !SetOption bangs what the [MeterImage] meter does show. This is also usable, just have to modify appropriately the IfTrueAction and IfFalseAction options.
Please let me know if you succeeded following the above description and getting shown the appropriate image.
django933
Posts: 12
Joined: June 18th, 2014, 12:15 am

Re: Validate if a file exist otherwise set a default file

Post by django933 »

thank you !! never used lua script so ill begin with that. ill post my results
User avatar
balala
Rainmeter Sage
Posts: 16109
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Validate if a file exist otherwise set a default file

Post by balala »

django933 wrote: October 31st, 2020, 8:47 pm thank you !! never used lua script so ill begin with that. ill post my results
It's not too hard, don't be afraid of it. And please do so and let me know about the results.
django933
Posts: 12
Joined: June 18th, 2014, 12:15 am

Re: Validate if a file exist otherwise set a default file

Post by django933 »

Ok , first i created MyLua.lua with the script on @Resources folder , then i added the measure, the condition and the default meter. But now is always showing [MeterDefault]. I checked the about section of the skin and it shows MeasureImageLua range (0-1). The value is always 0 even if the image is there.

Code: Select all


[Rainmeter]
Update=500

[Variables]

[mPlayer]
Measure=Plugin
Plugin=NowPlaying.dll
PlayerName=Winamp
PlayerType=TITLE
PlayerPath=
DisableLeadingZero=0

[MeasureImageLua]
Measure=Script
ScriptFile=#@#MyLua.lua
Disabled=1

[mAlbum]
Measure=Plugin
UpdateDivider=-1
Plugin=NowPlaying.dll
PlayerType=FILE
PlayerName=[mPlayer]
DynamicVariables=1
RegExpSubstitute=1
Substitute="(.*\\).*":"\1image.png"
OnChangeAction=[!UpdateMeter MeterImage]
IfCondition=([&MeasureImageLua:file_exists('#CURRENTSECTION#')]=0)
IfTrueAction=[!ShowMeter "MeterDefault"][!HideMeter "MeterImage"][!Redraw]
IfFalseAction=[!HideMeter "MeterDefault"][!ShowMeter "MeterImage"][!Redraw]

[MeterImage]
DynamicVariables=1
Meter=Image
MeasureName=mAlbum
X=1372              
Y=302
W=500
H=500

[MeterDefault]
DynamicVariables=1
Meter=Image
ImageName=default.png
X=1372              
Y=302
W=500
H=500
Hidden=1

User avatar
balala
Rainmeter Sage
Posts: 16109
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Validate if a file exist otherwise set a default file

Post by balala »

django933 wrote: October 31st, 2020, 10:27 pm But now is always showing [MeterDefault]. I checked the about section of the skin and it shows MeasureImageLua range (0-1). The value is always 0 even if the image is there.
Remove the UpdateDivider=-1 option of the [mAlbum] measure, because it doesn't let the measure to be updated.
django933
Posts: 12
Joined: June 18th, 2014, 12:15 am

Re: Validate if a file exist otherwise set a default file

Post by django933 »

balala wrote: November 1st, 2020, 12:50 pm Remove the UpdateDivider=-1 option of the [mAlbum] measure, because it doesn't let the measure to be updated.
Removed UpdateDivider but didn't work still the same behavior
User avatar
balala
Rainmeter Sage
Posts: 16109
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Validate if a file exist otherwise set a default file

Post by balala »

django933 wrote: November 1st, 2020, 2:42 pm Removed UpdateDivider but didn't work still the same behavior
I suppose the .lua script is not working properly. To can check, please pack the whole config and upload the package here.
django933
Posts: 12
Joined: June 18th, 2014, 12:15 am

Re: Validate if a file exist otherwise set a default file

Post by django933 »

check_1.rmskin
(39.59 KiB) Downloaded 16 times
User avatar
balala
Rainmeter Sage
Posts: 16109
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Validate if a file exist otherwise set a default file

Post by balala »

django933 wrote: November 1st, 2020, 3:06 pm check_1.rmskin
There is a problem with the encoding of the MyLua.lua script file. In the package it has a UTF-8 encoding, but the lua script files should be encoded as UTF-16LE. Change the encoding of the file to this one, then check again. For details see this: https://forum.rainmeter.net/viewtopic.php?f=5&t=33243&p=165012#p165012
Post Reply