Rename File w/ First Line

I have a lot of files imported from an email List. They have sequential numbered names: 038526, 038527 and so forth. The subject of the email is always the first line in each file.

What I would like to do is to automate renaming each file with


So “038526”, “038527” would be renamed:

  1. First Line of First File
  2. First Line of Second File

I have no idea if this can be done, and if it can, how.

Note: There are approximately 40,000 files (40,000 individual emails).

Any suggestions would be appreciated.

hi, this script should get you started. It works with simple rich text and plain text files in my database.

-- Set name to first line

tell application id "com.devon-technologies.thinkpro2"
		set counter to 0
		set theSelection to the selection
		if theSelection is {} then error "Please select some contents."
		repeat with theRecord in theSelection
			set counter to counter + 1
				set name of theRecord to (counter as string) & ". " & (paragraph 1 of rich text of theRecord) as text
			end try
		end repeat
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
end tell

That worked perfectly (so far as I can tell). I’m renaming 42,162 files, which will take a while, but it seems to have done the job.

Thank you, sir, for taking the time out to aid a newbie. I appreciate it.

Finally! Got this batch rename RTF files working again. Hallelujah!

I removed the automatic numbering because I want the title to begin with the first letter.

Just one more detail and my world will be sunshine:
Can I edit this script so that it ignores quotation marks, numerals, and other characters so the title ends up starting with the first letter?

Bonus Karma points to you if you can tell me how!


Why are you renaming a file as the first sentence in a file? This can result in files with overly long names.

Also, running this in a batch operation would require the files to have a useful or conforming first line.

You can also select a line, right-click and choose Set Name As on a one-by-one basis.

I would very much like a script to rename in this way. It is standard on many markdown note editors, and after importing some notes to DT I have a lot of notes with names like: 0FF20711-C015-44B5-9C00-F3ABC148F19B. I can’t spend the time to rename them all one-by-one. I don’t mind if it truncates overly long names, but having some kind of readable text would be nice!

So what’s wrong with a variation of the above script?
What changes do you need?

Personally, I wouldn’t need the counter in the name so I’d change to

set name of theRecord to (paragraph 1 of rich text of theRecord) as text

Not sure. Should it work if I ran it as is? From the discussion it wasn’t clear…

It is standard on many markdown note editors

I’m not sure what you’re referring to here.

Regarding renaming based on the first line, you could choose a file then use Tools > Batch Process with a Proposed Name, like so…

… as it pulls from the title (if existent) or first line of the content.

If you wanted a lilttle more control, this may also work (though untested with your specific files)

This captures the first 18 printable characters. The filename will only use output from the first line.

Ah, this first option (proposed name) worked perfectly. I also learned about the batch operations tool which I hadn’t known about. Thanks!

Here is the option in FS Notes:

You’re welcome. Glad it worked as hoped for.

1 Like