/// <summary> /// Replaces a tag (such as "%year%") with an appropriate value (such as "2020"). /// </summary> /// <param name="preview">Determines if this is a preview of a macro. We either use screen capture date/time or DateTime.Now depending on this boolean.</param> /// <param name="name">The name of a region or screen when parsing the %name% tag.</param> /// <param name="macro">The macro to parse. A macro usually includes tags such as %count% and %date%.</param> /// <param name="screenNumber">The screen number. For example, if this is the second display then the screen number is 2.</param> /// <param name="format">The image format to use as an image file extension when parsing the %format% tag.</param> /// <param name="activeWindowTitle">The title of the active window.</param> /// <param name="tag">The macro tag to use during parsing.</param> /// <returns>A parsed macro containing the appropriate values of respective tags in the provided macro.</returns> private static string ParseTag(bool preview, string name, string macro, int screenNumber, ImageFormat format, string activeWindowTitle, Tag tag) { int count; DateTime dt; if (preview || screenCapture == null) { count = 1; dt = DateTime.Now; } else { count = screenCapture.Count; dt = screenCapture.DateTimeScreenshotsTaken; } // Strip out any backslash characters from the name so we avoid creating unnecessary directories based on the name. name = name.Replace("\\", string.Empty); if (!tag.Active) { return(macro); } switch (tag.Type) { case TagType.ActiveWindowTitle: macro = macro.Replace(tag.Name, activeWindowTitle); break; case TagType.DateTimeFormat: macro = macro.Replace(tag.Name, dt.ToString(tag.DateTimeFormatValue)); break; case TagType.ImageFormat: macro = format != null && !string.IsNullOrEmpty(format.Name) ? macro.Replace(tag.Name, format.Name.ToLower()) : macro; break; case TagType.ScreenCaptureCycleCount: macro = macro.Replace(tag.Name, count.ToString()); break; case TagType.ScreenName: macro = !string.IsNullOrEmpty(name) ? macro.Replace(tag.Name, name) : macro; break; case TagType.ScreenNumber: macro = macro.Replace(tag.Name, screenNumber.ToString()); break; case TagType.User: macro = macro.Replace(tag.Name, Environment.UserName); break; case TagType.Machine: macro = macro.Replace(tag.Name, Environment.MachineName); break; case TagType.DateTimeFormatExpression: macro = macro.Replace(tag.Name, MacroTagExpressionParser.ParseTagExpressionForDateTimeFormat(dt, tag.DateTimeFormatValue)); break; } return(StripInvalidWindowsCharacters(macro)); }
/// <summary> /// Replaces a tag (such as "%year%") with an appropriate value (such as "2020"). /// </summary> /// <param name="preview">Determines if this is a preview of a macro. We either use screen capture date/time or DateTime.Now depending on this boolean.</param> /// <param name="name">The name of a region or screen when parsing the %name% tag.</param> /// <param name="macro">The macro to parse. A macro usually includes tags such as %count% and %date%.</param> /// <param name="screenNumber">The screen number. For example, if this is the second display then the screen number is 2.</param> /// <param name="x">The current X value.</param> /// <param name="y">The current Y value.</param> /// <param name="width">The current Width value.</param> /// <param name="height">The current Height value.</param> /// <param name="format">The image format to use as an image file extension when parsing the %format% tag.</param> /// <param name="activeWindowTitle">The title of the active window.</param> /// <param name="processName">The name of the current process.</param> /// <param name="label">The label that is applied to the saved screenshot.</param> /// <param name="tag">The macro tag to use during parsing.</param> /// <returns>A parsed macro containing the appropriate values of respective tags in the provided macro.</returns> private string ParseTag(bool preview, string name, string macro, int screenNumber, int x, int y, int width, int height, ImageFormat format, string activeWindowTitle, string processName, string label, MacroTag tag) { int count; DateTime dt; if (preview || screenCapture == null) { count = 1; dt = DateTime.Now; } else { count = screenCapture.CycleCount; dt = screenCapture.DateTimeScreenshotsTaken; } // Strip out any backslash characters from the name so we avoid creating unnecessary directories based on the name. name = name.Replace("\\", string.Empty); if (!tag.Enable) { return(macro); } switch (tag.Type) { case MacroTagType.ActiveWindowTitle: macro = macro.Replace(tag.Name, activeWindowTitle); break; case MacroTagType.DateTimeFormat: macro = macro.Replace(tag.Name, dt.ToString(tag.DateTimeFormatValue)); break; case MacroTagType.ImageFormat: macro = format != null && !string.IsNullOrEmpty(format.Name) ? macro.Replace(tag.Name, format.Name.ToLower()) : macro; break; case MacroTagType.ScreenCaptureCycleCount: macro = macro.Replace(tag.Name, count.ToString()); break; case MacroTagType.ScreenName: macro = !string.IsNullOrEmpty(name) ? macro.Replace(tag.Name, name) : macro; break; case MacroTagType.ScreenNumber: macro = macro.Replace(tag.Name, screenNumber.ToString()); break; case MacroTagType.User: macro = macro.Replace(tag.Name, Environment.UserName); break; case MacroTagType.Machine: macro = macro.Replace(tag.Name, Environment.MachineName); break; case MacroTagType.DateTimeFormatExpression: macro = macro.Replace(tag.Name, _macroTagExpressionParser.ParseTagExpressionForDateTimeFormat(dt, tag.DateTimeFormatValue, this)); break; case MacroTagType.QuarterYear: macro = macro.Replace(tag.Name, ((dt.Month - 1) / 3 + 1).ToString()); break; case MacroTagType.X: macro = macro.Replace(tag.Name, x.ToString()); break; case MacroTagType.Y: macro = macro.Replace(tag.Name, y.ToString()); break; case MacroTagType.Width: macro = macro.Replace(tag.Name, width.ToString()); break; case MacroTagType.Height: macro = macro.Replace(tag.Name, height.ToString()); break; case MacroTagType.Process: macro = macro.Replace(tag.Name, processName); break; case MacroTagType.Label: macro = macro.Replace(tag.Name, label); break; case MacroTagType.CaptureNowCount: macro = macro.Replace(tag.Name, screenCapture == null ? "0" : screenCapture.CaptureNowCount.ToString()); break; } // If we encounter "$AppDataLocal$" then replace it with the user's local AppData directory. // This is used by the installer in case the user puts the application in the "Program Files" folder // where we can't write any files to since we don't have permissions to write files in that folder // unless the application is running as Administrator so for it to work for normal users we have to // make sure that we write all the necessary XML files to the user's local AppData folder. macro = macro.Replace("$AppDataLocal$", Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)); // For roaming profiles. macro = macro.Replace("$AppDataRoaming$", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)); // The user's Desktop. macro = macro.Replace("$Desktop$", Environment.GetFolderPath(Environment.SpecialFolder.Desktop)); // The user's "My Documents" folder. macro = macro.Replace("$MyDocuments$", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)); return(StripInvalidWindowsCharacters(macro)); }