// Starts a new macro in record mode public static void StartNew() { CurrentMacro = new Macro { IsRecording = true }; }
// Loads the current macro from a file public static void LoadFromFile(string filename) { CurrentMacro = new Macro { Commands = XmlHelpers.Deserialize<List<MacroCommand>>(File.ReadAllText(filename)) }; }
// Saves the current macro to a file public static void SaveToFile(Macro macro, string filename) { File.WriteAllText(filename, XmlHelpers.Serialize(macro.Commands)); }
// Replays a macro private void Playback(Macro macro, int times = 1) { var pvaIn = Marshal.AllocCoTaskMem(16); try { for (int i = 0; i < times; i++) { foreach (var command in macro.Commands) { var pguidCmdGroup = command.CommandGroup; if (command.Character != null) { Marshal.GetNativeVariantForObject((ushort)command.Character, pvaIn); Next.Exec(ref pguidCmdGroup, command.CommandID, command.CommandOptions, pvaIn, IntPtr.Zero); } else Next.Exec(ref pguidCmdGroup, command.CommandID, command.CommandOptions, IntPtr.Zero, IntPtr.Zero); } } } finally { if (pvaIn != IntPtr.Zero) Marshal.FreeCoTaskMem(pvaIn); } }
// Saves the given macro under the given name public static void SaveMacro(Macro macro, string name, string guid = null) { // Get the list var list = GetMacroList(); // Update the list item or add a new one if (guid != null && list.Any(x => x.Guid == guid)) list.SingleOrDefault(x => x.Guid == guid).Name = name; else list.Add(new SavedMacro(guid = Guid.NewGuid().ToString(), name)); // Save the macro Macro.SaveToFile(macro, GetMacroFilename(guid)); // Update the list SaveMacroList(list); }
// Shows the save macro dialog private void ShowSaveMacroDialog(Macro macro, string name = null) { var dialog = new SaveMacroDialog(name); if (dialog.ShowDialog() == DialogResult.OK) SavedMacros.SaveMacro(macro, dialog.MacroName); }