Skip to content

Instantly share code, notes, and snippets.

@SKaplanOfficial
Created April 6, 2025 14:54
Show Gist options
  • Save SKaplanOfficial/9637cd69e2b21b1749521aafa1f71c95 to your computer and use it in GitHub Desktop.
Save SKaplanOfficial/9637cd69e2b21b1749521aafa1f71c95 to your computer and use it in GitHub Desktop.
Some example scripts showing how to make use of MarkEdit's AppleScript support.

MarkEdit Scripting Example Document

To run the MarkEdit example scripts, move or copy this file (Example.md) to your Downloads directory.

Very Important Content

The quick brown fox jumps over the lazy dog.

tell application "MarkEdit"
-- There are several ways to get the raw text of currently open documents.
-- The result is the same, so you can use whichever approach works best in your script.
set firstSource to source of document 1 -- By index
set secondSource to source of document "Example.md" -- By name
set thirdSource to source of first document where its file is (POSIX file "/Users/steven/Downloads/Example.md") -- By file
log firstSource -- # MarkEdit Scripting Example Document... The **quick** brown fox *jumps* over the *lazy* dog.
log (firstSource is secondSource) and (secondSource is thirdSource) -- true
-- You can also access the formatted text of the document.
-- This is the text after processing the source and removing Markdown syntax characters.
log formatted text of document 1 -- MarkEdit Scripting Example Document... The quick brown fox jumps over the lazy dog.
-- With formatted text, you can access the paragraphs, words, and characters.
set theParagraphs to paragraphs of formatted text of document 1
set theParagraphs to paragraphs of document 1 -- Specifying the 'formatted text' property is optional here (same result either way)
set theWords to words 2 thru 4 of item 7 of theParagraphs
set theCharacters to characters of item 3 of theWords
log theCharacters -- {"f", "o", "x"}
-- Formatted text also allows you to identify text by its style when rendered.
log (get words of document 1 whose font contains "Bold") -- quick
log (get words of document 1 whose font contains "Oblique") -- Downloads, jumps, lazy
end tell
tell application "MarkEdit"
-- Get the current selected text using the 'selection' property of any document.
-- This uses the raw source, so Markdown syntax is included in the returned text.
set mySelection to selection of document 1
-- Replace the selected text using 'set'.
set selection of document 1 to "Testing!"
-- Note that text gets replaced without re-selecting the area, so 'selection' is now an empty string.
set newSelection to selection of document 1
log newSelection is "" -- true
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment