jobisoft / quicktext

An extension for Thunderbird that lets you create templates that can be easily inserted into your own emails.
Mozilla Public License 2.0
187 stars 53 forks source link

Feature Request: Add attachment file name without file extension #336

Closed fireundubh closed 1 year ago

fireundubh commented 2 years ago

Currently, variables exist for attachment file names and attachment file names with file sizes. A variable for attachment file names without file extensions would also be useful. For example, Project Name.pdf would become simply Project Name and could be more generally used throughout a template without needing to be manually revised.

jobisoft commented 2 years ago

I do not think it is worth the work. This is a scripting engine, where those things can be handled by the user script. If we start with this, where should we stop with providing pre-processed data? @SamuelPlentz ? What do you think?

fireundubh commented 2 years ago

Worth the work? This is low-hanging fruit. These two functions appear in the Mozilla Toolkit:

function getFileExtension(path) {
  if (!path) {
    return "";
  }

  const lastIndex = path.lastIndexOf(".");
  return lastIndex !== -1 ? path.slice(lastIndex + 1).toLowerCase() : "";
}
function getFileExtension(aFile) {
  return Services.io.newFileURI(aFile).QueryInterface(Ci.nsIURL).fileExtension;
}

As for providing preprocessed data... that's a core feature of Quicktext, if not its entire purpose.

const persistentTags  = ['COUNTER', 'ORGATT', 'ORGHEADER', 'VERSION'];
const allowedTags     = ['ATT', 'CLIPBOARD', 'COUNTER', 'DATE', 'FILE', 'IMAGE', 'FROM', 'INPUT', 'ORGATT', 'ORGHEADER', 'SCRIPT', 'SUBJECT', 'TEXT', 'TIME', 'TO', 'URL', 'VERSION', 'SELECTION', 'HEADER'];

Am I to understand you're okay with preprocessing file names and file sizes but you draw the line at file extensions? 🤨

SamuelPlentz commented 2 years ago

I think any pull request extending current possibilities is fine.

For a workaround one could for now use a custom script RemoveExtension like this:

let text = this.mVariables[0];

text = text.replace(/(\.pdf|\.txt|\.docx)/ig, ""); // replace ".pdf", ".txt", ".docx"

return text;

It would be called like this:

[[SCRIPT=RemoveExtension|[[ATT=name|, ]]]]
fireundubh commented 2 years ago

My script-based solution was this:

let filename = await this.mQuicktext.get_att(["name"]);
const lastIndex = filename.lastIndexOf(".");

return lastIndex !== -1 ? filename.slice(0, lastIndex) : filename;

This works for any file extension. No idea what happens when there are multiple attachments.

SamuelPlentz commented 2 years ago

You got a good point there. However, my code handles several files at a time, your code only one.

File1.pdf, File2.png, File3.txt will convert to File1.txt, File2.png, File3 (your code) File1.pdf, File2.png, File3.txt will convert to File1, File2.png, File3 (my code)

Sorry did not mention you already mentioned this.

jobisoft commented 1 year ago

I think this can be closed, since you got a working solution.