Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

TheGenerous

macrumors 65816
Original poster
Nov 14, 2010
1,130
452
I'm an Austronaut
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
 
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

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.
 
  • Like
Reactions: TheGenerous
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
 
  • Like
Reactions: TheGenerous
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"
 
Last edited:
  • Like
Reactions: subsonix
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.
 
  • Like
Reactions: TheGenerous
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.