It is currently May 21st, 2024, 4:46 am

Rainmeter (If FILE EXIST THEN IMAGE 1 ELSE IMAGE 2)

Get help with creating, editing & fixing problems with skins
JPapavas
Posts: 8
Joined: September 5th, 2013, 10:07 pm

Rainmeter (If FILE EXIST THEN IMAGE 1 ELSE IMAGE 2)

Post by JPapavas »

Hello, i am new and i try to use calc value but i din't make it to work.
Can someone please show a example how to do this...

If file C:\Users\%username%\Documents\temp\1.txt has value "a" (just letter)
then image1
else image2

Kind regards,
JP
User avatar
jsmorley
Developer
Posts: 22634
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Rainmeter (If FILE EXIST THEN IMAGE 1 ELSE IMAGE 2)

Post by jsmorley »

JPapavas wrote:Hello, i am new and i try to use calc value but i din't make it to work.
Can someone please show a example how to do this...

If file C:\Users\%username%\Documents\temp\1.txt has value "a" (just letter)
then image1
else image2

Kind regards,
JP
I don't understand. Are you trying to detect whether the file 1.txt exists, or that it contains the text "a" in it?
User avatar
jsmorley
Developer
Posts: 22634
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Rainmeter (If FILE EXIST THEN IMAGE 1 ELSE IMAGE 2)

Post by jsmorley »

If your intent is that you want to check that a file called 1.txt is there and contains a letter "a" as the entire text of it, then:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1

[MeasureFile]
Measure=Plugin
Plugin=QuotePlugin
PathName=#CURRENTPATH#1.txt
UpdateDivider=10
Substitute="a":"1","":"-1"

[MeasureCheck]
Measure=Calc
Formula=[MeasureFile]
IfAboveValue=0
IfAboveAction=[!SetOption MeterOne ImageName "#@#Images\Border10.png"][!UpdateMeter MeterOne][!Redraw]
IfBelowValue=0
IfBelowAction=[!SetOption MeterOne ImageName "#@#Images\Border15.png"][!UpdateMeter MeterOne][!Redraw]
DynamicVariables=1

[MeterOne]
Meter=Image
http://docs.rainmeter.net/manual-beta/plugins/quote
http://docs.rainmeter.net/manual-beta/measures/general-options/substitute
http://docs.rainmeter.net/manual-beta/measures/general-options/ifactions
http://docs.rainmeter.net/manual-beta/bangs#SetOption

This is a somewhat unusual use of the Quote plugin, but I really think it is the best way. There is a way to get there with WebParser, but that is going to spit up errors in the log if the file isn't there or doesn't contain an "a", and although the skin will work fine, the errors would annoy me, as it isn't an error, just a perfectly valid "yes / no" binary answer.

With Quote plugin, you are asking it to open the file, and return a random line from it. Since there is only one line in the file, it will always return "a" or nothing... If it is "a", then the Substitute makes it "1", if the file is not there, or does not contain "a" on the only line, then the Substitute makes it "-1". The Calc measure tests for this and sets the ImageName.

Make sure the text file has just a "a" in it, not an "a" and a carriage return.
JPapavas
Posts: 8
Joined: September 5th, 2013, 10:07 pm

Re: Rainmeter (If FILE EXIST THEN IMAGE 1 ELSE IMAGE 2)

Post by JPapavas »

Thank you so much, i was reading last night pages and pages...
you always help everyone! i will use it right away!
And i will let you know...

Kind regards,
John P
JPapavas
Posts: 8
Joined: September 5th, 2013, 10:07 pm

Re: Rainmeter (If FILE EXIST THEN IMAGE 1 ELSE IMAGE 2)

Post by JPapavas »

Hello, something is wrong...

--------------------------------------------------------------
[MeasureFile]
Measure=Plugin
Plugin=QuotePlugin
PathName=#CURRENTPATH#1.txt
UpdateDivider=10
Substitute="a":"1","":"-1"

[MeasureCheck]
Measure=Calc
Formula=[MeasureFile]
IfAboveValue=0
IfAboveAction=[!SetOption MeterOne ImageName "#@#Images\nb.png"][!UpdateMeter MeterOne][!Redraw]
IfBelowValue=0
IfBelowAction=[!SetOption MeterOne ImageName "#@#Images\fb.png"][!UpdateMeter MeterOne][!Redraw]

--------------------------------------------------------------------------

P.S. file 1.txt must be in
C:\Users\%username%\Documents\Rainmeter\Skins\Name ?
Last edited by JPapavas on September 7th, 2013, 11:04 am, edited 1 time in total.
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5414
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Rainmeter (If FILE EXIST THEN IMAGE 1 ELSE IMAGE 2)

Post by eclectic-tech »

Yes, that is where the file has to be...
Did you make sure it is formatted as JSMorley said?
...
Make sure the text file has just a "a" in it, not an "a" and a carriage return.
...
You can also open the "About" window, click on "Skins" tab, and then this skin, to see the value being returned by the measure.
The "About" window is a valuable troubleshooting tool.
JPapavas
Posts: 8
Joined: September 5th, 2013, 10:07 pm

Re: Rainmeter (If FILE EXIST THEN IMAGE 1 ELSE IMAGE 2)

Post by JPapavas »

without "a" char:

measureFile range 0.0 - 1.0 value -1
measurecheck range 0.0 - 1.0 value -1

with "a" char:

measureFile range 0.0 - 1.0 value 1
measurecheck range 0.0 - 1.0 value -1


I see this... if it helps...

image need x and y location?
User avatar
jsmorley
Developer
Posts: 22634
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Rainmeter (If FILE EXIST THEN IMAGE 1 ELSE IMAGE 2)

Post by jsmorley »

You just need DynamicVariables=1 on the [MeasureCheck] measure.

Code: Select all

[MeasureCheck]
Measure=Calc
Formula=[MeasureFile]
IfAboveValue=0
IfAboveAction=[!SetOption MeterOne ImageName "#@#Images\lightonb.png"][!UpdateMeter MeterOne][!Redraw]
IfBelowValue=0
IfBelowAction=[!SetOption MeterOne ImageName "#@#Images\lightoffb.png"][!UpdateMeter MeterOne][!Redraw]
DynamicVariables=1
The file 1.txt can be anywhere you want it. Just change the PathName option on the Quote plugin measure.
User avatar
jsmorley
Developer
Posts: 22634
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Rainmeter (If FILE EXIST THEN IMAGE 1 ELSE IMAGE 2)

Post by jsmorley »

As an aside, if all you really need to do is check to see if a file exists, then this is a pretty general-purpose way to do that using Lua.

Skin Test.ini:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1

[Variables]
FileToCheck=#CURRENTPATH#Test.txt

[MeasureFile]
Measure=Script
ScriptFile=#CURRENTPATH#Test.lua
UpdateDivider=5

[MeasureCheck]
Measure=Calc
Formula=[MeasureFile]
IfAboveValue=0
IfAboveAction=[!SetOption MeterCheck Text "File exists."][!UpdateMeter MeterCheck][!Redraw]
IfBelowValue=0
IfBelowAction=[!SetOption MeterCheck Text "File not found."][!UpdateMeter MeterCheck][!Redraw]
DynamicVariables=1

[MeterCheck]
Meter=String
FontSize=14
FontColor=255,255,255,255
SolidColor=60,60,60,255
Padding=5,5,5,5
Antialias=1
Lua Test.lua:

Code: Select all

function Update()

	local FilePath = SKIN:MakePathAbsolute(SKIN:GetVariable('FileToCheck'))
	local FoundFile = io.open(FilePath,'r')
	
	if not FoundFile then
		return -1
	else
		io.close(FoundFile)
		return 1
	end
	
end
The Lua simply gets the path/name of the file you want to check from the variable FileToCheck, and attempts to open the file in read-only mode. If the file "open" works, then the file is there and the measure returns "1". If not, the measure returns "-1". The Calc measure then checks the value of the script measure and does whatever you want it to do.

The advantage to this approach is that you can alternatively have the Lua script do all kinds of things itself. Change text, change images, show or hide meters, disable or enable other measures, anything you want. It could be done via SKIN:Bang() commands from the Lua to the skin, without needing some IFAction from hell in a Calc measure.

It would also be really simple to have the Lua actually read the file contents and parse it looking for some value that you would use as a trigger, in addition to the existence of the file.

The only caveat I would add is that it is important to have some UpdateDivider (even just a couple of seconds) on the script measure. If you are just constantly opening the Test.txt file for reading, it will be hard for some external process that is deleting it to have an opportunity to do so without getting "file is in use" errors from Windows. While you can open and read or change the contents of a file that is open with the 'r' parameter in Lua with some other process, you can't rename or delete it while it is open in Lua.
JPapavas
Posts: 8
Joined: September 5th, 2013, 10:07 pm

Re: Rainmeter (If FILE EXIST THEN IMAGE 1 ELSE IMAGE 2)

Post by JPapavas »

Thank you for the (always) fast help. I can say for sure that this Forum has great support.

Now with your help the final code is this...

-----------------------------------------------------------
[Rainmeter]
Author=JPapavas
Update=1000

[Metadata]
Name=example1
Version=1.0

;End of added Metadata

[App1]
meter=button
buttonimage=a1.png
x=0
y=0
buttoncommand=!execute ["C:\example.exe"]

[App2]
meter=button
buttonimage=a2.png
x=0
y=17
buttoncommand=!execute ["C:\example.exe"]

[MeasureFile]
Measure=Plugin
Plugin=QuotePlugin
PathName=#CURRENTPATH#a1.txt
UpdateDivider=5
Substitute="1":"1","0":"-1"

[MeasureCheck]
Measure=Calc
Formula=[MeasureFile]
IfAboveValue=0
IfAboveAction=[!SetOption MeterOne ImageName "b1.png"][!UpdateMeter MeterOne][!Redraw]
IfBelowValue=0
IfBelowAction=[!SetOption MeterOne ImageName "b2.png"][!UpdateMeter MeterOne][!Redraw]
DynamicVariables=1

[MeterOne]
Meter=IMAGE
X=15
----------------------------------------------------------------------------------------------

Kind regards,
JPapavas