It is currently March 28th, 2024, 10:54 am

My First attempt

Discuss the use of Lua in Script measures.
Post Reply
User avatar
CyberTheWorm
Posts: 860
Joined: August 22nd, 2016, 11:32 pm
Location: Surrey, B.C., Canada

My First attempt

Post by CyberTheWorm »

So I decided to try LUA script any comments about what I did would be appreciated.
I'm trying to get the Julian date for Astronomy to display, not sure if I did it correctly or if there is a better way.
jd.PNG
Time.ini

Code: Select all

[Rainmeter]
Update=1000

[Metadata]
Author=CyberTheWorm
Version=1.0.0
License=Creative Commons Attribution-Non-Commercial-Share Alike 3.0
Information=Julian Date Test

;-------------------- Variables ----------------------
[Variables]
MyFontSize=15
MyFontColor=255,255,255,255
MyEffect=Shadow
MyEffectColor=0,0,0,255
;-------------------- Measures ----------------------------
[MeasureTime]
Measure=Time
Format=%T, %Z

[MeasureJDScript]
Measure=Script
ScriptFile=Julian.lua
Disabled=1
;--------------------- Meters --------------------------------
[MeterTime]
Meter=String
MeasureName=MeasureTime
AntiAlias=1
FontFace=#MyFont#
FontSize=#MyFontSize#
FontColor=#MyFontColor#
StringEffect=#MyEffect#
FontEffectColor=#MyEffectColor#
Text=%1

[MeterJD]
Meter=String
AntiAlias=1
Y=20
FontFace=#MyFont#
FontSize=#MyFontSize#
FontColor=#MyFontColor#
StringEffect=#MyEffect#
FontEffectColor=#MyEffectColor#
Text=Julian Date: [&MeasureJDScript:GetJulian()]
DynamicVariables=1
Julian.lua

Code: Select all

function GetJulian()

	Year = tonumber(os.date('%Y'))
	Month = tonumber(os.date('%m'))
	Day = tonumber(os.date('%e'))
	Hour = tonumber(os.date('%H'))
	Min = tonumber(os.date('%M'))
	Sec = tonumber(os.date('%S'))
	
	JDN = (1461 * (Year + 4800 + (Month - 14)/12))/4 +(367 * (Month - 2 - 12 * ((Month - 14)/12)))/12 - (3 * ((Year + 4900 + (Month - 14)/12)/100))/4 + Day - 32075
	
	JD = JDN + (Hour -12)/24 + Min/1440 + Sec/86400
	
	return JD
	
end
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: My First attempt

Post by jsmorley »

It's quite good. I really like the use of Inline Lua.

Inline Lua is one of the better things brian has done all year. It is just staggeringly useful, and beautiful in its simplicity.

I do think you can simplify your code just a hair, avoiding the work of doing all those tonumber() functions.

Julian.lua:

Code: Select all

function GetJulian()

   local now = os.date('*t')
   
   local JDN = (1461 * (now.year + 4800 + (now.month - 14)/12))/4 +(367 * (now.month - 2 - 12 * ((now.month - 14)/12)))/12 - (3 * ((now.year + 4900 + (now.month - 14)/12)/100))/4 + now.day - 32075
   
   local JD = JDN + (now.hour -12)/24 + now.min/1440 + now.sec/86400

   return JD
   
end
os.date('*t') creates a table, using the current local time, that is automatically populated with the fields:

year full year
month 01-12
day 01-31
hour 00-23
min 00-59
sec 00-59
isdst a boolean, true if daylight saving

when you create this table, you can reference the values in it with those field index names.

now = os.date('*t')
print(now.year)

Returns 2018

The syntax now.year is just Lua "shorthand" for now['year'].

As an aside, now = os.date('*!t') would return the same table, but in current UTC time.

I don't know much about Julian dates, and am assuming your formula is correct, so for me, your skin with my code produces:
1.png
User avatar
CyberTheWorm
Posts: 860
Joined: August 22nd, 2016, 11:32 pm
Location: Surrey, B.C., Canada

Re: My First attempt

Post by CyberTheWorm »

jsmorley wrote:It's quite good. I really like the use of Inline Lua.

Inline Lua is one of the better things brian has done all year. It is just staggeringly useful, and beautiful in its simplicity.

I do think you can simplify your code just a hair, avoiding the work of doing all those tonumber() functions.

Julian.lua:

Code: Select all

function GetJulian()

   local now = os.date('*t')
   
   local JDN = (1461 * (now.year + 4800 + (now.month - 14)/12))/4 +(367 * (now.month - 2 - 12 * ((now.month - 14)/12)))/12 - (3 * ((now.year + 4900 + (now.month - 14)/12)/100))/4 + now.day - 32075
   
   local JD = JDN + (now.hour -12)/24 + now.min/1440 + now.sec/86400

   return JD
   
end
os.date('*t') creates a table, using the current local time, that is automatically populated with the fields:

year full year
month 01-12
day 01-31
hour 00-23
min 00-59
sec 00-59
isdst a boolean, true if daylight saving

when you create this table, you can reference the values in it with those field index names.

now = os.date('*t')
print(now.year)

Returns 2018

The syntax now.year is just Lua "shorthand" for now['year'].

As an aside, now = os.date('*!t') would return the same table, but in current UTC time.
Nice, thanks for the info on the table that does clean it up a lot. I did find that the UTC time should be

Code: Select all

local now = os.date('!*t')
User avatar
CyberTheWorm
Posts: 860
Joined: August 22nd, 2016, 11:32 pm
Location: Surrey, B.C., Canada

Re: My First attempt

Post by CyberTheWorm »

OK I have a question about getting the data from the lua script.

I want to be able to get the value from that script into a variable and then pass it to another script.

jsmorley can you point me in the right direction? I have been looking but am getting stumped.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: My First attempt

Post by jsmorley »

CyberTheWorm wrote:OK I have a question about getting the data from the lua script.

I want to be able to get the value from that script into a variable and then pass it to another script.

jsmorley can you point me in the right direction? I have been looking but am getting stumped.
Not sure I follow...
User avatar
CyberTheWorm
Posts: 860
Joined: August 22nd, 2016, 11:32 pm
Location: Surrey, B.C., Canada

Re: My First attempt

Post by CyberTheWorm »

jsmorley wrote:Not sure I follow...
OK I have the script to calculate the Julian date, I want to get that number into a variable in the main rainmeter.ini so that I can send that number to another script which uses that number in a different lua script to calculate a different value.

If that makes more sense. it's either that or I have to make a very big lua script to calculate what I want.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: My First attempt

Post by jsmorley »

Well instead of using

Text=Julian Date: [&MeasureJDScript:GetJulian()]

on a String meter, you might consider using

String=[&MeasureJDScript:GetJulian()]

on a String measure, or

Formula=[&MeasureJDScript:GetJulian()]

on a Calc measure.

Then you can use the value of that String or Calc measure with MeasureName= on the String meter, and also use it anywhere else you need it.

I'm not sure I see the charm of two .lua files and two Script measures in the skin. I would be tempted to indeed have it all hosted by one Script measure. You can separate the functionality into two .lua files if it makes it easier to maintain, and just combine them with dofile() in your main Lua script.

But I feel like I'm not visualizing what you are getting at correctly. This feels to me like it might be complicating something that should be pretty straightforward.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: My First attempt

Post by jsmorley »

Inline Lua section variables can be used just as #Variables# are, and can even be nested...

Skin:

Code: Select all

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

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

[MeterOne]
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:MySecondFunction([&Lua:MyFirstFunction(150)])]
DynamicVariables=1
Lua:

Code: Select all

function MyFirstFunction(inArg)

	return inArg + 50
   
end

function MySecondFunction(inArg)

	return inArg + 100
	
end
1.png
Nesting is resolved "from the inside out", so this will work fine and return the expected 300.
User avatar
CyberTheWorm
Posts: 860
Joined: August 22nd, 2016, 11:32 pm
Location: Surrey, B.C., Canada

Re: My First attempt

Post by CyberTheWorm »

Thanks, I play and see what I come up with.
Should be interesting if I get it working :17good
Post Reply