Append Text to the Beginning of a File

Talk, speculate, discuss, pontificate. As long as it pertains to Hazel.

Moderators: Mr_Noodle, Moderators

Append Text to the Beginning of a File Fri Apr 19, 2013 7:10 pm • by pythonicmac
What I want is a way to have Hazel automatically add YAML header to files I add to a folder. What this would mean is that when a file is put into the folder that Hazel is monitoring, it would find the name and date created of that file. Then it would put at the beginning of that file:

Title: (file name)
Date: (date created, preferably in YYYY-MM-DD format).

I find that this is not offered stock with Hazel. So what I might need to do is to have Hazel run a shell or applescript. But my scripting skill is nonexistent and I have no idea how to do that. Could somebody offer me assistance?
pythonicmac
 
Posts: 4
Joined: Fri Apr 19, 2013 7:05 pm

Re: Append Text to the Beginning of a File Mon Apr 22, 2013 4:07 pm • by Mr_Noodle
Please do not post questions to the Tips forum. The sticky article at the top of that forum indicates you should not do this.

As for scripting, google around or perhaps someone else can chime in with suggestions.
Mr_Noodle
Site Admin
 
Posts: 11193
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Ah I see, sorry for the inconvenience.
pythonicmac
 
Posts: 4
Joined: Fri Apr 19, 2013 7:05 pm

Re: Append Text to the Beginning of a File Mon Jul 08, 2013 1:42 pm • by mws
Quick'n'dirty:

Code: Select all
#!/bin/bash
base=$(basename -s .txt "$1")
tmpfile=$(mktemp -t hazel)
echo "Title: \"$base\"" > "$tmpfile"
echo "Date: $(date +%F)" >> "$tmpfile"
cat "$1" >> "$tmpfile"
mv "$tmpfile" "$1"

Get Basename (no path) without extension (change .txt to whatever your's are) for the title. Make a temporary file, write header to it and append the content of the original file to it. Move temp-file over original.

Remove the 2 \"s if you don't want quotes around the title
mws
 
Posts: 6
Joined: Mon Jul 08, 2013 1:04 pm

Re: Append Text to the Beginning of a File Wed Jul 10, 2013 5:10 pm • by sjk
mws wrote:Quick'n'dirty:


And here's a "here document"ifed version of it. :)

Code: Select all
#!/bin/bash

base=$(basename -s .txt "$1")
tmpfile=$(mktemp -t hazel)

cat - "$1" > "$tmpfile" <<EOF
Title: "$base"
Date: $(date +%F)
EOF
mv -f "$tmpfile" "$1"
sjk
 
Posts: 332
Joined: Thu Aug 02, 2007 5:43 pm
Location: Eugene

Re: Append Text to the Beginning of a File Thu Jul 11, 2013 11:45 am • by mws
And an `in-memory`Version ;)

Code: Select all
#!/usr/bin/perl -w
use DateTime; use File::Basename;
open F, "<$ARGV[0]"; @c = <F>; close F;
unshift @c, "---\nTitle: \"" . basename($ARGV[0], ".txt") ."\"\nDate: " . DateTime->now->strftime('%Y-%m-%d') . "\n---\n";
open F, ">$ARGV[0]"; print F @c; close F;
mws
 
Posts: 6
Joined: Mon Jul 08, 2013 1:04 pm

Re: Append Text to the Beginning of a File Thu Jul 11, 2013 12:31 pm • by mws
I always wanted to learn some python so I took this example as a starter.

Here is a python version without a temp-file and with the advantage that the extension doesn't matter 8)
Code: Select all
#!/usr/bin/python
import sys, os.path, datetime
with open(sys.argv[1], "r") as d:
    f = d.readlines()
with open (sys.argv[1], "w") as d:
    d.write('---\nTitle: "' + os.path.splitext(os.path.basename(sys.argv[1]))[0] + '"\n')
    d.write('Date: ' +  datetime.datetime.now().strftime("%Y-%m-%d") + '\n---\n')
    d.writelines(f)

pythonicmac, take your pick!
mws
 
Posts: 6
Joined: Mon Jul 08, 2013 1:04 pm

Re: Append Text to the Beginning of a File Fri Jul 12, 2013 10:20 am • by mws
'Cos there's more than one way to do things :lol:
Code: Select all
#!/bin/bash
printf '%s---\nTitle: "%s"\nDate: %s\n---\n' "" "$(basename -s .txt "$1")" "$(date +%F)" | { echo '0a'; cat ; echo -e ".\nw";} | ed "$1" > /dev/null
mws
 
Posts: 6
Joined: Mon Jul 08, 2013 1:04 pm

Re: Append Text to the Beginning of a File Fri Jul 12, 2013 4:52 pm • by sjk
Where's a Ruby version? :lol:
sjk
 
Posts: 332
Joined: Thu Aug 02, 2007 5:43 pm
Location: Eugene

Re: Append Text to the Beginning of a File Fri Jul 12, 2013 11:16 pm • by pythonicmac
Wow thank you for all the replies even when this post wasn't up to forum rules. I love this community :D
pythonicmac
 
Posts: 4
Joined: Fri Apr 19, 2013 7:05 pm

Re: Append Text to the Beginning of a File Fri Jul 12, 2013 11:18 pm • by pythonicmac
mws wrote: pythonicmac, take your pick!


As my username suggest, I of course have to take the python version :P
pythonicmac
 
Posts: 4
Joined: Fri Apr 19, 2013 7:05 pm


Return to Open Discussion