Transform text
Applies one text filter (case, quoting, encoding, paths, URLs, counts, substring, calculation and more) and returns the result.
Transform text with op
Keyboard Maestro: FilterVariable, FilterClipboard, Substring
Details #
One step for what Keyboard Maestro's Filter actions do to text. Pick the filter with op, give it text, and read the result from into. The result is also the step's output. The text can use variables and expressions, as in any step.
The filters, by group:
Case:
uppercase,lowercase,capitalize,uppercaseFirst,lowercaseFirst,titleCase,sentence.Whitespace and lines:
trim,reverse,shuffle(reorders the lines),unwrap,wrap(useswidth),unixLineEndings,macLineEndings,windowsLineEndings,bulletControlCharacters,zapControlCharacters.Counts (numbers):
characterCount,lineCount,wordCount.Pieces:
substring(usesstart,length,kind).Quotes:
dumbQuotes,smartQuotes,frenchQuotes.Quoting for code:
quoteShell,quoteAppleScript,quoteJavaScript,quoteSwift,quoteJSON,quoteProcessTokens,regexEscape.Encoding:
base64Encode,base64Decode,urlEncode,urlDecode,htmlEncode,htmlEncodeNonASCII,htmlEncodeNumeric,htmlDecode,htmlList,md5.JSON:
jsonPretty,jsonCompact.Paths:
abbreviateTilde,expandTilde,standardizePath,resolveSymlinks,baseName,lastPathComponent,deleteLastPathComponent,pathExtension,deletePathExtension,displayName.URL parts:
urlScheme,urlHost,urlUser,urlPassword,urlPort,urlPath,urlQuery,urlFragment. A part that is missing gives empty text.Evaluate:
calculate(evaluates the text as an expression and returns the result as text),processTokens(expands Keyboard Maestro%Variable%name%tokens and{{ }}templates using the run's variables).Lookups:
namedVariable(the value of the variable calledname, or of the text ifnameis not given),namedClipboard(the text of the named clipboard in clipboard history).Clipboard actions:
cleanURL(strips tracking parameters such asutm_*,fbclid,gclid,mc_*andreffrom a link; other text comes back unchanged),markdownLink([host](url)),colorFormat(a color ashex,rgb,hsl,swiftui,nscolororuicolor, withformat),sortLines(A to Z, or Z to A withorder: desc),uniqueLines(drops repeated lines),collapseSpaces(trims and squeezes runs of spaces) andfenceCode(wraps in a ``` fence, with an optionallanguage). They are what the clipboard actions are made of.Paste as:
htmlToMarkdown(rich text as Markdown;textis{{clipboard.rich}}andfallbackthe plain text to use when nothing rich was copied),markdownToHTML(Markdown as an HTML fragment),toJSON(CSV, TSV, YAML,key: valueorkey=valuelines as pretty JSON) andjsonToCSV(an array of objects as CSV). They are what the rows of Paste as are made of.Rich text:
attributedTextToRTF,rtfToText,removeStyles(text in, text out; styling is not kept), andidentity(returns the text unchanged).
An unknown op fails with "unknown text.transform op" and a "did you mean" hint when only the capitalisation is off. (action is another spelling of op, and value another spelling of text.)
Parameters #
| Parameter | Type | Required | Values | What it does |
|---|---|---|---|---|
text | text | Yes | The input text. Required by the schema; may be empty. (value is another spelling.) | |
action | one of | abbreviateTilde attributedTextToRTF base64Decode base64Encode baseName bulletControlCharacters calculate capitalize characterCount deleteLastPathComponent deletePathExtension displayName dumbQuotes expandTilde frenchQuotes htmlDecode htmlEncode htmlEncodeNonASCII htmlEncodeNumeric htmlList identity jsonCompact jsonPretty lastPathComponent lineCount lowercase lowercaseFirst macLineEndings md5 namedClipboard namedVariable pathExtension processTokens quoteAppleScript quoteJSON quoteJavaScript quoteProcessTokens quoteShell quoteSwift regexEscape removeStyles resolveSymlinks reverse rtfToText sentence shuffle smartQuotes standardizePath substring titleCase trim unixLineEndings unwrap uppercase uppercaseFirst urlDecode urlEncode urlFragment urlHost urlPassword urlPath urlPort urlQuery urlScheme urlUser windowsLineEndings wordCount wrap zapControlCharacters | Another spelling of op. | |
fallback | text | For htmlToMarkdown: the text to convert when text is empty. | ||
format | text | For colorFormat: hex, rgb, hsl, swiftui, nscolor or uicolor. | ||
into | text | The variable that receives the result. | ||
kind | text | For substring: FromStart (the first length characters), FromEnd (the last length) or Range (the default; anything else is treated as a range). | ||
language | text | For fenceCode: the language written after the opening fence. | ||
length | any value | For substring: how many characters. The default is all of them. | ||
name | text | For namedVariable and namedClipboard: the name to look up. | ||
op | one of | abbreviateTilde attributedTextToRTF base64Decode base64Encode baseName bulletControlCharacters calculate capitalize characterCount cleanURL collapseSpaces colorFormat deleteLastPathComponent deletePathExtension displayName dumbQuotes expandTilde fenceCode frenchQuotes htmlDecode htmlEncode htmlEncodeNonASCII htmlEncodeNumeric htmlList htmlToMarkdown identity jsonCompact jsonPretty jsonToCSV lastPathComponent lineCount lowercase lowercaseFirst macLineEndings markdownLink markdownToHTML md5 namedClipboard namedVariable pathExtension processTokens quoteAppleScript quoteJSON quoteJavaScript quoteProcessTokens quoteShell quoteSwift regexEscape removeStyles resolveSymlinks reverse rtfToText sentence shuffle smartQuotes sortLines standardizePath substring titleCase toJSON trim uniqueLines unixLineEndings unwrap uppercase uppercaseFirst urlDecode urlEncode urlFragment urlHost urlPassword urlPath urlPort urlQuery urlScheme urlUser windowsLineEndings wordCount wrap zapControlCharacters | The filter to apply. Required (or action). See the groups above. | |
order | one of | asc desc | For sortLines: asc (the default) or desc. | |
start | any value | For substring with kind Range: the first character, counting from 1. The default is 1. | ||
value | text | Another spelling of text. | ||
width | number | For wrap: the line width in characters. The default is 72 and the smallest is 8. |
Every step also accepts label, onFailure, timeout and dryRunSafe.
Examples #
This only works on text, so it is safe:
- Transform “the lord of the rings” with trim
- Transform “{{clean}}” with title case
- Transform “{{title}}” with word count
- Show “{{title}} ({{words}} words)” for 3 s
Take the last three characters:
- Transform “Switchboard” with substring
- Show “{{tail}}”
Gotchas #
It is safe in a dry run: it runs for real, since it only changes text and touches nothing else.
base64Decodefails with "that text isn't Base64";jsonPrettyandjsonCompactfail with "that text isn't valid JSON".calculatefails with "nothing to calculate" on empty text and "couldn't calculate" when the expression is wrong. Use Variables and expressions for the expression language.namedClipboardfails with "no clipboard is named" when there is no such clipboard, and "clipboard history isn't running in this process" if history is off.urlEncodeleaves only letters, digits and-._~as they are.urlDecodeturns+into a space.md5is for checksums and matching, not for security.shufflegives a different order each run.