It is currently June 27th, 2019, 11:10 am

Parsing file name from path of obscure file type

Help with creating, editing & fixing problems with skins
User avatar
StArL0rd84
Posts: 335
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Parsing file name from path of obscure file type

StArL0rd84 » April 7th, 2019, 12:10 pm

The path i am trying to parse is a s follows:
Material Theme/schemes/Material Theme Palenight.tmTheme
And all i need is the file name.

Iv'e got two kinds of lua scripts i am testing with.
One i found in the Rainmeter documentation, and i cannot remember where i got the other one from.

From Rainmeter documentation:

Code: Select all

function ParseFilePath(s)
	assert(type(s) == 'string', 'ParseFilePath: string expected, got ' .. type(s) .. '.')
	local t = {}

	for Piece in s:gmatch('[^\\]+') do
		table.insert(t, Piece)
	end

	-- VOLUME
	if #t > 0 then
		local UNCprefix = s:match('^(\\\\)') or ''
		t.Vol = UNCprefix .. t[1] .. '\\'
	end

	-- NAME
	if #t > 1 then
		-- DETECT FOLDER OR SEPARATE EXTENSION
		if s:match('\\$') then
			t.Name = t[#t] .. '\\'
		elseif t[#t]:match('.+%..+') then
			t.Name, t.Ext = t[#t]:match('(.-)%.([^%.]-)$')
		else
			t.Name = t[#t]
		end
	end

	-- FOLDER
	if #t > 2 then
		t.Dir = table.concat(t, '\\', 2, #t-1) .. '\\'
	end

	return t
end
Mystery one:

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

   sDrive, sDir, sName, sExt = string.match(sPath, "(.):\\(.-)([^\\]-)%.([^%.]+)$")
   sNameExt = sName..'.'..sExt

   SKIN:Bang('!SetVariable "'..sVariablePrefix..'Drive" "'..sDrive..'"')
   SKIN:Bang('!SetVariable "'..sVariablePrefix..'Dir" "'..sDir..'"')
   SKIN:Bang('!SetVariable "'..sVariablePrefix..'Name" "'..sName..'"')
   
   if sOutput == 'Drive' then
      return sDrive
   elseif 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
The mystery one has worked for me in the past with normal files.
But does not seem to work with the .tmTheme file extention.

Code: Select all

[MeasureScript]
Measure=Script
ScriptFile=#@#lua\PathParser.lua
DynamicVariables=1
Input=C:\Program Files\Sublime Text 3\sublime_text.exe
Output=Name
And i am unclear on how to use the lua script from the Rainmeter documentation.

Code: Select all

[MeasureScript1]
Measure=Script
ScriptFile=#@#Lua\FilePathParser.lua
ParseFilePath=C:\Program Files\Sublime Text 3\sublime_text.exe]
Don't know if the scripts needs the actual file to parse the path or not.
But all i got is the path you see in the top of this post.

Maybe RegExpSubstitute is the answer in stead of lua?
Last edited by StArL0rd84 on April 7th, 2019, 1:22 pm, edited 1 time in total.
(#WorkTime# = 1 ? #Work# : ([mEnergyLoss:%] > 100 ? #SleepMode# : #Rainmeter#))
User avatar
jsmorley
Developer
Posts: 19273
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Parsing file name from path of obscure file type

jsmorley » April 7th, 2019, 12:21 pm

Both of the scripts are looking for the standard Windows form of path, which uses \ as the separator. Your path, Material Theme/schemes/Material Theme Palenight.tmTheme is using the Unix / character.

Hang on a bit and I will see how best to address this, to make it unconcerned with the form of the separator.
User avatar
StArL0rd84
Posts: 335
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Re: Parsing file name from path of obscure file type

StArL0rd84 » April 7th, 2019, 12:34 pm

jsmorley wrote:
April 7th, 2019, 12:21 pm
Both of the scripts are looking for the standard Windows form of path, which uses \ as the separator. Your path, Material Theme/schemes/Material Theme Palenight.tmTheme is using the Unix / character.

Hang on a bit and I will see how best to address this, to make it unconcerned with the form of the separator.
Found a way to trick it:
C:\Program Files\Material Theme\schemes\Material-Theme-Palenight.tmTheme
Added C:\Program Files\ in front of it, and flipped the separators around with a substitute.

But yeah, would be cleaner if the script had a way to discern this internally.

And BTW. i could only get the mystery script to work with this workaround.
(#WorkTime# = 1 ? #Work# : ([mEnergyLoss:%] > 100 ? #SleepMode# : #Rainmeter#))
User avatar
jsmorley
Developer
Posts: 19273
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Parsing file name from path of obscure file type

jsmorley » April 7th, 2019, 1:04 pm

Here is a way to do it in Lua:

Skin:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Variables]
MyPath=Material Theme/schemes/Material Theme Palenight.tmTheme

[Lua]
Measure=Script
ScriptFile=#CURRENTPATH#Test.lua
Disabled=1

[MeterPathOnly]
Meter=String
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=[&Lua:GetPath('#MyPath#')]
DynamicVariables=1

[MeterFileNameOnly]
Meter=String
Y=5R
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=[&Lua:GetFilename('#MyPath#')]
DynamicVariables=1
Test.lua:

Code: Select all

function GetPath(str)
	return str:match('(.*[/\\]).*')
end

function GetFilename(str)
	return str:match('.*[/\\](.*)')
end

1.png


Should work with any path and file name, with or without drive letters, with or without any file extension, and with either separator.

MyPath=C:\Users\Jeffrey\Documents\Rainmeter\Skins\illustro\Network\Network.ini

2.png
You do not have the required permissions to view the files attached to this post.
User avatar
StArL0rd84
Posts: 335
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Re: Parsing file name from path of obscure file type

StArL0rd84 » April 7th, 2019, 1:22 pm

Yep that worked wonderfully, clean solution.
Thanks J

Code: Select all

[mMainScheme]
Measure=WebParser
Url=file://#MainTheme&Scheme#
RegExp=(?siU)"color_scheme": "Packages/(.*).tmTheme",
UpdateRate=5
StringIndex=1
Substitute="-":" "

[mMainColorSchemeLua]
Measure=Script
ScriptFile=#@#lua\tmThemeFilePathParser.lua
Disabled=1

[ColorScheme]
Meter=String
MeterStyle=TextStyle
MeasureName=mMainColorScheme
Text=Color Scheme: #CRLF#[&mMainColorSchemeLua:GetFilename('[&mMainScheme]')]
X=r
Y=60r
SolidColor=0,0,0
(#WorkTime# = 1 ? #Work# : ([mEnergyLoss:%] > 100 ? #SleepMode# : #Rainmeter#))
User avatar
jsmorley
Developer
Posts: 19273
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Parsing file name from path of obscure file type

jsmorley » April 7th, 2019, 1:26 pm

Glad to help. I would imagine there is also a RegExpSubstitute solution for this, probably not at all dissimilar to the Lua pattern matching, but to be honest, I'd prefer the Lua, as is easy to re-use in many places, and can just be added to your toolkit of stuff. In addition, the RegExpSubstitute solution is going to require some "measure" to put the Substitute on in any case, so you aren't saving anything over having the simple Lua measure, and you get to keep the original string value, be it from a variable or a measure, rather than "changing" it.

And it lets you do clever stuff like:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Variables]
MyPath=C:\Users\Jeffrey\Documents\Rainmeter\Skins\illustro\Network\Network.ini

[Lua]
Measure=Script
ScriptFile=#CURRENTPATH#Test.lua
Disabled=1

[MeterPath]
Meter=String
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=#MyPath#
InlineSetting=Size | 14
InlinePattern=[&Lua:GetFilename('#MyPath#')]
InlineSetting2=Color | 188,255,212,255
InlinePattern2=[&Lua:GetFilename('#MyPath#')]
DynamicVariables=1

1.png


InlineSetting2=GradientColor | 180 | 207,250,213,255 ; 0.1 | 255,222,161,255 ; 0.5 | 240,108,95,255 ; 1.0


2.png



I've said it before, and I will say it again. Inline Lua is one of the best things we ever added to Rainmeter. One very simple disabled "host" Script measure can be used to do an unlimited number of things in an unlimited number of places, and is just crazy flexible.
You do not have the required permissions to view the files attached to this post.
User avatar
jsmorley
Developer
Posts: 19273
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Parsing file name from path of obscure file type

jsmorley » April 7th, 2019, 3:07 pm

Here is a slightly more robust version:

Skin:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Variables]
MyPath=C:\Users\Jeffrey\Documents\Rainmeter\Skins\illustro\Network\Network.ini

[Lua]
Measure=Script
ScriptFile=#CURRENTPATH#Test.lua
Disabled=1

[MeterPath]
Meter=String
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=Full String: #MyPath##CRLF#Path: [&Lua:SplitPath('#MyPath#','path')]#CRLF#Filename: [&Lua:SplitPath('#MyPath#','file')]#CRLF#Extension: [&Lua:SplitPath('#MyPath#','ext')]
DynamicVariables=1
Test.lua:

Code: Select all

function SplitPath(pathArg, pathBit)

	path, file, ext = string.match(pathArg, '(.-)([^\\/]-%.?([^%.\\/]*))$')

	if string.upper(pathBit) == 'PATH' then
		return path
	elseif string.upper(pathBit) == 'FILE' then
		return file
	elseif string.upper(pathBit) == 'EXT' then
		return ext
	else
		return 'Invalid component'		
	end

end

1.png
You do not have the required permissions to view the files attached to this post.
User avatar
StArL0rd84
Posts: 335
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Re: Parsing file name from path of obscure file type

StArL0rd84 » April 7th, 2019, 3:30 pm

Image
(#WorkTime# = 1 ? #Work# : ([mEnergyLoss:%] > 100 ? #SleepMode# : #Rainmeter#))