Page 1 of 2

[Script] Path Parser

Posted: July 29th, 2011, 6:23 am
by Kaelri
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

Re: [Script] Path Parser

Posted: July 29th, 2011, 3:04 pm
by Jkon
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.

Re: [Script] Path Parser

Posted: July 29th, 2011, 4:52 pm
by jsmorley
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.

Re: [Script] Path Parser

Posted: July 29th, 2011, 6:31 pm
by Jkon
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.

Re: [Script] Path Parser

Posted: July 29th, 2011, 6:33 pm
by jsmorley
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.

Re: [Script] Path Parser

Posted: July 29th, 2011, 6:57 pm
by Jkon
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.

Re: [Script] Path Parser

Posted: July 29th, 2011, 7:10 pm
by jsmorley
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.

Re: [Script] Path Parser

Posted: July 29th, 2011, 7:40 pm
by Jkon
Well,I'll keep my fingers crossed and seeing as what might be implemented in the next build,I'll not get greedy. :D

Re: [Script] Path Parser

Posted: May 30th, 2017, 7:03 pm
by StArL0rd84
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?

Re: [Script] Path Parser

Posted: May 30th, 2017, 7:35 pm
by balala
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.