It is currently January 16th, 2021, 7:58 am

Validate if a file exist otherwise set a default file

Help with creating, editing & fixing problems with skins
User avatar
Yincognito
Posts: 2948
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 pmThe only problem is that some paths with "[ ]" throws Syntax error in the IfCondition.
Oh, I see - let me think of a solution, as for sure there is one... :???:
User avatar
balala
Rainmeter Sage
Posts: 12062
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 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)

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.
Good point. Didn't noticed this, but you probably are right, especially if it does work for the OP as well. So, congratulations.
User avatar
Yincognito
Posts: 2948
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 2nd, 2020, 6:35 pm Good point. Didn't noticed this, but you probably are right, especially if it does work for the OP as well. So, congratulations.
No problem - glad to help. Any idea why the square brackets (actually, mostly [) yield an error if used in inline Lua (or, in this case, in the path)? For example:

IfCondition=([&MeasureImageLua:file_exists('C:\blabla\image.png')]=0)
works fine, but:
IfCondition=([&MeasureImageLua:file_exists('C:\bla[bla\image.png')]=0)
yields an error?

I couldn't figure this out, and I tried all kinds of escaping sequences for the [ character, including using Unicode character references. :confused:
User avatar
balala
Rainmeter Sage
Posts: 12062
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 2nd, 2020, 6:46 pm No problem - glad to help. Any idea why the square brackets (actually, mostly [) yield an error if used in inline Lua (or, in this case, in the path)? For example:

IfCondition=([&MeasureImageLua:file_exists('C:\blabla\image.png')]=0)
works fine, but:
IfCondition=([&MeasureImageLua:file_exists('C:\bla[bla\image.png')]=0)
yields an error?

I couldn't figure this out, and I tried all kinds of escaping sequences for the [ character, including using Unicode character references. :confused:
A while ago there has been such a question (and if I'm not mistaken a solution, as well) here on the forum. But unfortunately I don't know exactly when and what the solution was. I'm gonna try immediately to find it, but I'm not sure I'll succeed. If I won't, probably sooner or later a solution will arise here, so recommend to wait a little bit.
User avatar
Yincognito
Posts: 2948
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 2nd, 2020, 7:01 pm A while ago there has been such a question (and if I'm not mistaken a solution, as well) here on the forum. But unfortunately I don't know exactly when and what the solution was. I'm gonna try immediately to find it, but I'm not sure I'll succeed. If I won't, probably sooner or later a solution will arise here, so recommend to wait a little bit.
Not sure if you refer to this one, but found this from moshi and jsmorley (not related to Lua though). Unfortunately, it's not applicable to this scenario, since using different characters will also make the process not refer to the exact same path.

From what I noticed though, the error doesn't seem to affect the result in the skin, unless the OP has a different experience with this. So, in the end, it might be just a minor inconvenient. In any case, my other solution using negative coordinates is available, just in case.
User avatar
balala
Rainmeter Sage
Posts: 12062
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 2nd, 2020, 7:26 pm Not sure if you refer to this one, but found this from moshi and jsmorley (not related to Lua though). Unfortunately, it's not applicable to this scenario, since using different characters will also make the process not refer to the exact same path.

From what I noticed though, the error doesn't seem to affect the result in the skin, unless the OP has a different experience with this. So, in the end, it might be just a minor inconvenient. In any case, my other solution using negative coordinates is available, just in case.
No, not this. I gave a try as well in meantime, and found this, but I feel this is not either. Hoping someone who knows a solution will come to post it soon. In meantime if the code does work, even with error messages, I think it's alright, at least partially.
User avatar
Yincognito
Posts: 2948
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 2nd, 2020, 7:42 pm No, not this. I gave a try as well in meantime, and found this, but I feel this is not either. Hoping someone who knows a solution will come to post it soon. In meantime if the code does work, even with error messages, I think it's alright, at least partially.
Well, I believe in that post kyriakos876 tried to say the same as I do: there isn't a good way to escape the square brackets in an inline Lua. Or, there may be just a parser thing. Oh and yeah, where is that "someone" (jsmorley cough cough jsmorley :lol: ) when you really need him, you know, Lua problems and such? :Whistle

That being said, I may have to post this and another "bug" I discovered while trying my solution using negative coordinates earlier in the Bugs & Feature Suggestions. Just have to make sure they are both real issues and not false flags. Stay tuned.
User avatar
Yincognito
Posts: 2948
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

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

Post by Yincognito »

Ok, first I will post the code that checks for the image meter dimensions to see if the image exists or not, i.e. the one using negative coordinates like I explained earlier. This code should work, but it doesn't, despite being logically correct, at least the way I see it:

Code: Select all

[Variables]

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

---Measures---

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

[mAlbum]
Measure=NowPlaying
PlayerName=[mPlayer]
PlayerType=File
RegExpSubstitute=1
Substitute="^(.*\\).*$":"\1image.png"
OnChangeAction=[!UpdateMeterGroup ImagesGroup][!Redraw]
DynamicVariables=1

---Meters---

[MeterTest]
Group=ImagesGroup
Meter=Image
MeasureName=mAlbum
X=-9999
Y=-9999
DynamicVariables=1
OnUpdateAction=[!Log "W = [MeterTest:W]"]

[MeterDefault]
Group=ImagesGroup
Hidden=([MeterTest:W]>0)
Meter=Image
ImageName=default.png
X=0
Y=0
W=500
H=500
DynamicVariables=1

[MeterImage]
Group=ImagesGroup
Hidden=([MeterTest:W]=0)
Meter=Image
MeasureName=mAlbum
X=0
Y=0
W=500
H=500
DynamicVariables=1
I posted this so I can reference it from the future Bugs & Feature Suggestions, where I will post a simplified version of it. The idea is that if [mAlbum] is the path to a non existing image file, the width of [MeterTest] should be 0, but it is not. The value is logged in the OnUpdateAction option, in order to see its behavior.
User avatar
balala
Rainmeter Sage
Posts: 12062
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 2nd, 2020, 9:09 pm Ok, first I will post the code that checks for the image meter dimensions to see if the image exists or not, i.e. the one using negative coordinates like I explained earlier. This code should work, but it doesn't, despite being logically correct, at least the way I see it:
To get the size of an image you could use the ImageSize plugin. It can read the size of the image, without having to add it to an Image meter.
User avatar
Yincognito
Posts: 2948
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 2nd, 2020, 9:29 pm To get the size of an image you could use the ImageSize plugin. It can read the size of the image, without having to add it to an Image meter.
Thanks for the suggestion, but does one need to use a plugin for every little bit of functionality out there, especially when that functionality exists already? Or, if the plugins are really useful and efficient, why not including them in the standard Rainmeter distribution instead of having to hunt them through the many pages of the forum?

Bottom line, while I very much appreciate both the suggestion and the plugin, it really feels awkward to have to use an image size plugin when there is an image plugin already. It's like having a nice text editor, but using an entirely different program (or some add-on that you have to download, install, etc.) to get the number of lines in an opened text file in that editor. A bit redundant, IMHO, to put it mildly. :confused: