I’ve been working on some front-end design for a website I’m creating, and that’s okay, but I’m getting a little sick of just making things pretty. I wanted to make another tool, and potentially learn a little something in the process. After much consideration, I decided to go for a tool that compiles Scratch projects into Javascript. http://phosphorus.github.io/, but I don’t really mind. I’ll try to make mine a bit different by allowing the aspect ratio of the project to change, but for the most part I’ll just be re-doing what has been done before (and probably much less elegantly).

The first step was to pick a language, and because this is meant to be a learning experience, I decided to go with Python. I haven’t used Python much and it makes a lot of sense for this project, so that’s what I’m going with.

The next step, for me, was to learn a bit about Python’s file I/O. Ideally, this will be a command line tool where you can give it a .sb2 and it will spit out a .html (the html file will contain everything in it, for easy project portability).

I managed, after a few attempts, to read the project JSON and print it out into the console:

import zipfile
zf = zipfile.ZipFile('myProjectName.sb2', 'r')

print zf.read('project.json')

Of course, myProjectName.sb2 isn’t really an acceptable file name, so I had to let users input their own. I added a command line argument to fix that:

import zipfile, argparse

parser = argparse.ArgumentParser()
parser.add_argument('filename', type=str)
args = parser.parse_args()

zf = zipfile.ZipFile(args.filename, 'r')

print zf.read('project.json')

Now you can run python compile.py yourproject.sb2 to see the json.

The last thing I wanted to do before taking a break was parse the json. It turned out to be surprisingly easy:

import zipfile, argparse, json

parser = argparse.ArgumentParser()
parser.add_argument('filename', type=str)
args = parser.parse_args()

zf = zipfile.ZipFile(args.filename, 'r')

projectJSON = zf.read('project.json')

print json.loads(projectJSON)["costumes"][0]["costumeName"]

Then we can get the name of the first backdrop:

python compile.py project.sb2
> backdrop1

So far this project has been coming along fairly well! There’s nothing too interesting going on just yet, but I’m having fun, and that was really the goal in the first place.