# Apple Script to get the H1 in a Markdown file as String

**URL:** <https://discourse.devontechnologies.com/t/apple-script-to-get-the-h1-in-a-markdown-file-as-string/72835>\
**Category:** Automation\
**Created:** [September 20, 2022, 2:24pm UTC](https://discourse.devontechnologies.com/t/apple-script-to-get-the-h1-in-a-markdown-file-as-string/72835 "2022-09-20T14:24:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![OAL](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/o/e95f7d/32.png) [@OAL](https://discourse.devontechnologies.com/u/OAL)\
**Post date:** [September 20, 2022, 2:24pm UTC](https://discourse.devontechnologies.com/t/apple-script-to-get-the-h1-in-a-markdown-file-as-string/72835/1 "2022-09-20T14:24:33Z")

</div>

Can anybody please help me with this.

The code from the link below that @BLUEFROG shared already processes the markdown file. I only need it to get the heading 1 and save it into something like titleText. So that I can use it.

I wrote something like this but didn’t work.

```auto
set titleText to (characters 3 thru -1 of paragraph 0 of currentText as string) 
                -- Cache all but the control characters in H1

```

You can also check out the conversation below to see what i mean.

> [@How to run Javascript on Markdown "summarize highlights" output?](https://discourse.devontechnologies.com/t/how-to-run-javascript-on-markdown-summarize-highlights-output/53375/20):
>
> Hey Jim, @BLUEFROG, Could you be so kind as to modify this code so that: Instead of Page, it writes the name of the file in the paranthesis in the following format(semicolon)? Right Now: How I would love it to be:

---

<div class="post-metadata">

**Author:** ![DTLow](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/dtlow/32/18161_2.png) [@DTLow](https://discourse.devontechnologies.com/u/DTLow)\
**Post date:** [September 20, 2022, 3:52pm UTC](https://discourse.devontechnologies.com/t/apple-script-to-get-the-h1-in-a-markdown-file-as-string/72835/2 "2022-09-20T15:52:31Z")

</div>

> [@OAL](#):
>
> I wrote something like this but didn’t work.

Can you add more detail to “didn’t work”

I’m thinking paragraph 0 should be paragraph **1**  
You might want to check if the paragraph is actually a heading; ie begins with \*\*

---

<div class="post-metadata">

**Author:** ![OAL](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/o/e95f7d/32.png) [@OAL](https://discourse.devontechnologies.com/u/OAL)\
**Post date:** [September 20, 2022, 6:01pm UTC](https://discourse.devontechnologies.com/t/apple-script-to-get-the-h1-in-a-markdown-file-as-string/72835/3 "2022-09-20T18:01:00Z")

</div>

paragraph 1 did the trick! Thanks a lot.

For anyone interested in the resulting solution you can check out both the Applescript and the Javascript solutions in the original topic:

> [@How to run Javascript on Markdown "summarize highlights" output?](https://discourse.devontechnologies.com/t/how-to-run-javascript-on-markdown-summarize-highlights-output/53375/25):
>
> I managed to solve this both in Apple Script and Javascript. Now I get the Heading 1 in the parentheses. This is the Applescript version: on performSmartRule(theRecords) tell application id "DNtp" repeat with theRecord in theRecords if (type of theRecord as string) = "markdown" then -- Only process Markdown docs set newText to {} -- Set up an empty list to push the paragraphs into) set currentText to (plain text of theRecord) -- Get the current text of the …

---

<div class="post-metadata">

**Author:** ![system](https://devontech-discourse.s3.dualstack.us-east-1.amazonaws.com/uploads/original/2X/4/4e6ff70c96be2eb307a274d890bba8d71ba671b3.png) [@system](https://discourse.devontechnologies.com/u/system)\
**Post date:** [September 19, 2025, 6:01pm UTC](https://discourse.devontechnologies.com/t/apple-script-to-get-the-h1-in-a-markdown-file-as-string/72835/4 "2025-09-19T18:01:26Z")

</div>

This topic was automatically closed 1095 days after the last reply. New replies are no longer allowed.
