Font parameter (@param font)
The font parameter type makes the parameter dialog open the standard Windows font picker. The user chooses typeface, size and style (bold/italic/underline); the script receives a ready-made font and applies it to text — to a whole shape or only partially (e.g. size only).
Companion to color
@param font is the font counterpart to @param color (color picker). Color is deliberately not carried along — use a separate @param color for that. See section Combining font and color.
Syntax
// @param font <name> "<Label>" [default="Name,Size,Style"] [tooltip="…"]
- default — initial value as
FontName,Size,Style. Size in points; style optional:Bold,Italic,Underline,Strikeout(several separated by spaces). Examples:default="Segoe UI,24,Bold"·default="Arial,32"·default="Calibri,18,Bold Italic" - In the script,
Params.GetFont("<name>")returns aSystem.Drawing.FontwithName,Size(points),Bold,Italic,Underline.
Apply the whole formatting
Applies all properties of the chosen font to the text of every selected shape. The helper ppptools.SetFont(shape, font) sets name, size, bold, italic and underline in one call:
// @param font schrift "Font" default="Segoe UI,24,Bold" tooltip="Opens the Windows font dialog"
// @button Apply
var f = Params.GetFont("schrift");
var sel = ppptools.GetSelectedRange();
for (int i = 1; i <= sel.Count; i++)
ppptools.SetFont(sel[i], f);
When run, the parameter dialog shows the "Font" field with a button displaying the current choice (e.g. Segoe UI, 24 pt Bold). Clicking it opens the Windows font dialog.
Apply only parts (e.g. size only)
You don't have to apply the whole font — read individual properties. Here typeface and style stay unchanged, only the size from the dialog is applied:
// @param font gr "Choose font size" default="Arial,32"
// @button Set size
var f = Params.GetFont("gr");
var sel = ppptools.GetSelectedRange();
for (int i = 1; i <= sel.Count; i++)
sel[i].TextFrame.TextRange.Font.Size = f.Size; // size only
Likewise for individual properties:
| Property | Apply |
|---|---|
| Typeface | …Font.Name = f.Name; |
| Size | …Font.Size = f.Size; |
| Bold | …Font.Bold = f.Bold ? MsoTriState.msoTrue : MsoTriState.msoFalse; |
| Italic | …Font.Italic = f.Italic ? MsoTriState.msoTrue : MsoTriState.msoFalse; |
| Underline | …Font.Underline = f.Underline ? MsoTriState.msoTrue : MsoTriState.msoFalse; |
Text runs instead of whole shape
sel[i].TextFrame.TextRange is the entire text. For a portion, use TextRange.Runs(start, length) or .Paragraphs(n) and set .Font there.
Combining font and color
In PPPTools the Windows font dialog carries no color — the color picker is deliberately hidden in the dialog, and a System.Drawing.Font holds no color anyway. So Params.GetFont(...) never returns a color.
If a snippet should set font and color, combine @param font with a separate @param color:
// @param string SearchString "Search term" tooltip="Text to format"
// @param bool CaseSensitive "Match case?" tooltip="o ≠ O, type ≠ Type"
// @param font FormatFont "Font" default="Calibri,18,Bold"
// @param color FormatColor "Color" default="#FF0000"
// @button Apply
string searchString = Params.GetString("SearchString", "");
bool caseSensitive = Params.GetBool("CaseSensitive", false);
var fmt = Params.GetFont("FormatFont");
var fmtColor = Params.GetColor("FormatColor");
MsoTriState matchCase = caseSensitive ? MsoTriState.msoTrue : MsoTriState.msoFalse;
foreach (dynamic oShape in ppptools.GetAllShapes())
{
if ((int)oShape.HasTextFrame != (int)MsoTriState.msoTrue) continue;
dynamic tr = oShape.TextFrame.TextRange;
dynamic found = tr.Find(searchString, 0, matchCase, MsoTriState.msoFalse);
while (found != null)
{
// Font from the font dialog …
found.Font.Name = fmt.Name;
found.Font.Size = fmt.SizeInPoints;
found.Font.Bold = fmt.Bold ? MsoTriState.msoTrue : MsoTriState.msoFalse;
found.Font.Italic = fmt.Italic ? MsoTriState.msoTrue : MsoTriState.msoFalse;
found.Font.Underline = fmt.Underline ? MsoTriState.msoTrue : MsoTriState.msoFalse;
// … color from the separate color picker
found.Font.Color.RGB = System.Drawing.ColorTranslator.ToOle(fmtColor);
int after = found.Start + found.Length - 1;
found = tr.Find(searchString, after, matchCase, MsoTriState.msoFalse);
}
}
Keep the original (change only part)
The parameter dialog always yields a value — there is no "nothing selected". To make font or color optional, gate each with a checkbox and apply the block only when it is on:
csharp
// @param bool ChangeFont "Change font?" default="true"
// @param bool ChangeColor "Change color?" default="false"
…
if (Params.GetBool("ChangeFont", true)) { /* found.Font.Name = … etc. */ }
if (Params.GetBool("ChangeColor", false)) { found.Font.Color.RGB = System.Drawing.ColorTranslator.ToOle(fmtColor); }
Unchecked = the matches' original font resp. color stays unchanged.
Save as an Action
Such formatting scripts typically belong in the Action Manager (they do something rather than create an object). With // @button Apply the dialog button is labelled accordingly, and the action can later be reused via gallery/search.