It is currently October 22nd, 2021, 9:52 pm

Quote plugin alternative - Images

Quote of the Day, To-Do List and other text-based skins
User avatar
jsmorley
Developer
Posts: 22374
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Quote plugin alternative - Images

Post by jsmorley »

One of the slight issues with the Quote plugin is that it generates a new random number each time it is updated, so depending on what you are using as a resource (files in a folder or lines in a text file) each call to the plugin is treated as a brand-new random search of the resource, and no attempt is made to ensure that an item is not "repeated" until all items have been used.

The fewer the number of items in total, the more likely it is to repeat one, but there is never any guarantee, just the "odds", that it won't.

This quote skin shows how you can use a Lua script to overcome this minor limitation. What it will do is use the FileView plugin to index any number of images from some folder(s), will provide a random image on each update, and keep track of what has already been used so it won't repeat any of them until all of them have been displayed.
RandomImage_1.0.rmskin
RandomImage.jpg
RandomImage.ini

Code: Select all

[Rainmeter]
Update=1000

[Metadata]
Name=RandomImage
Author=JSMorley
Information=Displays a random non-repeating image from folder tree defined in [Variables] ImagePath.
License=Creative Commons Attribution-Non-Commercial-Share Alike 3.0
Version=Mar 21, 2013

[Variables]
ImagePath="C:\Users\Jeffrey\Pictures"
SecondsBetween=30

[MeasureFolder]
Measure=Plugin
Plugin=FileView
Recursive=2
Path=#ImagePath#
Extensions=jpg;png;bmp
FinishAction=[!EnableMeasure MeasureRandom][!ShowMeter MeterBackground]

[MeasureCount]
Measure=Plugin
Plugin=FileView
Path=[MeasureFolder]
Type=FileCount

[MeasureRandom]
Measure=Script
ScriptFile=RandomImage.lua
Disabled=1
UpdateDivider=#SecondsBetween#

[MeasureImagePath]
Measure=Plugin
Plugin=FileView
Path=[MeasureFolder]
Type=FilePath
IgnoreCount=1
Index=[MeasureRandom]
DynamicVariables=1

[MeterBackground]
Meter=Image
W=210
H=135
SolidColor=60,60,60,170
Hidden=1

[MeterImage]
Meter=Image
W=200
H=125
X=5
Y=5
MeasureName=MeasureImagePath
PreserveAspectRatio=1
TooltipText=[MeasureImagePath]
LeftMouseUpAction=["[MeasureImagePath]"]
DynamicVariables=1
RandomImage.lua

Code: Select all

function Initialize()

	msMeasureCount = SKIN:GetMeasure("MeasureCount")
	
	currentTable = {}
	
end -- Initialize

function Update()

	fileCount = msMeasureCount:GetValue()
	
	if #currentTable == 0 then
		currentTable = RandomOrder(fileCount)
	end
	
	return table.remove(currentTable)
	
end	-- Update

function RandomOrder(inputCount)

	assert(type(inputCount) == 'number', ('RandomOrder must receive a number. Received %s instead.'):format(type(inputCount)))

	local sortedTable, unsortedTable = {}, {}
	
	for inc = 1, inputCount do
		unsortedTable[inc] = inc
	end
	
	math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,6)))

	while #unsortedTable > 0 do
		table.insert(sortedTable, table.remove(unsortedTable, math.random(1, #unsortedTable)))
	end
	
	unsortedTable = nil
	
	return sortedTable	
	
end -- RandomOrder
What the Lua does is:

- in Update(), get the total count of files indexed by the FileView plugin measure.

- Check to see if a temporary table (currentTable) created to hold the random numbers between 1 and that total count is empty.

- If so - Execute the RandomOrder() function which will create the temporary table by placing the numbers between 1 and the total count in it in a random order.

- Remove and return the first index number from the temporary / random table to the skin, for display in the meter.

Each of the images from the folder(s) will be used in a random order, never repeating until all of them have been used. Then it will start over. Feel free to ask any questions about how / what this is doing.
You do not have the required permissions to view the files attached to this post.