Day One import shell script - need help

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Day One import shell script - need help Fri Mar 08, 2013 4:39 pm • by gcoghill
I need a hand converting this shell script to work with Hazel, so I can import .txt files to the Day One app:

Code: Select all
# Part of the txt-to-dayone repository: https://github.com/christophersu/txt-to-dayone
IFS=$'\n'
files=$(ls | egrep '(.txt)')
for i in $files; do
   created=$( GetFileInfo -d "$i" )
   dayone -d="$created" new < "$i"
done
unset IFS

More info here: http://dayoneapp.com/tools/ - there might be a better way to do this than the script I found. The suggested code from the developers for importing a note.txt file seems simpler:

Code: Select all
Creates a new entry using yesterday’s date with the specified text

$ ./dayone -d="03/03/2011 5:30PM" -s=true new < ~/Desktop/note.txt

My goal is to save text files in a folder, files named "2013-03-08 3:35 PM.txt" with the contents of the file added as the body of the entry. I can tweak the file name if necessary. I have the Day One CLI tools installed.

Thanks!
gcoghill
 
Posts: 290
Joined: Tue Sep 18, 2007 8:09 am
Location: Kent, OH

Re: Day One import shell script - need help Fri Mar 08, 2013 4:51 pm • by a_freyer
Alright, we have lots of problems here.

First, it is never appropriate to parse the output of ls. Basically, since filenames can include any characters, parsing of a list of filenames is going to be unreliable. That said, all the files=$(ls | egrep '(.txt)') line does is search for *.txt files.

Second, setting the the delimiter to line may very well screw with the dayone app. That is not good practice.

Third, you will need to specify the full path for day one. I do not have it installed, but a quick google search told me the path was this:

Code: Select all
for f in *.txt; /usr/local/bin/dayone -d="$(GetFileInfo -d "$f")" new < "$f"; done


EDIT - to clarify, this will parse over all files in the directory and import them. if you want to import one file at a time, then we'll have to modify this script a bit.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Day One import shell script - need help Fri Mar 08, 2013 5:01 pm • by gcoghill
Thank you. I should have noted that my scripting is pretty much non-existent. I can usually figure out what is going on but my knowledge of commands is zero. I reverse-engineer and copy/paste.

In the Hazel script field, it mentions using $1 for the file you are referring to. Should I replace $f with $1? I got a script error when running as you provided. Your full path for Day One seems correct here.
gcoghill
 
Posts: 290
Joined: Tue Sep 18, 2007 8:09 am
Location: Kent, OH

Re: Day One import shell script - need help Fri Mar 08, 2013 5:03 pm • by a_freyer
EDIT - See the following post.
Last edited by a_freyer on Fri Mar 08, 2013 5:11 pm, edited 1 time in total.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Day One import shell script - need help Fri Mar 08, 2013 5:10 pm • by a_freyer
Matching the folder:
Reference to "$1" is not necessary because Hazel runs this script within the matched directory:
Code: Select all
for f in *.txt; /usr/local/bin/dayone -d="$(GetFileInfo -d "$f")" new < "$f"; done


Matching the file:
Reference to $1 is in the basename
Code: Select all
BASE=${1##*/}
BASE=${BASE%.txt}
/usr/local/bin/dayone -d="$BASE" new < "$1"


Line 1: extracts the file name without the extension
Line 2: extracts the base filename without the path
Line 3: passes the filename, which is the date you're looking for to dayone

EDIT - removed comments from code block for readability
Last edited by a_freyer on Fri Mar 08, 2013 5:14 pm, edited 1 time in total.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Day One import shell script - need help Fri Mar 08, 2013 5:12 pm • by gcoghill
Folder containing files, as below. And I did set color label to None for one file when testing.

Image

Image

What I am doing is saving text files to Dropbox, via Drafts on iPhone. I want Day One to grab those text files and create new entries in the Mac app. Thank you.
gcoghill
 
Posts: 290
Joined: Tue Sep 18, 2007 8:09 am
Location: Kent, OH

Re: Day One import shell script - need help Fri Mar 08, 2013 5:15 pm • by a_freyer
Replying again here so you get another notification. I updated my answers while you were typing your reply.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Day One import shell script - need help Fri Mar 08, 2013 5:15 pm • by gcoghill
I will try your new script, thank you. I wonder if I need to match the date formatting in the developer's sample code? That could be an issue.
gcoghill
 
Posts: 290
Joined: Tue Sep 18, 2007 8:09 am
Location: Kent, OH

Re: Day One import shell script - need help Fri Mar 08, 2013 5:20 pm • by a_freyer
Likely, yes you'll have to adjust the date format properly. That's the next thing that we can worry about.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Day One import shell script - need help Fri Mar 08, 2013 5:28 pm • by gcoghill
OK great news - new script works! Seems Dropbox doesn't allow colons in filenames, but that is not an issue. Neither is the date formatting, Day One recognizes both "03/07/2013 10/03PM.txt" and "2013-03-07 10/03 PM.txt"

The odd thing is that using the name "2013-03-07 10/03 PM.txt" added the entry at 5:03PM, and using "03/07/2013 10/03PM.txt" added the entry on 3/08 (today's date).
gcoghill
 
Posts: 290
Joined: Tue Sep 18, 2007 8:09 am
Location: Kent, OH

Re: Day One import shell script - need help Fri Mar 08, 2013 5:34 pm • by a_freyer
It doesn't sound like day one is recognizing the date format.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Day One import shell script - need help Fri Mar 08, 2013 5:37 pm • by a_freyer
If you're creating the files at the time you want them entered into dayone, then this will work (and you can name your files whatever you'd like):


Code: Select all
/usr/local/bin/dayone -d="$(GetFileInfo -d "$1")" new < "$1"
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Day One import shell script - need help Fri Mar 08, 2013 5:39 pm • by gcoghill
a_freyer wrote:It doesn't sound like day one is recognizing the date format.

Agreed. Looks like OS X forbids colons in filenames. Is there a way to reformat this in the script? Sorry to put you to all this work! Much appreciated.
gcoghill
 
Posts: 290
Joined: Tue Sep 18, 2007 8:09 am
Location: Kent, OH

Re: Day One import shell script - need help Fri Mar 08, 2013 5:41 pm • by gcoghill
a_freyer wrote:If you're creating the files at the time you want them entered into dayone, then this will work (and you can name your files whatever you'd like):


Code: Select all
/usr/local/bin/dayone -d="$(GetFileInfo -d "$1")" new < "$1"


That will more than suffice. Time stamp does not need to be exact. Thanks again for all the help! I really appreciate it. I'll post this info on the blog, if you want direct credit to your website let me know.
gcoghill
 
Posts: 290
Joined: Tue Sep 18, 2007 8:09 am
Location: Kent, OH

Re: Day One import shell script - need help Fri Mar 08, 2013 5:44 pm • by a_freyer
Great! Glad to hear that we got this working.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Next

Return to Support