It is currently April 26th, 2024, 6:47 am

Validate if a file exist otherwise set a default file

Get help with creating, editing & fixing problems with skins
User avatar
Yincognito
Rainmeter Sage
Posts: 7164
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

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

Post by Yincognito »

django933 wrote: October 31st, 2020, 7:20 pmHi 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.
balala wrote: October 31st, 2020, 8:38 pmThe simplest (the only?) solution is to use a .lua script, to check the existence of the appropriate file.
I believe there's an even simpler variant to this, assuming the default image spans the entire extent of the image frame - just put the actual image over the default one (the X,Y,W,H should be the same for both):

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

---Meters---

[DefaultImage]
Meter=Image
ImageName=#@#a.ico

[ActualImage]
Meter=Image
ImageName=#@#b.ico
I used my own a.ico and b.ico images, but this can be adjusted, of course. The idea is that the default image will be displayed anyway, and must exist. Then, if the actual image exists, it will be drawn "over" the default image, in effect replacing it visually. If it doesn't, well, no harm done, nothing will be drawn over the default image, in effect displaying the default when the actual image is missing (without having to check for the file existence).

Now, of course, it would help if the image files have the same width and height, or at least their meters be drawn on the same dimension area through the W and H options. Otherwise, small pieces of the default image will be visible from "underneath" the actual image (just an example). I'm sure the approach could be tweaked to suit these possibilities though, for example by adding an actual image background to the scheme.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
balala
Rainmeter Sage
Posts: 16172
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

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

Post by balala »

Yincognito wrote: November 1st, 2020, 5:25 pm I used my own a.ico and b.ico images, but this can be adjusted, of course. The idea is that the default image will be displayed anyway, and must exist. Then, if the actual image exists, it will be drawn "over" the default image, in effect replacing it visually. If it doesn't, well, no harm done, nothing will be drawn over the default image, in effect displaying the default when the actual image is missing (without having to check for the file existence).
Sorry, can't follow you this time, but i think if the image doesn't exist, you get error messages, which would be great to be avoided, if possible.
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, 4:38 pm 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
Same problem, i converted to UTF-16LE on notepad or USC-2 LE with notepad++ and also created the file again, converted then i pasted the code and doesn't work.
django933
Posts: 12
Joined: June 18th, 2014, 12:15 am

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

Post by django933 »

Yincognito wrote: November 1st, 2020, 5:25 pm
I believe there's an even simpler variant to this, assuming the default image spans the entire extent of the image frame - just put the actual image over the default one (the X,Y,W,H should be the same for both):
not an option for me, images are pngs and have differents zones with transparency. Thanks
User avatar
Yincognito
Rainmeter Sage
Posts: 7164
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

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

Post by Yincognito »

django933 wrote: November 1st, 2020, 11:59 pm not an option for me, images are pngs and have differents zones with transparency. Thanks
I see. Too bad, it was a really simple variant, despite balala's incorect claim that the method would produce "errors". :jawdrop
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
Yincognito
Rainmeter Sage
Posts: 7164
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

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

Post by Yincognito »

Building on the approach from my previous post, here's a working version:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

---Meters---

[TestImage]
Meter=Image
X=-32
Y=-32
ImageName=#@#a.png

[DefaultImage]
Hidden=(([TestImage:W]>0) && ([TestImage:H]>0))
Meter=Image
W=32
H=32
ImageName=#@#c.png

[ActualImage]
Hidden=(([TestImage:W]=0) && ([TestImage:H]=0))
Meter=Image
W=32
H=32
ImageName=#@#a.png
These are, as before, based on my own "test" images (I don't have album images for my tracks, I don't use those), so they should probably adapted to your own scenario. The idea is that:
- the invisible (due to the appropriate negative coordinates, not supported by Rainmeter) test image meter tries to load the actual (album) image
- the default image meter is hidden if the test image meter's dimensions are greater than 0, i.e. if the image exists, otherwise shown
- the actual image meter is hidden if the test image meter's dimensions are equal to 0, i.e. if the image doesn't exist, otherwise shown

This follows a similar principle to the Total option of the FreeDiskSpace measure, which can be used to test if a drive / partition exists since if it doesn't its value is 0. Here, the "out of view" test image meter is used to check if the image file exists by checking the meter's dimensions against 0 values. Naturally, while the other image meters can have their W and H set, the test image meter should not, in order to be able to tell if it's "populated" or not by checking its dimensions. Also, its X and Y should be adjusted to the appropriate negative values so that it is entirely out of the skin area (or out of view).

Of course, in your scenario, a bit of care should be taken on the matter of updating the meters, generally doing it only when needed. I won't tailor this to your example today, as it's a bit late where I live and I'm in the mood for some relax time, but tomrrow, if you don't figure out how to adapt it by then, I'll do it.

Or, you can wait for balala to tell you how to fix the Lua variant. Your choice. ;-)

P.S. The test image can be made "invisible" by placing it into a transparent Container meter as well, but of course, doing it by setting its coordinates to negative values is simpler.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
balala
Rainmeter Sage
Posts: 16172
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, 11:56 pm Same problem, i converted to UTF-16LE on notepad or USC-2 LE with notepad++ and also created the file again, converted then i pasted the code and doesn't work.
Didn't check in time, just now, but the skin's main file (meter.ini) has the same encoding problem as the .lua file. So convert its encoding as well to UTF-16LE (UCS-2 LE BOM in Notepad++) and try again.
User avatar
Yincognito
Rainmeter Sage
Posts: 7164
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

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

Post by Yincognito »

balala wrote: November 1st, 2020, 4:38 pmThere 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
django933 wrote: November 1st, 2020, 11:56 pmSame problem, i converted to UTF-16LE on notepad or USC-2 LE with notepad++ and also created the file again, converted then i pasted the code and doesn't work.
Just tested now your variant, based on a slightly modified code without the UpdateDivider=-1 as suggested, and django933 is right, it doesn't work - the image stays the default one. It's NOT the encoding though (that's certain, as I converted both the script and the INI to UCS-2 LE BOM), it's the wrong syntax in the inline Lua call (can't believe I'm lecturing on Lua syntax now - must be something wrong with me today, LOL) - it should be:

IfCondition=([&MeasureImageLua:file_exists('[&[#CURRENTSECTION]]')]=0)
instead of the original:
IfCondition=([&MeasureImageLua:file_exists('#CURRENTSECTION#')]=0)

That's because of the nested syntax when using similar Lua inline calls, as shown and explained in the simple sample here, coupled with the fact that the #CURRENTSECTION# variable is used as well.

Full code:

Code: Select all

[Variables]

[Rainmeter]
Update=500
DynamicWindowSize=1
AccurateText=1

---Measures---

[mPlayer]
Measure=NowPlaying
PlayerName=Winamp
PlayerType=Title
DisableLeadingZero=0

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

[mAlbum]
Measure=NowPlaying
PlayerName=[mPlayer]
PlayerType=File
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]
DynamicVariables=1

---Meters---

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

[MeterDefault]
Hidden=1
Meter=Image
ImageName=default.png
X=0
Y=0
W=500
H=500
DynamicVariables=1
So, this should work (as far as I tested it). I made some other minor changes to the original code, related to getting it up to date with the modern syntax (e.g. NowPlaying is now a measure, not a plugin), some X and Y changes so I can see the skin on my 1366x768 laptop monitor, removing the Lua measure disabling (just to eliminate the possibility that it was a culprit) and some better tuning of the Substitute. Feel free to revert any of those changes, if you like.

That is, if you didn't apply my previous solution using negative coordinates already, of course.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
django933
Posts: 12
Joined: June 18th, 2014, 12:15 am

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

Post by django933 »

Yincognito wrote: November 2nd, 2020, 3:32 pm Just tested now your variant, based on a slightly modified code without the UpdateDivider=-1 as suggested, and django933 is right, it doesn't work - the image stays the default one. It's NOT the encoding though (that's certain, as I converted both the script and the INI to UCS-2 LE BOM), it's the wrong syntax in the inline Lua call (can't believe I'm lecturing on Lua syntax now - must be something wrong with me today, LOL) - it should be:

IfCondition=([&MeasureImageLua:file_exists('[&[#CURRENTSECTION]]')]=0)
instead of the original:
IfCondition=([&MeasureImageLua:file_exists('#CURRENTSECTION#')]=0)
This worked !!! . I was trying with all the files UTF-16 LE and nothing.Now i updated the NowPlaying too.

The only problem is that some paths with "[ ]" throws Syntax error in the IfCondition.


Thank you both!!!
User avatar
Yincognito
Rainmeter Sage
Posts: 7164
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

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

Post by Yincognito »

django933 wrote: November 2nd, 2020, 5:01 pm This worked !!! . I was trying with all the files UTF-16 LE and nothing.Now i updated the NowPlaying too.

The only problem is that some paths with "[ ]" throws Syntax error in the IfCondition.


Thank you both!!!
Excellent then - glad you got it working as you wanted to. :great:
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth