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.