Search code examples
pythonshellbatch-filetype-conversionopentype

Modify Python Script to Batch Convert all "WOFF" Files in Directory


I have been using a great open-source python script by GitHub user @hanikesn (see below) that converts a single WOFF file back into OTF format via command line (Terminal on Mac OS X).

As it is now, I drag-and-drop the script to Terminal, then drag-and-drop a WOFF file to Terminal, press Enter, and the script creates an OTF in the same directory. The script can be invoked manually, but I find it much easier and much faster to drag-and-drop. However as a typographer, I work with large font-families, some of them have over 100 styles each, so I need a batch conversion tool. I know there are some tools online, but these usually have a size limit unless you pay for the service, and the results are never as consistent as this script. Besides, I would like to have an offline tool.

I would like to modify it to run on a directory (folder of WOFFs) rather than a single file. Ideally I would like to drag-and-drop the script into terminal, and then drag-and-drop a folder. The script should only attempt to convert files with the .woff extension.

When I asked the creator of the script, he said "This can easily be done with a simple one line shell script:

for file in *.woff; do woff2otf.py $file; done

However I don't know how to implement this. I am not a programmer, but I've had to work with some basic python scripting in my typography projects. So, please explain it to me like I'm 5.

woff2otf.py


Solution

  • If you want create an Drag&drop app, you could:

    • open the Automator.app
    • select "Application"
    • in the left-side Library find the Filter Finder Items action
    • drag it into the right
    • add the extenstion is woff condition
    • in the Library find the Run Shell Script action
    • change the Pass input popup to as arguments
    • drag&drop the your woff2otf.py into the place of the echo (you will get it's full path name)
    • save the app somewhere as woff2otf.app
    • congratz - you're done with your 1st OS X application. :)

    Just drag the woff files into the application icon and it should convert them. I can't test it, because i havent installed python3.

    The final app should be as in the following screenshot:

    enter image description here

    EDIT: If your python3 command isn't in the standard command search $PATH, you must change the line:

    • /path/to/the/woff2otf.py "$f" to
    • /path/to/your/python3 /path/to/the/woff2otf.py "$f"

    I installed python3 using anyenv - e.g. it wont help you. Therefore my python3 is:

    $ type python3
    python3 is /opt/anyenv/envs/pyenv/shims/python3
    

    so the line in the shell script (for me) looks like:

    /opt/anyenv/envs/pyenv/shims/python3 ~/bin/woff2otf.py "$f"
    

    But, your installation is surely different.

    Also could help: