Thursday, March 3, 2016

Open CSV and TSV Files Correctly and Automatically in *Nix

Opening csv and tsv files automatically in *nix is a pain in the butt as libreoffice (openoffice) prompts the users every time a file is opened and does not automatically set the correct settings. This document is a way to streamline this process so opening them on *nix is very easy as it can be in windows.

In my experience in programming, it is a good idea to stick with strict standards surrounding what a csv file and a tsv file. No abiguity as there exists in RFC4180. It is my view is that csv file is comma separated and has .csv extension. A tsv file is tab separated flat file and has a .tsv extension.

Also tsv or csv files have any special characters escaped using pairs of double quotes. A good reference for such a more strict standard is CSV-1203 which is a valiant effort at creating a real world standard for csv. Updates are pending in CSV-1203 to add tsv to the definitions as per some discussions I have had with the author.

In order to have csv and tsv files open elegantly in *nix, we need to utilize the freedesktop.org Desktop Entry and xdg-mime to create proper association.

For automatically opening a csv file in libreoffice create a file called ~/.local/share/applications/CSV-1203_CSV_open.desktop with the following contents:

#!/usr/bin/env xdg-open

# Instructions on how to deploy
# Get Mime for csv
# $ touch /tmp/dummy.csv
# $ MIME_TYPE=`xdg-mime query filetype /tmp/dummy.csv` && echo $MIME_TYPE
# This returns the mime registered for csv.
# If mime is not returned yet, It will need to get created.
# . This is not covered in these instructions.
# Create Association:
# $ xdg-mime default CSV-1203_CSV_open.desktop $MIME_TYPE
# Verify that mime is properly associated:
# $ xdg-mime query default $MIME_TYPE
# This will should return CSV-1203_CSV_open.desktop

[Desktop Entry]
Encoding=UTF-8
Name=CSV-1203_CSV
Exec=sh -c "libreoffice --calc --infilter=CSV:44,34,UTF8 %f"
#Exec=sh -c "$HOME/.local/share/applications/CSV-1203_CSV_open.sh %f"
Type=Application
Terminal=false
NoDisplay=true



For automatically opening a tsv file in libreoffice create a file called ~/.local/share/applications/CSV-1203_CSV_open.desktop with the following contents:

#!/usr/bin/env xdg-open

# Instructions on how to deploy
# Get Mime for tsv
# $ touch /tmp/dummy.tsv
# $ MIME_TYPE=`xdg-mime query filetype /tmp/dummy.tsv` && echo $MIME_TYPE
# This returns the mime registered for tsv.
# If mime is not returned yet, It will need to get created. 
# . This is not covered in these instructions.
# Create Association:
# $ xdg-mime default CSV-1203_TSV_open.desktop $MIME_TYPE
# Verify that mime is properly associated:
# $ xdg-mime query default $MIME_TYPE
# This will should return CSV-1203_TSV_open.desktop

[Desktop Entry]
Encoding=UTF-8
Name=CSV-1203_TSV
Exec=sh -c "libreoffice --calc --infilter=CSV:9,34,UTF8 %f"
Type=Application
Terminal=false
NoDisplay=true
Name[en_US]=CSV-1203_TSV_open


Notes:

  • These desktop files do not need to be set as executable as they are not menu items.
  • Open office should work for the above. If using openoffice just substitute the executable name above.


By following the instructions in these desktop files, you will have an association to .tsv and .csv files that will automatically launch and open libreoffice.

Good luck!

No comments:

Post a Comment