Documentation
Plugins
Plugins add your own commands. They are small C# files, compiled automatically when MacroFoundry starts.
Using plugins#
Plugins add extra commands. Put a plugin's .dll or .cs file in the Plugins folder next to MacroFoundry.exe, then restart MacroFoundry. Its commands appear under + Add command.
Help > Plugins shows which plugins loaded and any problems, and can open the Plugins folder.
MacroFoundry comes with an example, SamplePlugin.cs, which adds Save Date/Time (puts the date and/or time into a Text variable) and Write Line to File (adds a line of text to a file).
Only use plugins from people you trust: a plugin is a program and can do anything a program can.
Writing a plugin#
Plugins are written in C#. A plugin is a class that implements IMacroFoundryPlugin (from MacroFoundry.Core), with a Name and a Register(PluginApi api) method.
In Register, call api.AddCommand(type, category, title, description, run) for each command. Then add settings with .Text, .Number, .Variable, .Checkbox, .Choice and .File, and optionally .Summary for the text shown in the command list.
The run function gets a context with Get (a setting's text, with {Variable} filled in), GetNumber, GetBool, SetVariable, GetVariable, Log and Sleep. Return null when it worked, or a message to show as a warning.
A .cs file is compiled automatically when MacroFoundry starts. See SamplePlugin.cs in the plugins folder for a complete example.
The sample plugin#
This is the complete SamplePlugin.cs that ships in the Plugins folder. It adds two commands, Save Date/Time and Write Line to File. Copy it, rename the class and change the commands to make your own.
// Sample MacroFoundry plugin. MacroFoundry compiles .cs files in its Plugins folder when it starts,
// so you can add commands by dropping a file like this one there. A compiled .dll works too.
using System;
using System.IO;
using MacroFoundry.Core;
public class SamplePlugin : IMacroFoundryPlugin
{
public string Name { get { return "Sample plugin"; } }
public void Register(PluginApi api)
{
api.AddCommand("Sample.SaveDateTime", "Plugins", "Save Date/Time", "Puts the current date and/or time into a Text variable.", ctx =>
{
string fmt;
switch (ctx.Get("Format"))
{
case "Date": fmt = "yyyy-MM-dd"; break;
case "Time": fmt = "HH:mm:ss"; break;
default: fmt = "yyyy-MM-dd HH:mm:ss"; break;
}
return ctx.SetVariable(ctx.Get("Variable"), DateTime.Now.ToString(fmt));
})
.Variable("Variable", "Save in", "A Text variable.")
.Choice("Format", "What", new[] { "Date and time", "Date", "Time" }, null)
.Summary(c => "Save " + c.Get("Format").ToLowerInvariant() + " in " + c.Get("Variable"));
api.AddCommand("Sample.AppendLine", "Plugins", "Write Line to File", "Adds a line of text to the end of a text file (creates the file if needed). Handy for keeping a log of what a routine did.", ctx =>
{
string path = ctx.Get("Path").Trim().Trim('"');
if (path.Length == 0) return "No file chosen.";
File.AppendAllText(path, ctx.Get("Line") + Environment.NewLine);
return null;
})
.File("Path", "File", "Text files (*.txt;*.csv;*.log)|*.txt;*.csv;*.log", "The file to add the line to.")
.Text("Line", "Line", "", "Use {VariableName} to include a variable's value.")
.Summary(c => "Write \"" + c.Get("Line") + "\" to " + Path.GetFileName(c.Get("Path")));
}
}