Resolved Terminal batch and apply to each file in folder?

Discussion in 'Mac Programming' started by TheGenerous, Aug 27, 2015.

  1. TheGenerous macrumors 6502a

    TheGenerous

    Joined:
    Nov 14, 2010
    Location:
    I'm an Austronaut
    #1
    I'd like to be able to double-click something that automatically converts all markdown files (txt) existing inside a folder into a pdf (or tex) file. I think it's call a Batch but I couldn't understand what to look for.

    I know how to use the Terminal to convert a single markdown file (md, or txt) into a pdf using Pandoc and MacTeX:

    To output a pdf is,
    Code:
    pandoc input-file.txt -s -o output-file.pdf
    And to output a LaTeX file is,
    Code:
    pandoc input-file.txt -s -o output-file.tex
    Thank you
     
  2. MacUser2525 macrumors 68000

    MacUser2525

    Joined:
    Mar 17, 2007
    Location:
    Canada
    #2
    It is called a shell script in *nix operating systems like OS X something like below will do for what you want if the folder never changes.

    Code:
    
    #!/bin/bash
    
    # Change to folder to process
    cd /path/to/folder/to/process
    
    # Now process all .txt files under that folder
    for i in $(find $PWD | grep .txt ); do
     
         pandoc "$i" -s -o "$i".pdf
    
    # Done processing all files
    done
    
    Now Automator will run a shell script so create an app using it with one function to run the bash script and you have your clickable action. You would need to create two script files one for each action you want or add second line to get the .tex file in one go.
     
  3. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #3
    Alternatively. It may also be a good idea to make a service out of this, then the path doesn't need to be defined since the script is then applied to any folder with a cmd-click.

    Code:
    for i in *.txt; do
         pandoc "$i" -s -o "$i".pdf
    done
    
     
  4. TheGenerous, Aug 28, 2015
    Last edited: Aug 28, 2015

    TheGenerous thread starter macrumors 6502a

    TheGenerous

    Joined:
    Nov 14, 2010
    Location:
    I'm an Austronaut
    #4
    yaaaaay!

    Thank you @MacUser2525 & @subsonix ! With your help I was able to look up for a few things and add to your code for robustness. I saved the script as .command and use the Terminal to make it executable. It runs perfect.

    Now it's straight forward to produce good looking documents from plain text with some markdown. It is ideal for working with my japanese notes written in plain text. Exporting them as .tex allows them to be included in other LaTeX projects. The main goal is to have future proof text files I can easily edit with readable formatting and to be able to use them in LaTeX.

    For converting Markdown or plain text to PDF;

    Code:
    #!/bin/bash
    
    # NOTE: To Make it executable run chmod +x <script> in Terminal.
    
    # Run script in current folder
    cd -- "$(dirname "$0")"
    
    # When there are no txt files in the current directory
    shopt -s nullglob
    
    # Now process all .txt files under that folder
    
    for i in *.txt; do
        echo " "
        echo "- Processing "${i%%.*}"..." # The {i%%.*} removes '.txt' from the filename
        pandoc "$i" -s -o "${i%%.*}".pdf --latex-engine=xelatex -V mainfont="YuMincho Medium" # Japanese requires XeLaTeX and specific font
        echo "-- Done"
    done
    
    echo " "
    echo "END"
    To convert Markdown or plain text to .tex;

    Code:
    #!/bin/bash
    
    # NOTE: To Make it executable run chmod +x <script> in Terminal.
    
    # Run script in current folder
    cd -- "$(dirname "$0")"
    
    # When there are no txt files in the current directory
    shopt -s nullglob
    
    # Now process all .txt files under that folder
    for i in *.txt; do
        echo " "
        echo "- Processing "${i%%.*}"..." # The {i%%.*} removes '.txt' from the filename
        pandoc -o "${i%%.*}".tex "$i"
        echo "-- Done"
    done
    
    echo " "
    echo "END"
     
  5. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #5
    Just a thought but now that you have a working solution you may wish to look into Setting up "Folder Actions" in Finder to facilitate dragging files into a "Folder Action" enabled Finder folder to convert files using drag and drop on the Desktop.

    This envolves a little Apple Script which in turn can call Shell script files to do conversions, copying and organizing.
     

Share This Page