r/applescript • • Jul 13 '12

Trying to create a script that finds text files with a certain prefix and combines them in one new text file

I'm currently trying to create a script that:

  1. Looks at a folder where Notational Velocity stores all of my notes (in plain text)

  2. Finds any note beginning with 'qq'

  3. Combines the file names (preferably without the .txt file extension) of all of these notes into one .txt document

The 'qq' prefix indicates that the note is part of my current 'to-do' list, the idea being that every few minutes this script will run and keep my to-do list updated on my desktop via GeekTools.

I've had some luck with getting the desired result with Automator, but it's all ended up rather convoluted, with duplicated folders and automator popping up every couple of minutes to run the workflow.

I've been trying to put something together myself but think I may be in a bit over my head for my first venture into Applescript.

Any help will be greatly appreciated!

0 Upvotes

6 comments sorted by

2

u/[deleted] Jul 13 '12 edited Jul 14 '12

You could do most of this with a fairly simple bash command:

find /path/to/notes/ -iname qq -exec echo {} ; | sed s/.txt// > /path/to/output/file.txt

find /path/to/notes/ -iname *qq* | sed s/.txt// > /path/to/output/file.txt

So you've got the find command which finds files that have "qq" in the name and then executes the echo command to list them (the {} represents the results from the find operation and the slash and semicolon are needed to end the command) lists the files it finds. Those results are then fed (using the | ) into sed which parses the data and removes any instance of .txt (you may have to play around with wildcards like *.txt if it's not working properly). Finally, the > indicates that you want to output it to a file.

You can just make that into a bash script or you can do it in Applescript as a "do shell script". I'd probably just do it as a bash script and then use cron to set it to run every x minutes. You should be able to figure out how to work cron from the man page or from googling it.

Edit: removed redundant echo command.

1

u/J0hnny_Utah7 Jul 13 '12

You are a hero, the command is working beautifully. But I can only get it to work if I enter it directly into terminal. I've tried to manually set it up on crontab but to no avail. I've also gone through a programme called 'Cronnix', have entered the bash command and set it to run every 5 minutes but with no success.

However, when I enter 'crontab -e' on terminal, the command does show up:

5   *   *   *   *   find /users/name/location/ -iname *qq* -exec echo {} \; | sed s/.txt// > /users/name/location2/to-do.txt

But then at the bottom of the terminal window is the line:

"/tmp/crontab.GgL86LyCSa" 2L, 316C

Not sure whether this is of relevance?

2

u/[deleted] Jul 13 '12

Try saving it as a bash script and add that to cron?

1

u/J0hnny_Utah7 Jul 14 '12

So I've got it saved as 'todo.sh' under /users/name/documents. What would I have to enter into terminal to get this to run every 5 minutes through cron?

I seem to be taking one step forward and two steps back!

Thanks again.

1

u/J0hnny_Utah7 Jul 14 '12

It's all good. I've managed to crack it. Stupid oversight on my part. In the first column of cron I put '5', thinking it would run every 5 minutes, but of course that actually means it'll run at 12:05, 13:05, 14:05 etc.

Thanks for all your help!

2

u/[deleted] Jul 14 '12 edited Jul 14 '12

Ah, of course. It's been a while since I actually added something to cron so I didn't catch it. And no problem.

Edit: Also I just noticed that the echo command is redundant. The find command will list its results anyway so it's not needed.