Skip to content

Instantly share code, notes, and snippets.

@gruber
Last active April 25, 2026 05:02
Show Gist options
  • Select an option

  • Save gruber/66e87fc6011d3f648cdab17dcc22a874 to your computer and use it in GitHub Desktop.

Select an option

Save gruber/66e87fc6011d3f648cdab17dcc22a874 to your computer and use it in GitHub Desktop.
Save MarsEdit Document to Text File
-- Details and explanation:
-- https://daringfireball.net/2026/03/applescript_save_marsedit_document_to_text_file
tell application "MarsEdit"
set _doc to document of window 1
if _doc is not missing value then
-- front window is a document window
set _title to _doc's name
set _blog to _doc's current blog's name
set _tags to _doc's tags
set _slug to _doc's keywords
set _excerpt to _doc's excerpt entry
set _edited_date to _doc's edited date
set _body to _doc's current text
set _text_file_contents to "Title:" & tab & _title & linefeed & ¬
"Blog:" & tab & _blog & linefeed & ¬
"Edited:" & tab & _edited_date & linefeed & ¬
"Tags:" & tab & _tags & linefeed & ¬
"Slug:" & tab & _slug & linefeed & ¬
"Excerpt: " & tab & _excerpt & linefeed & ¬
"---" & linefeed & linefeed & ¬
_body
set f to choose file name with prompt "Save file as:" default name my MakeSafeFilename(_title) & ".text"
set fh to open for access f with write permission
set eof fh to 0 -- overwrite if file exists
write _text_file_contents to fh as «class utf8»
close access fh
else
-- Front window is main window, or there are no open windows?
beep
end if
end tell
on MakeSafeFilename(_str)
# Change colons (:) to bullets (•)
# It seems like we don't need to worry about things like newlines or tabs; they
# get turned into spaces automatically by `choose file name`. Colons are the
# gremlins we need to defend against. You get unexpected results when you pass a
# string with colons to `choose file name` -- it tries to parse the string as
# path components treating the colons as folder separators.
set _oldTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set _parts to text items of _str
set AppleScript's text item delimiters to "•"
set _newname to _parts as text
set AppleScript's text item delimiters to _oldTIDs
return _newname
end MakeSafeFilename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment