Create a new .txt file?

hello clueless n00b here — been using popclip for years and it is an essential part of my toolkit but i’ve not dabbled with making extensions before. but now it’s so easy! :wink: i wondered how to make one that creates a new .txt file from the selected text and saves it to a pre-specified folder without the need any interaction using say the first few words for the filename.
alternatively or additonally an extension that fires an existing mac o/s service would almost do the job also…
thanks for any clues and keep up the good work nick!

1 Like

Here’s a starting point.

For writing to files you will need to use a shell script action. Here I’m using zsh
but you could also use something like python or ruby.

I’ve set the filename to the current timestamp since that’s an easy way to get an unique-ish filename.
Getting the first few words is a bit more tricky, I’ll leave that as an exercise for the reader!

You can of course change the folder, extension name, icon etc.

#!/bin/zsh
# #popclip
# name: Save to .txt
# icon: txt
# stdin: text
folder=~/Documents
filename=$(date "+%Y%m%d-%H%M%S").txt
cat > $folder/$filename

(The above block is an extension snippet — select it then click “Install Extension” in PopClip.)

2 Likes

For firing a macOS service you can use a Service action

1 Like

awesome! thank you!

I think the existing Notes extension does a lot of what you want. Albeit bundled into one big thingum; but searchable …

hi thanks do you mean the bit about setting the file name to the first few words of the selected text? because i am a bear of little brain i don’t really know where to look because the notes extension is not an extension snippet. i have opened the package but there are a lot of files and folders in there :flushed:

Sorry, I was a bit terse. No, I just meant that the Notes extension as is does much of what you want. One selects text, chooses Notes from the popclip popup, and a note is created and added to the Notes library. The beginning of the text is used as the “title” of the Note. So, when you wake up the Notes app and look down the list of notes those first few words you’re interested in will be sitting there. Try it.

Could you please modify this extension to consolidate all texts into a single file, separated by blank lines, and include a date and time stamp after each? I really appreciate its current functionality.

Something like this:

#!/bin/zsh
# #popclip
# name: Append to .txt
# icon: tx2
# stdin: text
file=~/Documents/clips.txt
cat >> $file
print "\n$(date)\n" >> $file
1 Like

You really are a genius!

i used this to make an instant blogging popclip extension for blot which turns a text file in a dropbox folder into a blog entry :wink:

#!/bin/zsh
# #popclip
# name: blog
# icon: blg
# stdin: text
folder=~/Dropbox/Apps/Blot
filename=$(date "+%Y%m%d-%H%M%S").txt
cat > $folder/$filename

i just highlight some text, click on the icon in popclip and hey presto it’s on my blog ha ha. you can see it in the wild here : https://every.thingness.link/

thanks again!

2 Likes

That’s awesome. I love to see how people use PopClip to get things done. Interesting blog too.

1 Like

ok you can almost feel another question coming but would it be easy to add a line to the snippet which opens the newly created .txt file with the application WriteRoom?

and for an added bonus point (ok have two!) prepends text in the file with the date in the format “Tuesday 31 October 2023” and two returns?

and then i promise to leave you alone and never darken your doorstep again :slight_smile:

The shell command to open the file would be

open -a WriteRoom $folder/$filename

which you can just add at the end of the script.

… I’ll come back for the bonus points, I’m going for a run :running_man:

1 Like

thank you! you’re too good, too generous. but you know where to draw the line. i predict great things for you :slight_smile: oh wait you’ve already achieved them!

1 Like

I’m just leaving this here for later…
date "+%A %d %B %Y"

Fantastic blog!

2 Likes
#!/bin/zsh
# #popclip
# { name: blog, icon: blg, stdin: text }
folder=~/Dropbox/Apps/Blot
filename_date=$(date "+%Y%m%d-%H%M%S") # 20231103-095341
display_date=$(date "+%A %-d %B %Y")   # Friday 3 November 2023
file=$folder/$filename_date.txt
echo "$display_date\n" > $file
cat >> $file
open -a WriteRoom $file || echo "meh"

Notes:

  • %-d gives 3 where %d gives 03
  • There’s only one \n (return) on the date string because echo adds its own return at the end
  • || echo "meh" is just there to absorb the error condition that would otherwise cause the nasty “X” to shake its head if WriteRoom is not installed (which was the case for me… I was like why is this script failing??)

please stay!

1 Like

thank you so much.
it works like a dream.
(yes i recognise WriteRoom might a bit obscure and maybe it’s some kind of record to be still using a programme daily which i bought at the special introductory price of $1 thirteen years ago but hey it does everything i need a text editor to do!)

1 Like

No, I remember when WriteRoom first came out, it was revolutionary!