-- Pages to Markdown converter. -- © 2010 Perette Barella -- There's gotta be a better way. -- Currently implements bold, italics, and headings based on paragraph styles: -- "Title" -> H1, "Heading 1" -> H2, "Heading N" -> H(N+1) on repeat_character(the_character, repeat_count) set result to "" repeat with repetitions from 1 to repeat_count set result to result & the_character end repeat return result end repeat_character on format_heading(heading_type, title) set cr to ASCII character 10 if heading_type = "Title" then return title & repeat_character("=", length of title) & cr else if heading_type = "Heading 1" then return title & repeat_character("-", length of title) & cr else if heading_type = "Heading 2" then return "### " & text 1 through -2 of title & " ###" & cr else if heading_type = "Heading 3" then return "#### " & text 1 through -2 of title & " ####" & cr else if heading_type = "Heading 4" then return "##### " & text 1 through -2 of title & " #####" & cr else if heading_type = "Heading 5" then return "###### " & text 1 through -2 of title & " ######" & cr end if return missing value end format_heading on run argv if (count argv) > 0 then open argv end if set cr to ASCII character 10 set output to {} tell document 1 of front window of application "Pages" set document_length to (count of paragraphs of body text) repeat with paragraph_number from 1 to document_length set this_paragraph to paragraph paragraph_number of body text set markdown to "" select paragraph paragraph_number of body text set selection_type to properties of selection set class_stuff to class of selection_type if class_stuff = text then set style_name to name of paragraph style of selection_type set markdown to format_heading(style_name, this_paragraph) of me if markdown is missing value then set markdown to "" set boldized to false set italicized to false set engaged to false repeat with char_num from 1 to (length of this_paragraph) - 1 select character char_num of paragraph paragraph_number of body text set ch to contents of selection if ch ≠ " " and ch ≠ " " then set engaged to true if engaged then set charprops to properties of selection if boldized ≠ bold of charprops then set markdown to markdown & "**" set boldized to bold of charprops end if if italicized ≠ italic of charprops then set markdown to markdown & "_" set italicized to italic of charprops end if set markdown to markdown & ch end if end repeat if italicized then set markdown to markdown & "_" end if if boldized then set markdown to markdown & "**" end if set markdown to markdown & cr else set markdown to cr & cr & markdown end if else set markdown to this_paragraph end if set end of output to (markdown & cr) end repeat end tell return output as text end run