# Batch insert of first line into existing files

I have 1470 existing notes, and I would like to make the filename for each the first line in each note - "Title: (name of file)". Is there a script for this someone may have already worked up? I don't know how to use Apple Script Editor. Or perhaps another way to do this batch style? I am using The Archive for software.

• What if you don't? What is the problem that you're trying to solve? You may be able to solve it by doing that manually whenever you stumble upon a note that's not how you want it to.

• @verivox said:
I have 1470 existing notes, and I would like to make the filename for each the first line in each note - "Title: (name of file)".

I'd love to help you with this.
Seems entirely doable. I have three questions.
1. I'm assuming that name of file for each note would be something like-
/Users/will/Dropbox/zettelkasten/Cognitive Reappraisal 202203232020.md
2. Do ALL the files use the exact same naming convention?
3. How would you like this to be formatted?
Title: Cognitive Reappraisal
Title: Cognitive Reappraisal 202203232020
Title: Cognitive Reappraisal [[202203232020]]
Title: Cognitive Reappraisal §[[202203232020]]

Send me via DM or post here a list of 10 complete file names copied from Finder.

Will Simpson
“Read Poetry, Listen to Good Music, and Get Exercise”
kestrelcreek.com

• That is what I think I will have to do. I was hoping for a way to automatically insert title for first line in already existing notes. Thanks!

• I don't know of any tool that would automatically take a files file name and insert it with custom formatting into the said file. This would be trivial to do this with egrep, sed and awk or in a python script. Good luck.

Will Simpson
“Read Poetry, Listen to Good Music, and Get Exercise”
kestrelcreek.com

• Thank you for your willingness to help. I must confess my ignorance of technical stuff like "egrep, sed and awk or in a python script", I am not at all familiar with those things. Here is a list of sample file names. As you can see, they do not all have the same naming convention, but the ones that begin with the ID number make up the majority of file names.

Here are some examples:
202006231322-formats-of-lists.txt
Move-to-Help-In-Another-Area?–2014-01-19.txt
quick-capture.txt
Terr-Coverage-4Months–2014-01-19.txt
202106171359-terminal-commands.txt
201802121037 Welcome to The Archive.txt
202008171648-booknote-sonke-ahrens.txt

As far as the format I would like in the actual note's first line:
Title: 201802121037 Welcome to The Archive.txt

Again, I appreciate the interest and help!

• edited April 1

@verivox

I’m following this thread to see what @Will comes up with. Certainly it would be possible using Python, but if you are not a Python programmer, that won’t help you much.

I have a separate suggestion for you, though. If you are responding to someone’s comment on the forum, include their name in your response (like I did above, for both you and Will). Or you can use the “quote” link at the bottom of their post, that will open up a new post for you that quotes their post. Either approach will alert them to the fact that someone responded to or commented on something they posted. Otherwise, they may not see your question or comment (maybe not at all or maybe not very quickly).

• @verivox, I got a script working. The notes I used for testing have dummy text in them but the first line is where we'll when to focus our attention.

As you can see the titling is less than perfect. The capitalization is off and the are "-" between words. The script so far only works on those files with the name format of 12 numbers at the beginning and ".txt" at the end. Exception handling is usually what takes so long in developing scripts.

I'd like to know which version of python is installed on your machine.
Run Terminal
Type the following command
whence python
Report back the version number.

Will Simpson
“Read Poetry, Listen to Good Music, and Get Exercise”
kestrelcreek.com

• @GeoEng51 said:
@verivox

I’m following this thread to see what @Will comes up with. Certainly it would be possible using Python, but if you are not a Python programmer, that won’t help you much.

I have a separate suggestion for you, though. If you are responding to someone’s comment on the forum, include their name in your response (like I did above, for both you and Will). Or you can use the “quote” link at the bottom of their post, that will open up a new post for you that quotes their post. Either approach will alert them to the fact that someone responded to or commented on something they posted. Otherwise, they may not see your question or comment (maybe not at all or maybe not very quickly).

Thanks for the tip!

• @Will said:
@verivox, I got a script working. The notes I used for testing have dummy text in them but the first line is where we'll when to focus our attention.

As you can see the titling is less than perfect. The capitalization is off and the are "-" between words. The script so far only works on those files with the name format of 12 numbers at the beginning and ".txt" at the end. Exception handling is usually what takes so long in developing scripts.

I'd like to know which version of python is installed on your machine.
Run Terminal
Type the following command
whence python
Report back the version number.

I'm running mac os Monterey 12.3.1, and that command does not yield anything in Terminal.

• @verivox said:

@Will said:
@verivox, I got a script working. The notes I used for testing have dummy text in them but the first line is where we'll when to focus our attention.

As you can see the titling is less than perfect. The capitalization is off and the are "-" between words. The script so far only works on those files with the name format of 12 numbers at the beginning and ".txt" at the end. Exception handling is usually what takes so long in developing scripts.

I'd like to know which version of python is installed on your machine.
Run Terminal
Type the following command
whence python
Report back the version number.

I'm running mac os Monterey 12.3.1, and that command does not yield anything in Terminal.

Thank you again for working on this. Your example looks fine to me, I don't mind the "-" between words; I hope it's not a bad idea, but I like to use it in naming. But maybe better to leave a space.

• @verivox said:

@Will said:
@verivox, I got a script working. The notes I used for testing have dummy text in them but the first line is where we'll when to focus our attention.

As you can see the titling is less than perfect. The capitalization is off and the are "-" between words. The script so far only works on those files with the name format of 12 numbers at the beginning and ".txt" at the end. Exception handling is usually what takes so long in developing scripts.

I'd like to know which version of python is installed on your machine.
Run Terminal
Type the following command
whence python
Report back the version number.

I'm running mac os Monterey 12.3.1, and that command does not yield anything in Terminal.

And I don't think we have to worry about exception handling if it will work on all with ID of 12 numbers.

• @verivox, I got a few more minutes to work on this and I got the capitalization and dashes handled. A few more minutes and I'll have the files without the 12 digit UUID working too. My life situation is such that I only get a few minutes here and there to work on these things. I'll be able to work on it again at noonish tomorrow.

Try the command from Terminal
which python

Will Simpson
“Read Poetry, Listen to Good Music, and Get Exercise”
kestrelcreek.com

• @Will said:
@verivox, I got a few more minutes to work on this and I got the capitalization and dashes handled. A few more minutes and I'll have the files without the 12 digit UUID working too. My life situation is such that I only get a few minutes here and there to work on these things. I'll be able to work on it again at noonish tomorrow.

Try the command from Terminal
which python

I got this from Terminal: python not found

• Working on this script is fun. Thanks for giving me this opportunity. We'll soon see if this is of some value.

I had heard that Apple would stop shipping new Macs with python, but I thought it would be in the next year. Oh well???

My python skills are still too immature to ship a self-contained GUI app. I'll get there. I've switched to writing a shell script that will be easy to execute. Everything is working, but I'm working out the failsafe features. I want to program backups of each file before the change is made. Also, there is one variable snafu I still need to resolve.

I hope you can wait a little longer. Here is what part of the testing environment looks like. You can see the file names convert to nice-looking titles. A minor tweak for formatting and safety procedures is all that is left to do.

For those following along who have shell scripting skills, here's a peek at what I'm up to. If you see something that could be improved or want to collaborate somehow, please speak up.

for filename in temp/*.txt; do
title=$( basename "$filename" .txt )
# Do things using "$prefix" here if [[ "$title" =~ ^[a-zA-Z],*.* ]]; then
echo $title | sed 's/-/ /g' | awk '{for(i=1;i<=NF;i++){$i=toupper(substr($i,1,1)) substr($i,2) }}1'
fi
if [[ "$title" =~ ^20*.* ]]; then echo "${title:13:100}" | sed 's/-/ /g' | awk '{for(i=1;i<=NF;i++){ $i=toupper(substr($i,1,1)) substr($i,2) }}1' fi sed '1a\ File Name: '$title'
' < "$filename" done  Will Simpson “Read Poetry, Listen to Good Music, and Get Exercise” kestrelcreek.com • @Will said: Working on this script is fun. Thanks for giving me this opportunity. We'll soon see if this is of some value. I had heard that Apple would stop shipping new Macs with python, but I thought it would be in the next year. Oh well??? My python skills are still too immature to ship a self-contained GUI app. I'll get there. I've switched to writing a shell script that will be easy to execute. Everything is working, but I'm working out the failsafe features. I want to program backups of each file before the change is made. Also, there is one variable snafu I still need to resolve. I hope you can wait a little longer. Here is what part of the testing environment looks like. You can see the file names convert to nice-looking titles. A minor tweak for formatting and safety procedures is all that is left to do. For those following along who have shell scripting skills, here's a peek at what I'm up to. If you see something that could be improved or want to collaborate somehow, please speak up. for filename in temp/*.txt; do title=$( basename "$filename" .txt ) # Do things using "$prefix" here
if [[ "$title" =~ ^[a-zA-Z],*.* ]]; then echo$title | sed 's/-/ /g' | awk '{for(i=1;i<=NF;i++){ $i=toupper(substr($i,1,1)) substr($i,2) }}1' fi if [[ "$title" =~ ^20*.* ]]; then
echo "${title:13:100}" | sed 's/-/ /g' | awk '{for(i=1;i<=NF;i++){$i=toupper(substr($i,1,1)) substr($i,2) }}1'
fi
sed '1a\
File Name: '$title' ' < "$filename"
done


Awesome! I can wait for however long I need to. I'm intrigued to see how this comes out.

• @verivox, I've got the script working. It should handle all the files. I'm currently working out the safety backup systems and the user options.

For those following along who have shell scripting skills, here's a peek at what I'm up to. If you see something that could be improved or want to collaborate somehow, please speak up.

#! /bin/zsh

removedashes()
{
sed -E 's/-| / /g'
return
}

capitalize()
{
awk '{for(i=1;i<=NF;i++){ $i=toupper(substr($i,1,1)) substr($i,2) }}1' return } for filename in temp/*.txt; do title=$( basename "$filename" .txt ) #Notes that don't led with a UUID if ! [[ "$title" =~ ^[0-9]{12}.* ]]; then
cp $filename /Users/will/Dropbox/Git/insert-UUID/tmp/$title.bak
plain=echo $title | removedashes | capitalize sed '1a\ File Name: '$plain'
' < "$filename" > /Users/will/Dropbox/Git/insert-UUID/tmp/$title.md
fi
#Note that led with a UUID
if [[ "$title" =~ ^[0-9]{12}.* ]]; then cp$filename /Users/will/Dropbox/Git/insert-UUID/tmp/$title.bak uuid=echo "${title:13:100}" | removedashes | capitalize
sed '1a\
File Name: '$uuid' ' < "$filename" > /Users/will/Dropbox/Git/insert-UUID/tmp/$title.md fi done  Will Simpson “Read Poetry, Listen to Good Music, and Get Exercise” kestrelcreek.com • @Will said: @verivox, I've got the script working. It should handle all the files. I'm currently working out the safety backup systems and the user options. For those following along who have shell scripting skills, here's a peek at what I'm up to. If you see something that could be improved or want to collaborate somehow, please speak up. #! /bin/zsh removedashes() { sed -E 's/-| / /g' return } capitalize() { awk '{for(i=1;i<=NF;i++){$i=toupper(substr($i,1,1)) substr($i,2) }}1'
return
}

for filename in temp/*.txt; do
title=$( basename "$filename" .txt )
#Notes that don't led with a UUID
if ! [[ "$title" =~ ^[0-9]{12}.* ]]; then cp$filename /Users/will/Dropbox/Git/insert-UUID/tmp/$title.bak plain=echo$title | removedashes | capitalize
sed '1a\
File Name: '$plain' ' < "$filename" > /Users/will/Dropbox/Git/insert-UUID/tmp/$title.md fi #Note that led with a UUID if [[ "$title" =~ ^[0-9]{12}.* ]]; then
cp $filename /Users/will/Dropbox/Git/insert-UUID/tmp/$title.bak
uuid=echo "${title:13:100}" | removedashes | capitalize  sed '1a\ File Name: '$uuid'
' < "$filename" > /Users/will/Dropbox/Git/insert-UUID/tmp/$title.md
fi
done


👍🏼