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

2

2

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

Adalbertoadalheid answered 9/12, 2015 at 17:24 Comment(7)
Just open the terminal, type cd /path/to/your/directory and then paste that shell command there. That's it.Ethben
Ok, but where should I put the python script for it to be found with that shell command? And when you say "paste that shell command, you mean the "for file in *.woff; do woff2otf.py $file; done" (without the quotes) right?Adalbertoadalheid
I tried what you said, but I keep getting this error message: zsh: parse error near `done' — which I figured was from typing an extra space or failure to type one...but no matter what I try, I keep getting different parse errors... like I said; I am not a programmer, explain it like I'm 5Adalbertoadalheid
@Ethben a little help?Adalbertoadalheid
Put the 'shell script' content to the file and put this file to your folder. Then run it with bash yourscriptname. File woff2otf.py should reside in the same path. And you may modify a bit this script - put python after do keyword to be sure.Ethben
I had zsh installed but reset Terminal shell to bash. I put woff2otf.py in the same directory as you said, and typed for file in cd /path/to/fonts/and/woff2otf.py and pasted shell command. Output says -bash: syntax error near unexpected token do and no conversion took place.Adalbertoadalheid
I am also confused by your second reply. Your first post said to paste the shell command, but yesterday you said "Put the 'shell script' content to the file and put this file to your folder. Then run it with bash yourscriptname. File woff2otf.py should reside in the same path". 1) I don't know what "put the content to the file" means, or if that was a typo. 2) are you now saying I should be saving the shell script as a .sh file and using it as part of the process, rather than pasting it as you first suggested? 3) if so, can you explain where I should put the .sh script & how to use it?Adalbertoadalheid
R
1

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:

Russian answered 9/12, 2015 at 18:17 Comment(14)
that sounds great, but I followed your instructions and screenshot to the letter (literally), but dragging a folder of WOFF files to the app just displays an dialog box saying The action "Run Shell Script" encountered an error. The same thing happens after selecting a group of WOFF files and dragging to the app. And I DO have python 3 installed, cause the python script works great on its own.Adalbertoadalheid
Then can't help unfortunately. Only for curiosity, what is the full path to your woff2otf.py - e.g. what do you entered in place of the /path/to/the/woff2otf.py ?Russian
I tried 2 different paths: first I tried embedding it in the app itself, like the workflow, but when that didn't work I just put the script in my applications folder and changed the path to /Applications/woff2otf.py. But I don't think the location is the problem, because its the same error no matter what I try.Adalbertoadalheid
Ok, just installed python3. The above automator app works without any flaw and converted the dragged woff files into it. Of course, you must use right paths. If your python3 is in some nonstandard place, the line /path/to/woff2otf.py must be used as: /path/to/python3 /path/to/woff2otf.py "$f".Russian
I installed python3 via Terminal; so its in some hidden subdirectory... how did you install it? And where? Did you have to call python three by using /path/to/python3 /path/to/woff2otf.py "$f" ? Thanks again for all your help.Adalbertoadalheid
I just downloaded 3.5.1 which came out 3 days ago, it had been awhile since I installed it originally. The installer placed a Python 3.5 folder in /Applications/ but I'm not sure what file you meant for me to place in /path/to/python3Adalbertoadalheid
Sorry man, can't help. If you want use terminal, you really must know at least the basics of te shell and the basics of the directory hierarchy - in your OS. The above automator workflow works - so, you can use it - if/when you got over the above basics.Russian
I only just saw your above edit where you went into detail. That was greatly informative and helpful. I mimiced the type python3 command for the python3 path: python3 is /Library/Frameworks/Python.framework/Versions/3.5/bin/python3. Thank you for bearing with me, I appreciate your help and your patience with such a complete beginner. I've started reading the links you posted about learning shell basics.Adalbertoadalheid
Ugh. Still not working http://i.imgur.com/qARhS44.png.Adalbertoadalheid
then why do you not entered /Library/Frameworks/Python.framework/Versions/3.5/bin/python3 but only /Library/Frameworks/Python.framework/Versions/3.5/bin/ ?Russian
Ok it is working. Thank you again! This was my first post, so i don't know the rules on who marks a question as answered. You should be the one to get the credit, if theres a way for you to mark it closed.Adalbertoadalheid
Great, congtraz for your 1st drag&drop application in Mac OSX. :) To mark an answer as accepted, click on the check mark beside the answer to toggle it from hollow to green about how to accept answers read more here.Russian
Since I am a new user without enough rep points, choosing an answer as accepted yields this popup: Thanks for the feedback! Once you earn a total of 15 reputation, your votes will change the publicly displayed post score. i.imgur.com/pDtyGOy.pngAdalbertoadalheid
@Adalbertoadalheid you have accepted the answer. You got +2 rep for this Everything is OK - Thank you and enjoy the automator app.Russian
P
1

You are using zsh, aren't you? so to loop on your WOFF files you could to do

for f in *.woff ; /complete/path/to/your/script.py $f
Pandolfi answered 10/12, 2015 at 12:15 Comment(3)
Re: zsh a few weeks ago i found a githib repo with an automator that installed multiple development tools and libraries. One of those must have been zsh which I didn't know about till you asked and I googled it. I plan on learning more about this stuff and thought it'd come in handy, not confuse me in the meantime. If you think having it installed is going to get in the way of me accomplishing the goal of this post, I will uninstall it. I thought I had been entering the complete path...or are you saying literally type the word /complete/ before the path?Adalbertoadalheid
I didn't realise that zsh came installed with os x, but that installer must've switched my shell from bash to zsh. Anyway I reset it back to default (bash)Adalbertoadalheid
In a comment you said "I keep getting this error message: zsh: parse error near *done*" so I had to think that you were using zsh that has different syntax with repect to do loops, and I suggested to you the right command to use when using the zsh. +|+|+ With /complete/path/to/your/script.py I was meaning that you need to type the complete path name of your script. +|+|+ With respect to which shell you should use in the Terminal... both are good choices but in your case I will stay with bash, because most examples, close to the 100 % of examples, you will see are bash examples.Pandolfi

© 2022 - 2024 — McMap. All rights reserved.