Scripting syntax - basics

Forum dedicated to INDIGO Script know-how exchange
Post Reply
JohanB
Posts: 268
Joined: Thu Apr 21, 2016 5:55 pm

Scripting syntax - basics

Post by JohanB »

Hi all,

Was having a lot of trouble finding out the script syntax for the basic stuff and could not find easy to read documentation. So i put some time into getting some basic functions to work, reading different documentation sources and with a lot of trail and error. Thought it would be good to share, to save you the time. Not sure if there are easier or better ways of doing the same (there probably are...), but hey, at leat i got the following to work ;-)

indigo_log("------ Math, String and Date examples ------");
var x = 0;
var y = 0;
var s = "";
var d;

x = -1.05;
y = Math.abs(x);
indigo_log("abs(" + x + ") = " + y); // Makes positive

x = 1.55;
y = Math.floor(x);
indigo_log("floor(" + x + ") = " + y); // Rounds down

x = 1.55;
y = Math.round(x);
indigo_log("round(" + x + ") = " + y); // Rounds up from .5

y = Math.PI;
indigo_log("Pi = " + y); // Pi

x = 1.05;
y = Math.cos(x);
indigo_log("cos(" + x + ") = " + y); // Co-Sine

x = 1.05;
indigo_log ("Number to string - function " + String(x)); // Converts nbr to string with function
indigo_log ("Number to string - method " + (x).toString()); // Converts nbr to string with method of number
indigo_log ("Number to string - fixed " + (x).toFixed(3)); // Converts nbr to string, with 3 decimals

s = "1.05";
y = Number(s) * 3;
indigo_log ("String to number " + y); // Converts string to number, double (!)

s = "1.05";
y = (Number(s) * 3).toFixed(2); // Converts to number and back to string, with fixed decimals
indigo_log ("String to number " + y);

s = "TESTING";
indigo_log ("Length of " + s + " = " + s.length); // String length

s = "TESTING";
indigo_log ("Left three chars of " + s + " are " + s.substr(0,3)); // Substring left

s = "TESTING";
indigo_log ("Left right chars of " + s + " are " + s.substr((s.length -3),3)); // Substring right

d = new Date(1 * 1 * 1 * 1 * 1 * 1).toString(); // New date object from ms (Y * Days/y * H * M * S * ms) (1 ms since 1-1-1970 00:00)
indigo_log ("Date object - string (1) -> " + d);

d = new Date(2009,11,14,1); // New date object from constructor; 2009 year, + 11 full months, + 14 full days, + 1H
indigo_log ("Date object - string (2) -> " + d);

d = new Date(51 * 365 * 24 * 60 * 60 * 1000).toISOString(); // Same in ISO string format (is not accounting for leap years)
indigo_log ("Date object - ISO string -> " + d);

d = new Date(); // Current system date and time
indigo_log ("Current time of day -> " + d);

Produces the following output:

Jan 17 13:18:26 ZGAstroScope indiboot[2310]: 13:18:26.509259 indigo_server: ------ Math, String and Date examples ------
Jan 17 13:18:26 ZGAstroScope indiboot[2310]: 13:18:26.509427 indigo_server: abs(-1.05) = 1.05
Jan 17 13:18:26 ZGAstroScope indiboot[2310]: 13:18:26.509507 indigo_server: floor(1.55) = 1
Jan 17 13:18:26 ZGAstroScope indiboot[2310]: 13:18:26.509580 indigo_server: round(1.55) = 2
Jan 17 13:18:26 ZGAstroScope indiboot[2310]: 13:18:26.509658 indigo_server: Pi = 3.141592653589793
Jan 17 13:18:26 ZGAstroScope indiboot[2310]: 13:18:26.509752 indigo_server: cos(1.05) = 0.49757104789172696
Jan 17 13:18:26 ZGAstroScope indiboot[2310]: 13:18:26.509815 indigo_server: Number to string - function 1.05
Jan 17 13:18:26 ZGAstroScope indiboot[2310]: 13:18:26.509876 indigo_server: Number to string - method 1.05
Jan 17 13:18:26 ZGAstroScope indiboot[2310]: 13:18:26.509938 indigo_server: Number to string - fixed 1.050
Jan 17 13:18:26 ZGAstroScope indiboot[2310]: 13:18:26.510063 indigo_server: String to number 3.1500000000000004
Jan 17 13:18:26 ZGAstroScope indiboot[2310]: 13:18:26.510179 indigo_server: String to number 3.15
Jan 17 13:18:26 ZGAstroScope indiboot[2310]: 13:18:26.510241 indigo_server: Length of TESTING = 7
Jan 17 13:18:26 ZGAstroScope indiboot[2310]: 13:18:26.510306 indigo_server: Left three chars of TESTING are TES
Jan 17 13:18:26 ZGAstroScope indiboot[2310]: 13:18:26.510370 indigo_server: Left right chars of TESTING are ING
Jan 17 13:18:26 ZGAstroScope indiboot[2310]: 13:18:26.510502 indigo_server: Date object - string (1) -> 1970-01-01 01:00:00.001+01:00
Jan 17 13:18:26 ZGAstroScope indiboot[2310]: 13:18:26.510709 indigo_server: Date object - string (2) -> 2009-12-14 01:00:00.000+01:00
Jan 17 13:18:26 ZGAstroScope indiboot[2310]: 13:18:26.510783 indigo_server: Date object - ISO string -> 2020-12-19T00:00:00.000Z
Jan 17 13:18:26 ZGAstroScope indiboot[2310]: 13:18:26.510907 indigo_server: Current time of day -> 2021-01-17 13:18:26.510+01:00

Regards, Johan
Post Reply