4

I love my command line. I love that I can type:

emacs non-existing-file.txt

and then emacs will start editing a file, that does not exist yet, but if I save it, it will be called non-existing-file.txt.

I am also quite fond of LibreOffice.org, but I have been unable to find a way to do the same trick, as I can with emacs:

lowriter non-existing.odt

will open LibreWriter, but it will not create the file. Instead it will complain that the file does not exist.

Is there a way that I from the command line can tell lowriter that I want to start a new file in this dir called non-existing.odt?

1
  • 1
    Inspired by this comment: file='new.odt'; touch "$file" && libreoffice "$file" & Commented May 4, 2023 at 19:02

2 Answers 2

2

This feature was requested for OpenOffice in 2002 and for LibreOffice in 2011. As of now, neither project has implemented it.

A workaround is to create a blank file somewhere, and make a copy to create a new file. However this retains metadata such as the creation date from the original creation, so the result is not the same as creating a new document. Untested.

#!/bin/sh
# Create a new file if the argument does not exist. Do it only if there is a single
# argument and no option.
if [ $# -eq 1 ]; then
  case "$1" in
    -*) :;;
    *)
      if ! [ -e "$" ]; then
        basename="${1##*/}"
        case "$basename" in
          *.*)
            extension="${basename##*.}"
            cp ~/templates/default."$extension" "$1" || exit $?
        esac
      fi;;
  esac
fi

exec loffice "$@"

Converting an empty file works to create a text document (.odt), but not for other types of documents (with Libreoffice 5.1.6.2). I don't know why.

#!/bin/sh
set -e
# Create a new file if the argument does not exist. Do it only if there is a single
# argument and no option.
if [ $# -eq 1 ]; then
  case "$1" in
    -*) :;;
    *)
      if ! [ -e "$" ]; then
        basename="${1##*/}"
        case "$basename" in
          *.*)
            extension="${basename##*.}"
            empty_file=$(mktemp)
            unoconv -f "$extension" -o "$1" "$empty_file"
            rm -f "$empty_file"
        esac
      fi;;
  esac
fi

exec loffice "$@"
0

Problem and solution

I was hoping that this would be an available, but it's not! I've recently been doing some job applications and manually creating a new CV/cover letter each time is tedious.

My solution was to create a directory called "base" where base a cv and cover letter are stored.

The bash script is then saved in a different 'scripts' folder elsewhere. It has been named 'apply.sh'.

Code, examples, and file structure

An example of the creation of a new cv (or résumé) file:

apply.sh -r important_business_job

# creates...

~/.../your_job_applications_folder/cv_your_name_here_important_business_job.docx

The code I used is here:

# Takes a CV or cover letter and copies it with the role name changed.
#! /bin/bash

if getopts c:r: name
then
    case $name in
        c)OUTFILE= cover_letter_your_name_here_${OPTARG}.docx; INFILE=base/base_cover_letter.docx;;
        r)OUTFILE=cv_your_name_here_${OPTARG}.docx; INFILE=base/base_cv.docx;;
        *)echo "Invalid argument. Please select either 'r' for resume or 'c' for cover letter";;
    esac
    cp $INFILE $OUTFILE
else
    echo "Please select the type of file to create (either 'r' for resume or 'c' for cover letter)"
fi

shift $(($OPTIND -1))

File structure:

your_job_applications_folder
├── base
│   ├── base_cover_letter.docx
│   └── base_cv.docx
└── NEW_FILES_GO_HERE

So, how does this relate to this post?

Using this same 'base' folder setup the code could be changed to copy a blank document from a base.docx document each time, effectively creating a new document. However, this may keep the original metadata...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .