How to automate date in template-generated file with Thunar [xfce4]












1















In XFCE4 it is possible to install templates in ~/Templates and they will appear under the Create Document entry of the Context and File Menus.



Now, let's say I create a Python template there (it can be any language) with the following snippet.



#! /usr/bin/env python
# -*- coding: utf-8 -*-
#################################################
# Author: Me
# Date: $(today)
#################################################


My question is how to achieve that $(date) be automatically replaced with the current date when a new file is created using the template. The solution should work for any text-based template like tex, c, cpp, h, html, js, css, etc.



Considering that Thunar is very lightweight it very likely this is not possible. Nonetheless I would like to know if there is a workaround.



Thanks!










share|improve this question























  • NB you use $(today) within the example snippet, instead of $(date)!

    – aplaice
    Jan 21 at 0:39
















1















In XFCE4 it is possible to install templates in ~/Templates and they will appear under the Create Document entry of the Context and File Menus.



Now, let's say I create a Python template there (it can be any language) with the following snippet.



#! /usr/bin/env python
# -*- coding: utf-8 -*-
#################################################
# Author: Me
# Date: $(today)
#################################################


My question is how to achieve that $(date) be automatically replaced with the current date when a new file is created using the template. The solution should work for any text-based template like tex, c, cpp, h, html, js, css, etc.



Considering that Thunar is very lightweight it very likely this is not possible. Nonetheless I would like to know if there is a workaround.



Thanks!










share|improve this question























  • NB you use $(today) within the example snippet, instead of $(date)!

    – aplaice
    Jan 21 at 0:39














1












1








1








In XFCE4 it is possible to install templates in ~/Templates and they will appear under the Create Document entry of the Context and File Menus.



Now, let's say I create a Python template there (it can be any language) with the following snippet.



#! /usr/bin/env python
# -*- coding: utf-8 -*-
#################################################
# Author: Me
# Date: $(today)
#################################################


My question is how to achieve that $(date) be automatically replaced with the current date when a new file is created using the template. The solution should work for any text-based template like tex, c, cpp, h, html, js, css, etc.



Considering that Thunar is very lightweight it very likely this is not possible. Nonetheless I would like to know if there is a workaround.



Thanks!










share|improve this question














In XFCE4 it is possible to install templates in ~/Templates and they will appear under the Create Document entry of the Context and File Menus.



Now, let's say I create a Python template there (it can be any language) with the following snippet.



#! /usr/bin/env python
# -*- coding: utf-8 -*-
#################################################
# Author: Me
# Date: $(today)
#################################################


My question is how to achieve that $(date) be automatically replaced with the current date when a new file is created using the template. The solution should work for any text-based template like tex, c, cpp, h, html, js, css, etc.



Considering that Thunar is very lightweight it very likely this is not possible. Nonetheless I would like to know if there is a workaround.



Thanks!







xubuntu xfce automation thunar templates






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 20 at 22:08









KyordhelKyordhel

112




112













  • NB you use $(today) within the example snippet, instead of $(date)!

    – aplaice
    Jan 21 at 0:39



















  • NB you use $(today) within the example snippet, instead of $(date)!

    – aplaice
    Jan 21 at 0:39

















NB you use $(today) within the example snippet, instead of $(date)!

– aplaice
Jan 21 at 0:39





NB you use $(today) within the example snippet, instead of $(date)!

– aplaice
Jan 21 at 0:39










1 Answer
1






active

oldest

votes


















0














AFAIK you can't use only templates for this. One workaround could be to combine templates with custom actions and a bash script.



Preparation



First, write a script that, when given a file, will replace all occurrences of the string $(date) with the actual date (in some specified format). For instance, create ~/.local/bin/insert_date_into_template with the contents:



#!/bin/bash
# produce dd/mm/yyyy date, other formats possible
date=$(date +"%d/%m/%Y")
# see https://stackoverflow.com/questions/407523/escape-a-string-for-a-sed-replace-pattern
escaped_date=$(echo "$date" | sed 's/[/&]/\&/g')
sed -i 's/$(date)/'"$escaped_date"'/g' "$*"


and make it executable (chmod u+x ~/.local/bin/insert_date_into_template). Hopefully, ~/.local/bin/ is already in your $PATH.



Next, define a new custom action, by going to Edit > Configure custom actions in Thunar, and clicking on the plus sign on the side. Choose an appropriate name (say Insert date into template) and description, and set the command to insert_date_into_template %F (or, if that doesn't work, ~/.local/bin/insert_date_into_template %F).



Operation



Now, if you want to have a new file, pre-filled according to the template, but with $(date) replaced by the current date, you need to carry out two steps:




  1. Create the new file from the template, as you'd normally do (right-click, Create Document etc.).


  2. Invoke the custom action, by right-clicking on the newly created file and selecting Insert date into template (or whatever you named the custom action, above).



(Note that obviously the template needs to contain the string $(date) for this to have any effect. Also obviously don't invoke the custom action on the actual, original templates in ~/Templates!)



Room for improvement



In principle, the process could be condensed to just one step (rather than two), by writing an appropriate custom action. However, things might then get slightly fiddly, as you'd need to have a way of specifying which template (python, LaTeX, cpp etc.) you want to use. (If zenity is available in Xubuntu, you might use that, for the selection of the template.)






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "89"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1111496%2fhow-to-automate-date-in-template-generated-file-with-thunar-xfce4%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    AFAIK you can't use only templates for this. One workaround could be to combine templates with custom actions and a bash script.



    Preparation



    First, write a script that, when given a file, will replace all occurrences of the string $(date) with the actual date (in some specified format). For instance, create ~/.local/bin/insert_date_into_template with the contents:



    #!/bin/bash
    # produce dd/mm/yyyy date, other formats possible
    date=$(date +"%d/%m/%Y")
    # see https://stackoverflow.com/questions/407523/escape-a-string-for-a-sed-replace-pattern
    escaped_date=$(echo "$date" | sed 's/[/&]/\&/g')
    sed -i 's/$(date)/'"$escaped_date"'/g' "$*"


    and make it executable (chmod u+x ~/.local/bin/insert_date_into_template). Hopefully, ~/.local/bin/ is already in your $PATH.



    Next, define a new custom action, by going to Edit > Configure custom actions in Thunar, and clicking on the plus sign on the side. Choose an appropriate name (say Insert date into template) and description, and set the command to insert_date_into_template %F (or, if that doesn't work, ~/.local/bin/insert_date_into_template %F).



    Operation



    Now, if you want to have a new file, pre-filled according to the template, but with $(date) replaced by the current date, you need to carry out two steps:




    1. Create the new file from the template, as you'd normally do (right-click, Create Document etc.).


    2. Invoke the custom action, by right-clicking on the newly created file and selecting Insert date into template (or whatever you named the custom action, above).



    (Note that obviously the template needs to contain the string $(date) for this to have any effect. Also obviously don't invoke the custom action on the actual, original templates in ~/Templates!)



    Room for improvement



    In principle, the process could be condensed to just one step (rather than two), by writing an appropriate custom action. However, things might then get slightly fiddly, as you'd need to have a way of specifying which template (python, LaTeX, cpp etc.) you want to use. (If zenity is available in Xubuntu, you might use that, for the selection of the template.)






    share|improve this answer




























      0














      AFAIK you can't use only templates for this. One workaround could be to combine templates with custom actions and a bash script.



      Preparation



      First, write a script that, when given a file, will replace all occurrences of the string $(date) with the actual date (in some specified format). For instance, create ~/.local/bin/insert_date_into_template with the contents:



      #!/bin/bash
      # produce dd/mm/yyyy date, other formats possible
      date=$(date +"%d/%m/%Y")
      # see https://stackoverflow.com/questions/407523/escape-a-string-for-a-sed-replace-pattern
      escaped_date=$(echo "$date" | sed 's/[/&]/\&/g')
      sed -i 's/$(date)/'"$escaped_date"'/g' "$*"


      and make it executable (chmod u+x ~/.local/bin/insert_date_into_template). Hopefully, ~/.local/bin/ is already in your $PATH.



      Next, define a new custom action, by going to Edit > Configure custom actions in Thunar, and clicking on the plus sign on the side. Choose an appropriate name (say Insert date into template) and description, and set the command to insert_date_into_template %F (or, if that doesn't work, ~/.local/bin/insert_date_into_template %F).



      Operation



      Now, if you want to have a new file, pre-filled according to the template, but with $(date) replaced by the current date, you need to carry out two steps:




      1. Create the new file from the template, as you'd normally do (right-click, Create Document etc.).


      2. Invoke the custom action, by right-clicking on the newly created file and selecting Insert date into template (or whatever you named the custom action, above).



      (Note that obviously the template needs to contain the string $(date) for this to have any effect. Also obviously don't invoke the custom action on the actual, original templates in ~/Templates!)



      Room for improvement



      In principle, the process could be condensed to just one step (rather than two), by writing an appropriate custom action. However, things might then get slightly fiddly, as you'd need to have a way of specifying which template (python, LaTeX, cpp etc.) you want to use. (If zenity is available in Xubuntu, you might use that, for the selection of the template.)






      share|improve this answer


























        0












        0








        0







        AFAIK you can't use only templates for this. One workaround could be to combine templates with custom actions and a bash script.



        Preparation



        First, write a script that, when given a file, will replace all occurrences of the string $(date) with the actual date (in some specified format). For instance, create ~/.local/bin/insert_date_into_template with the contents:



        #!/bin/bash
        # produce dd/mm/yyyy date, other formats possible
        date=$(date +"%d/%m/%Y")
        # see https://stackoverflow.com/questions/407523/escape-a-string-for-a-sed-replace-pattern
        escaped_date=$(echo "$date" | sed 's/[/&]/\&/g')
        sed -i 's/$(date)/'"$escaped_date"'/g' "$*"


        and make it executable (chmod u+x ~/.local/bin/insert_date_into_template). Hopefully, ~/.local/bin/ is already in your $PATH.



        Next, define a new custom action, by going to Edit > Configure custom actions in Thunar, and clicking on the plus sign on the side. Choose an appropriate name (say Insert date into template) and description, and set the command to insert_date_into_template %F (or, if that doesn't work, ~/.local/bin/insert_date_into_template %F).



        Operation



        Now, if you want to have a new file, pre-filled according to the template, but with $(date) replaced by the current date, you need to carry out two steps:




        1. Create the new file from the template, as you'd normally do (right-click, Create Document etc.).


        2. Invoke the custom action, by right-clicking on the newly created file and selecting Insert date into template (or whatever you named the custom action, above).



        (Note that obviously the template needs to contain the string $(date) for this to have any effect. Also obviously don't invoke the custom action on the actual, original templates in ~/Templates!)



        Room for improvement



        In principle, the process could be condensed to just one step (rather than two), by writing an appropriate custom action. However, things might then get slightly fiddly, as you'd need to have a way of specifying which template (python, LaTeX, cpp etc.) you want to use. (If zenity is available in Xubuntu, you might use that, for the selection of the template.)






        share|improve this answer













        AFAIK you can't use only templates for this. One workaround could be to combine templates with custom actions and a bash script.



        Preparation



        First, write a script that, when given a file, will replace all occurrences of the string $(date) with the actual date (in some specified format). For instance, create ~/.local/bin/insert_date_into_template with the contents:



        #!/bin/bash
        # produce dd/mm/yyyy date, other formats possible
        date=$(date +"%d/%m/%Y")
        # see https://stackoverflow.com/questions/407523/escape-a-string-for-a-sed-replace-pattern
        escaped_date=$(echo "$date" | sed 's/[/&]/\&/g')
        sed -i 's/$(date)/'"$escaped_date"'/g' "$*"


        and make it executable (chmod u+x ~/.local/bin/insert_date_into_template). Hopefully, ~/.local/bin/ is already in your $PATH.



        Next, define a new custom action, by going to Edit > Configure custom actions in Thunar, and clicking on the plus sign on the side. Choose an appropriate name (say Insert date into template) and description, and set the command to insert_date_into_template %F (or, if that doesn't work, ~/.local/bin/insert_date_into_template %F).



        Operation



        Now, if you want to have a new file, pre-filled according to the template, but with $(date) replaced by the current date, you need to carry out two steps:




        1. Create the new file from the template, as you'd normally do (right-click, Create Document etc.).


        2. Invoke the custom action, by right-clicking on the newly created file and selecting Insert date into template (or whatever you named the custom action, above).



        (Note that obviously the template needs to contain the string $(date) for this to have any effect. Also obviously don't invoke the custom action on the actual, original templates in ~/Templates!)



        Room for improvement



        In principle, the process could be condensed to just one step (rather than two), by writing an appropriate custom action. However, things might then get slightly fiddly, as you'd need to have a way of specifying which template (python, LaTeX, cpp etc.) you want to use. (If zenity is available in Xubuntu, you might use that, for the selection of the template.)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 21 at 0:37









        aplaiceaplaice

        6301516




        6301516






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Ask Ubuntu!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1111496%2fhow-to-automate-date-in-template-generated-file-with-thunar-xfce4%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?