Table of contents and new files


I would like to create a markdown file with a list (e.g):

001 Text title 1
002 Text title 2
003 Text title 3
004 Text title 4

Then I would like to add a new blank markdown file for each one of that.

[[001 Text title 1]](x-devonthink-item://5CFBADA2-8535-47BD-97A0-985895585C42)
[[002 Text title 2]](x-devonthink-item://BC02516D-F302-4FAB-A507-EC4BABEA3B01)
[[003 Text title 3]](x-devonthink-item://8FF710AD-E8EC-4638-820A-330CD325D978)
[[004 Text title 4]](x-devonthink-item://6107C441-6EA0-43DB-AC10-A9368E59785D)

Nowadays, I use to do that manually and it spent a lot of time.

[[001 Text title 1]](x-devonthink-item://5CFBADA2-8535-47BD-97A0-985895585C42)

This is incorrect syntax for a Markdown link. There should only be one set of brackets on the link text.

You can Command-Option-drag and drop files into a Markdown document to insert a link.

This is very useful!

Perhaps, it is not my doubt.

I would like to create FIRST a list. and then I wish each item of that turns a new empty markdown file.

I’m not sure, but I think that if I type [[text]], a new blank file creates as a new file with the name directly.

I’m thinking about a kind of wiki command.

Is it possible?

This script uses selected text to create markdown records.

If no text is selected it displays a dialog where you can provide a vertical list of names. To add a new line in the dialog use option + return (⌥ ⏎) .

It optionally creates a TOC.

-- Create Markdown records from selected text or dialog (and optionally create TOC)

property createTOC : true
property theTOCName : "Inhaltsverzeichnis"
property addEmptyLines : true --  readability 
property openTOC : false -- open TOC or TOC's group

tell application id "DNtp"
		set theInfo to "Create Records"
		if createTOC = true then set theInfo to theInfo & space & "And TOC"
			set defaultAnswer to selected text of window 1 & "" as string
		on error
			set defaultAnswer to ""
		end try
		set theNames to paragraphs of (display name editor theInfo info "Names:" default answer defaultAnswer)
		if theNames = {} then error "Try again."
		set theGroup to display group selector "Create records in:"
		set theNewRecords to {}
		repeat with thisName in theNames
			if thisName as string ≠ "" then set end of theNewRecords to (create record with {name:thisName, type:markdown} in theGroup)
		end repeat
		if createTOC = true then
			set theTOCSource to ""
			repeat with thisRecord in theNewRecords
				set theTOCSource to theTOCSource & linefeed & ("[" & (name of thisRecord) & ".md](" & (reference URL of thisRecord) & ")") & space & space
				if addEmptyLines = true then set theTOCSource to theTOCSource & linefeed
			end repeat
			set theTOC to create record with {name:theTOCName, type:markdown, source:theTOCSource} in theGroup
			if openTOC = true then
				open window for record theTOC
				set selection of (open window for record theGroup) to {theTOC}
			end if
			set selection of (open window for record theGroup) to theNewRecords
		end if
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell
1 Like

Many thanks @pete31. It works perfectly!

Hey pete, you really shared a lot of scripts in this community~ :rose:I changed it myself based on yours, very easy to use!

A small question, whether the script can do the following:

  • If the selected file is not a picture, generated a normal TOC.
  • If the file is a picture, insert this picture at the end of each normal TOC

I want to use this script to generate notes that index the contents of the Group. With the current script, I need to manually copy the link, add a “!” and delete the name for the image file. I wonder if it can continue to optimize :stuck_out_tongue_winking_eye:

thank you!

You’ll find most, if not all, of what you need to modify it in the various script modifications I wrote for you in the last weeks in this thread.

The modifications you asked for in the past were all about a very specific database setup in which you use images and thumbnails in an unusual way. I already had a hard time understanding your first desired setup and as it turned out you didn’t really know how it should look like as you’ve changed your mind several times.

For most people who just want to solve a specific task for which a script would be useful (or the only way) it doesn’t make much sense to try to modify an existing script on their own or to learn AppleScript, so I’ll be happy to write it.

In your case however there is this very unusual database setup, plus you’ve added DEVONthink features to your setup which I don’t use myself, plus you change the setup all the time.

I think what you mean is: you wonder if I will modify it for you. No, I won’t. I won’t be the guy who modifies scripts for you on a regular basis.

Yes, I’ve shared a lot of scripts here, but what do you expect me to do? Do you want me to modify every script I’ve shared to fit your specific setup? Sorry, I won’t do that.

And I don’t think you should expect from other users that they’ll wrap their heads around your unusual setup every time you want someone to modify or write a script, but maybe you’ll find users who do that.

Don’t get me wrong, there’s nothing wrong with your setup or with asking for help.

But altough I understand that you’re a new user and don’t know AppleScript I think that it doesn’t make sense and actually is quite impolite to want someone else to do something for you only to find out that the time spent with helping you was all for nothing because you’ve changed your mind again.

So my conclusion is that you’ll have to learn AppleScript.

If you do chances are that you can realize exactly the setup you want after some weeks or even days.

How to get started with AppleScript in general:

  • Don’t modify existing scripts. Read and try to understand the steps taken in a script, then write your own.

  • Do search if you don’t know how something is done. Use e.g. to search for “AppleScript” plus the command or idea you have.

  • Do use descriptive variable names. Using self-explanatory variable names makes it easier for you to understand what your script does while writing it and in the future. It also makes it easier for other users, e.g. when you post one that you need help with. You’ll often find variable names used in someone else’s script cryptic or not descriptive enough. Change these to your own when you write your version.

  • Do use comments. -- or # in front of a line comments it out. Use them to deactivate code while testing something or to add your own comments. It’s also a good idea to start every script with a short description of the desired outcome and the date you started to write it.

How to get started with understanding what happens in a script:

  • Download Script Debugger.

  • Read Script Debugger’s help.

  • Use Script Debugger’s Results & Variables Tab

How to explore an app’s AppleScript dictionary:

  • Select Script Debugger’s menu Window > New Dictionary Window.

  • Select e.g. DEVONthink 3 in the sidebar.

  • Use the three tabs at the top (“Dictionary”, “Object Model” and “Explorer”) to explore the dictionary.

How to find scripts on the forum:

  • Don’t search for “AppleScript” as this is the main scripting language used with DEVONthink and most script posts don’t contain this term.

  • Do search for somthing that’s part of almost all scripts, e.g. “tell application”

  • Do add “after:2019-09-12” to the query to find scripts which work with DEVONthink 3 (that’s the date of the official DEVONthink 3 release).

  • Do also search with “before:2019-09-12” to find older scripts, but be aware that things might have changed.

Good luck!

1 Like

:sob: Sorry!!!
I apologize for my rudeness and blame myself for my ignorance. You are right. My needs are too personal. I have no reason to be willful anymore~

For me, Applecript is indeed a bit difficult to learn, and there is no similar community around me to communicate with. There are even fewer people around me who know Applecsript and use Dt at the same time…

But thank you for your study guide, I will learn it slowly~ I would also like to give back your help very much if possible, thank you again!!!

See my response in your other post.

Also Script Debugger, while capable, is not required. Script Debugger can be a lot to take in. I’ve professionally automated for 20+ years using only Apple’s Script Editor and the results pane or Log window… with pen and paper also. Still very handy today.