It is currently June 25th, 2019, 12:25 am

[Script] Path Parser

Discuss the use of Lua in Script measures.
User avatar
Kaelri
Developer
Posts: 1739
Joined: July 25th, 2009, 4:47 am

[Script] Path Parser

Kaelri » July 29th, 2011, 6:23 am

Not as dramatic as yesterday's invention, but I've written a little script that takes a file path and returns the directory, filename (with or without extension), or the extension, as desired. It's pretty durable - doesn't mind periods and spaces in either folder or file names. I'm going to use it for a few of my skins: getting the filenames of Notes files as the title, and showing the filename of the Gallery image on mouseover, which has been a much-requested feature.

Usage

Code: Select all

[MeasurePathParser]
Measure=Script
ScriptFile=PathParser.lua
Input=C:\Path\To\File.txt
Output=OutputType
The path to parse may be provided as one of three keys, in the following order of precedence:
  • Input - a pure value.
  • InputVariable - the name of a variable which resolves to the filepath.
  • InputMeasure - the name of a measure whose current value is the filepath.
The latter two are read on each Update(), so they'll work with dynamic variables.

As for Output, you can specify one of four types:
  • Dir - the file directory, including trailing backslash (\).
  • Name - the file name, not including extension.
  • NameExt - the file name, including extension.
  • Ext - the file extension.
The script also sets the dynamic variables #Dir#, #Name#, #NameExt# and #Ext#. If you want to use multiple instances of the script in the same skin, you can set a VariablePrefix on the Script measure, which will be added to each of the variables created by that method, just like the feed reader.

Code: Select all

PROPERTIES = {
	Input = '0';
	InputVariable = '0';
	InputMeasure = '0';
	Output = '';
	VariablePrefix = '';
}

function Initialize()
	sInput = PROPERTIES.Input
	sInputVariable = PROPERTIES.InputVariable
	sInputMeasure = PROPERTIES.InputMeasure
	sOutput = PROPERTIES.Output
	sVariablePrefix = PROPERTIES.VariablePrefix
end

function Update()
	if sInput ~= '0' then
		sPath = sInput
	elseif sInputVariable ~= '0' then
		sPath = SKIN:GetVariable(sInputVariable)
	elseif sInputMeasure ~= '0' then
		msInput = SKIN:GetMeasure(sInputMeasure)
		sPath = msInput:GetStringValue()
	else
		return 'Invalid input type.'
	end
	
	sDir, sName, sExt = string.match(sPath, "(.-)([^\\]-)%.([^%.]+)$")
	sNameExt = sName..'.'..sExt
	
	SKIN:Bang('!SetVariable "'..sVariablePrefix..'Dir" "'..sDir..'"')
	SKIN:Bang('!SetVariable "'..sVariablePrefix..'Name" "'..sName..'"')
	SKIN:Bang('!SetVariable "'..sVariablePrefix..'NameExt" "'..sNameExt..'"')
	SKIN:Bang('!SetVariable "'..sVariablePrefix..'Ext" "'..sExt..'"')
	
	if sOutput == 'Dir' then
		return sDir
	elseif sOutput == 'Name' then
		return sName
	elseif sOutput == 'NameExt' then
		return sNameExt
	elseif sOutput == 'Ext' then
		return sExt
	else
		return 'Invalid output type.'
	end
end
User avatar
Jkon
Posts: 192
Joined: December 4th, 2009, 2:05 am

Re: [Script] Path Parser

Jkon » July 29th, 2011, 3:04 pm

Guess who's turning into a right little Lua junkie :D .Very useful script,thanks for sharing it.Now if you can create a script that will return the dimensions of the image being used,I'll kiss your feet.
Image
User avatar
jsmorley
Developer
Posts: 19268
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: [Script] Path Parser

jsmorley » July 29th, 2011, 4:52 pm

Not that hard really...

Skin: ImageSize.ini

Code: Select all

[Rainmeter]
Update=1000

[MeterImage]
Meter=Image
ImageName=SomeImage.png
ImageTint=255,255,255,1

[MeasureLua]
Measure=Script
ScriptFile=ImageSize.lua
MeterToRead=MeterImage
UpdateDivider=30

[MeterOne]
Meter=String
FontColor=255,255,255,255
FontSize=15
AntiAlias=1
Text=Width: #Width# Height: #Height#
DynamicVariables=1
Lua: ImageSize.lua

Code: Select all

PROPERTIES =
{

MeterToRead = "";

}

function Initialize()

sMeterToRead = PROPERTIES.MeterToRead
mtMeterToRead = SKIN:GetMeter(sMeterToRead)

end -- function Initialize

function Update()

Width = mtMeterToRead:GetW()
Height = mtMeterToRead:GetH()

SKIN:Bang("!SetVariable Width "..Width)
SKIN:Bang("!SetVariable Height "..Height)

return tostring(Width.." X "..Height)

end -- function Update
What's it doing:

- Loads an image in Rainmeter, but does not display it. (ImageTint=255,255,255,1)
Could be an image meter getting an image from Quote plugin, WebParser, whatever, the key is the image meter, not where it comes from or what the name or path of the image is. If you want to "show" the meter, fine. If you don't, that's fine too, but use ImageTint to "hide" it. Hidden=1 will not work as there is then no "width" or 'height".

- Passes the name of the Image meter to Lua, which reads the W and H from the meter.

- Returns the Width and Height using !SetVariable. (note, you don't really need to set them in the skin in [Variables] to use this)
This is the width and height that the meter is using. If you don't scale the meter with W and H, then it is the actual image size, if you do, it is the size that the meter is displaying it at.

- Displays these variables on a meter with DynamicVariables = 1. (Or use them in a Calc measure or whatever you want.)

If this is an image you are not actually displaying, you might want to play some tricks with using the Lua to hide the meter after you get the dimensions so you don't have an "almost" invisible meter sitting at 0,0 on your screen where it might get in the way of something else, but that could be done as well by having the Lua use a bang to hide the meter and disable itself after you read the W and H from the meter.
User avatar
Jkon
Posts: 192
Joined: December 4th, 2009, 2:05 am

Re: [Script] Path Parser

Jkon » July 29th, 2011, 6:31 pm

So am I right in thinking that I would need 2 meters if I want to manipulate the dimensions of the image,one to acquire the real dimensions and a 2nd to portray the manipulations.The reason I asked,is that I've been working on an collage like multi image viewer,each image is scalable by dragging a handle (using the images width as the base value with preserveaspectratio on,this works fine till i try and add a border to the images.I then seem to require the image height.
Image
User avatar
jsmorley
Developer
Posts: 19268
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: [Script] Path Parser

jsmorley » July 29th, 2011, 6:33 pm

Jkon wrote:So am I right in thinking that I would need 2 meters if I want to manipulate the dimensions of the image,one to acquire the real dimensions and a 2nd to portray the manipulations.The reason I asked,is that I've been working on an collage like multi image viewer,each image is scalable by dragging a handle (using the images width as the base value with preserveaspectratio on,this works fine till i try and add a border to the images.I then seem to require the image height.
Yeah, this will return what the W and H is on the meter you pass to it. So if you are scaling on the image meter you are actually showing, you would need another "hidden" one to get the real dimensions. it's not a perfect solution. Today it would be somewhat easier to use a little AutoIt addon that just gets the dimensions from the image on disk, and uses !SetVariable to pass back the dimensions. We are hoping in the future to extend Lua with a library that will allow access to the Windows API system that would make stuff like this that is not "platform independent" possible in Lua itself.
User avatar
Jkon
Posts: 192
Joined: December 4th, 2009, 2:05 am

Re: [Script] Path Parser

Jkon » July 29th, 2011, 6:57 pm

it's not a perfect solution
We all can and should strive for perfection,but to presume that we can or ever will reach it is naive.Your solution is more then adequate for my needs.Thank you (again).Just one other thing,as I understand it,when an image is displayed without W or H defined,the meter is actually aware of the dimensions,so could this not allow for something like a #CurrentMeterWidth# built in variable.Something I think could be very handy.
Image
User avatar
jsmorley
Developer
Posts: 19268
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: [Script] Path Parser

jsmorley » July 29th, 2011, 7:10 pm

Jkon wrote: We all can and should strive for perfection,but to presume that we can or ever will reach it is naive.Your solution is more then adequate for my needs.Thank you (again).Just one other thing,as I understand it,when an image is displayed without W or H defined,the meter is actually aware of the dimensions,so could this not allow for something like a #CurrentMeterWidth# built in variable.Something I think could be very handy.
I'm not sure how easy it would be to "expose" the "original" size of an image meter before scaling. You never know.
User avatar
Jkon
Posts: 192
Joined: December 4th, 2009, 2:05 am

Re: [Script] Path Parser

Jkon » July 29th, 2011, 7:40 pm

Well,I'll keep my fingers crossed and seeing as what might be implemented in the next build,I'll not get greedy. :D
Image
User avatar
StArL0rd84
Posts: 335
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Re: [Script] Path Parser

StArL0rd84 » May 30th, 2017, 7:03 pm

I have run into what might be an unique situation.
I am trying to parse the drive letter only.
Any chance your script can do this?
(#WorkTime# = 1 ? #Work# : ([mEnergyLoss:%] > 100 ? #SleepMode# : #Rainmeter#))
User avatar
balala
Rainmeter Sage
Posts: 8308
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: [Script] Path Parser

balala » May 30th, 2017, 7:35 pm

StArL0rd84 wrote:I have run into what might be an unique situation.
I am trying to parse the drive letter only.
Any chance your script can do this?
Not very sure is a good idea to wake up a thread from 2011, but however...
Try to replace the sDir, sName, sExt = string.match(sPath, "(.-)([^\\]-)%.([^%.]+)$") line of the lua script, with the following one: [color=#FF0000]sDrive,[/color] sDir, sName, sExt = string.match(sPath, "[color=#FF0000](.):\\[/color](.-)([^\\]-)%.([^%.]+)$"). See that this line has a newly introduced variable: sDrive. This variable will get the letter of the drive.
Then add the following line (theoretically you can add it anywhere after the above line, but logically would be to add it before the SKIN:Bang('!SetVariable "'..sVariablePrefix..'Dir" "'..sDir..'"') line): SKIN:Bang('!SetVariable "'..sVariablePrefix..'Drive" "'..sDrive..':"'). With this command, you'll set the Drive variable usedin the skin, which will return the letter of the drive.