Discuss Scratch
- cproctor
-
Scratcher
2 posts
Scratch 2 API
Hi,
I'm a middle school CS teacher and a software developer myself. Does anyone know if there's a working API (or plans for one) in Scratch 2 that would allow access to features such as those documented in the old API docs? For example, I'd love to be able to get a textual representation of project blocks or a json representation of a project. Thanks!
I'm a middle school CS teacher and a software developer myself. Does anyone know if there's a working API (or plans for one) in Scratch 2 that would allow access to features such as those documented in the old API docs? For example, I'd love to be able to get a textual representation of project blocks or a json representation of a project. Thanks!
- blob8108
-
Scratcher
1000+ posts
Scratch 2 API
It's an unofficial & undocumented API, but you can get the project JSON using http://scratch.mit.edu/internalapi/project/<id>/get/ — more on the wiki.
The scratchblocks generator uses that API to convert the blocks in a project into a textual representation suitable for pasting into forum posts.
If you want a different textual representation, you could try writing a Python script that uses Kurt to read the Scratch files.
The scratchblocks generator uses that API to convert the blocks in a project into a textual representation suitable for pasting into forum posts.
If you want a different textual representation, you could try writing a Python script that uses Kurt to read the Scratch files.
- scimonster
-
Scratcher
1000+ posts
Scratch 2 API
@nXIII wrote up a list of the APIs, but I don't have it. You could ask him for a link.
- DadOfMrLog
-
Scratcher
1000+ posts
Scratch 2 API
The scratchblocks generator uses that API to convert the blocks in a project into a textual representation suitable for pasting into forum posts.
Just noticed the generator can't deal with text strings containing ] (not that surprising, really…)
ask [I presume forum scratchblocks have the same "]" problem?] and waitYes, they do!
if ((answer)=[yes]) then
say [Yes, they do!]
else
say [No problemo]
Have fun recoding the parser for that one…

Last edited by DadOfMrLog (Sept. 10, 2013 11:12:55)
- blob8108
-
Scratcher
1000+ posts
Scratch 2 API
Have fun recoding the parser for that one…I was going to add backslash escaping for `]`, but I haven't gotten round to it (= I can't be bothered
). I think it's on the GitHub Issues page, if anyone feels like fixing it…EDIT: The thing is, that the blocks plugin only really supports a subset of Scratch: you can't have brackets in variable names, you can't have strings ending with “ v”, you can't have close square brackets in strings, etc etc. I could add escaping for those things, but it's unintuitive and won't get used by most forumites afaict. So my attitude has been “wait until someone complains”… are you complaining?

Last edited by blob8108 (Sept. 10, 2013 11:29:14)
- DadOfMrLog
-
Scratcher
1000+ posts
Scratch 2 API
Yeah, strings ending in “v” I'd noticed - but it's only this character at a specific position, rather than at any position in the string. Plus, you can kinda make it look passable just by adding space at start and end of string:
I've just been trying to find an ambiguous case with some string containing various [ and ], but I failed (didn't try *very* hard, tho'…)
So far, it looks like it might be always possible to somehow work it out from other hints (checking other delimiters match up, matching the layout & wording of certain blocks, like the “ask” above having “ask” at the start and “and wait” at the end).
I haven't looked at any of the scratchblocks coding to see how it matches the various ‘recognised’ blocks to create the different colours (e.g. above is red because it failed to notice it's of the form "ask […] and wait"), but if it found a match for one of those first (and a normal RegExp should match that, right?), then it could extract and deal with the sub-parts afterwards, couldn't it…?
say [ hi v ]But if you need a ] in a string, there's not much you can do to work around it…
I've just been trying to find an ambiguous case with some string containing various [ and ], but I failed (didn't try *very* hard, tho'…)
So far, it looks like it might be always possible to somehow work it out from other hints (checking other delimiters match up, matching the layout & wording of certain blocks, like the “ask” above having “ask” at the start and “and wait” at the end).
I haven't looked at any of the scratchblocks coding to see how it matches the various ‘recognised’ blocks to create the different colours (e.g. above is red because it failed to notice it's of the form "ask […] and wait"), but if it found a match for one of those first (and a normal RegExp should match that, right?), then it could extract and deal with the sub-parts afterwards, couldn't it…?
Last edited by DadOfMrLog (Sept. 10, 2013 17:19:20)
- mobluse
-
Scratcher
100+ posts
Scratch 2 API
It's an unofficial & undocumented API, but you can get the project JSON using http://scratch.mit.edu/internalapi/project/<id>/get/ — more on the wiki.
It's not always JSON that is returned. For projects uploaded from Scratch 1.4 you get the project in that format, e.g.
http://scratch.mit.edu/internalapi/project/10845221/get/
Those projects that were uploaded using Scratch 1.0 to 1.3 have they been converted to 1.4? I've recently tested to upload from versions earlier than 1.4 but without success. Can Scratch 1.4 open projects from 1.0 to 1.3? (I could test this myself.)
I had an idea about analyzing the JSON to get to know what version of Scratch a project could have been developed with, but now I can't get the JSON of all projects automatically. This program should answer which version a Scratch program could have been developed with. If a program only used blocks that existed in Scratch 1.2 or earlier it could be more easily converted to languages that currently doesn't have strings or lists: e.g. Catrobat (PocketCode). It could also be useful when teaching people how to program, that they could start with projects at the level of earlier versions of Scratch.
Scratch must have some unpublished function that converts between 1.4 and 2.0 because if you edit a 1.4 project it gets upgraded to 2.0. I would like to have this function. It would also for Raspberry Pi-use be good to have a converter from 2.0 to 1.4.
- blob8108
-
Scratcher
1000+ posts
Scratch 2 API
Can Scratch 1.4 open projects from 1.0 to 1.3?Think so, yes.
function that converts between 1.4 and 2.0Kurt, the Python library I wrote, can work with both Scratch 1.4 and 2.0 files. I used it to make the Retro Converter tool, which can convert 2.0 projects to 1.4 ones.
You can easily go the other way and convert 1.4 -> 2.0 using Kurt:
import kurt project = kurt.Project.load("test.sb") # 1.4 file project.convert("scratch20") project.save("test-converted.sb2") # 2.0 file
Last edited by blob8108 (Sept. 25, 2013 21:36:54)
- mobluse
-
Scratcher
100+ posts
Scratch 2 API
I tested it using a 2.0 project and the conversion went without problems (after I changed from vector to bitmap pictures and replaced function calls with broadcasts), but I got three error messages when I tried to open it in Scratch 1.4 (for Ubuntu 10.04): “Message not understood: depth.” It does not open. This was the project:function that converts between 1.4 and 2.0Kurt, the Python library I wrote, can work with both Scratch 1.4 and 2.0 files. I used it to make the Retro Converter tool, which can convert 2.0 projects to 1.4 ones.
http://scratch.mit.edu/projects/12612306/
I could also not open it in Scratch 2 Offline Editor.
You can easily go the other way and convert 1.4 -> 2.0 using Kurt:I discovered that Scratch 2 Offline Editor can open Scratch 1.0-1.4 files (in general but not the converted mentioned above).
- blob8108
-
Scratcher
1000+ posts
Scratch 2 API
I tested it using a 2.0 project and the conversion went without problems (after I changed from vector to bitmap pictures and replaced function calls with broadcasts), but I got three error messages when I tried to open it in Scratch 1.4 (for Ubuntu 10.04): “Message not understood: depth.” It does not open. This was the project:Thanks for pointing this out! Weird bug. It only seems to happen in Scratch's File -> Open dialog — double-clicking the file to open it seems to avoid the problem. I think it's something to do with project thumbnails.
http://scratch.mit.edu/projects/12612306/
I could also not open it in Scratch 2 Offline Editor.
- mobluse
-
Scratcher
100+ posts
Scratch 2 API
I tested it using a 2.0 project and the conversion went without problems (after I changed from vector to bitmap pictures and replaced function calls with broadcasts), but I got three error messages when I tried to open it in Scratch 1.4 (for Ubuntu 10.04): “Message not understood: depth.” It does not open. This was the project:Thanks for pointing this out! Weird bug. It only seems to happen in Scratch's File -> Open dialog — double-clicking the file to open it seems to avoid the problem. I think it's something to do with project thumbnails.
http://scratch.mit.edu/projects/12612306/
I could also not open it in Scratch 2 Offline Editor.
Thanks for the workaround. If you use Save (not Save as) after loading it from the file-system, you have a project that can be opened from inside Scratch 1.4 and 2.0. I've now uploaded my Benchmarks-project as Scratch 1.4 that should be possible to run on Raspberry Pi: http://scratch.mit.edu/projects/12697861/
I discovered that Scratch 1.4 is about 1000 times slower than Scratch 2.
See also: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=77&t=44525&p=427890
Last edited by mobluse (Sept. 26, 2013 16:00:46)
- blob8108
-
Scratcher
1000+ posts
Scratch 2 API
See also: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=77&t=44525&p=427890Cool. Thanks for responding to that topic!
- mobluse
-
Scratcher
100+ posts
Scratch 2 API
Is it possible to download a Scratch 2 project (sb2-file) using the command-line in Linux (Bash)? It is possible to do this with a Scratch 1.4 project (sb-file), e.g.
If you are on Mac OS X w/o Wget but w/ cURL you can try:
Change 12766255 to any Scratch 1.4-project-number you want. You can see that a project is 1.4 by using the Share to-button and if it shows the text Download 1.4 Project it is a 1.4 project (or earlier).
Another way is to use this in Bash, and it shows you 14, 20 or 00:
proj=12766255;wget -O $proj.sb http://scratch.mit.edu/internalapi/project/$proj/get/
If you are on Mac OS X w/o Wget but w/ cURL you can try:
proj=12766255;curl -L -o $proj.sb http://scratch.mit.edu/internalapi/project/$proj/get/
Change 12766255 to any Scratch 1.4-project-number you want. You can see that a project is 1.4 by using the Share to-button and if it shows the text Download 1.4 Project it is a 1.4 project (or earlier).
Another way is to use this in Bash, and it shows you 14, 20 or 00:
proj=12612306; curl -L http://scratch.mit.edu/internalapi/project/$proj/get/ 2> /dev/null | head -1 | sed -n 's/^{.*/20/;t end;s/^ScratchV02.*/14/;t end;s/.*/00/;:end;p'Last edited by mobluse (Oct. 3, 2013 09:55:17)
- BigBlueBlock
-
Scratcher
500+ posts
Scratch 2 API
http://scratch.mit.edu/internalapi/backpack/USERNAME/get 
E.G: http://scratch.mit.edu/internalapi/backpack/BigBlueBlock/get

E.G: http://scratch.mit.edu/internalapi/backpack/BigBlueBlock/get
Last edited by BigBlueBlock (Sept. 29, 2013 17:01:44)
- blob8108
-
Scratcher
1000+ posts
Scratch 2 API
@mobluse Yes, though it may need a little scripting. See Scratch File Format (2.0)
- scimonster
-
Scratcher
1000+ posts
Scratch 2 API
@mobluse Yes, though it may need a little scripting. See Scratch File Format (2.0)The APIs don't get a full SB2 though.
- BigBlueBlock
-
Scratcher
500+ posts
Scratch 2 API
@mobluse Yes, though it may need a little scripting. See Scratch File Format (2.0)The APIs don't get a full SB2 though.

- blob8108
-
Scratcher
1000+ posts
Scratch 2 API
That's why it needs a little scripting.@mobluse Yes, though it may need a little scripting. See Scratch File Format (2.0)The APIs don't get a full SB2 though.

- mobluse
-
Scratcher
100+ posts
Scratch 2 API
I got one step closer to downloading sb2-files using only one command-line. This should be typed on one line in Linux (bash):
This downloads the files that should go in the sb2-file and prints:
“baseLayerID”: -1
“baseLayerMD5”: “05e2b7746ab497f60bec60ee2621d017.png”
“currentCostumeIndex”: 0
“penLayerMD5”: “279467d0d49e152706ed66539b577c00.png”
“baseLayerID”: -1
“baseLayerMD5”: “0b1f9ac7a60b2b39dea39d3299cfa9e4.png”
“baseLayerID”: -1
“baseLayerMD5”: “6f90a2a29a9c14631f253ba1a30dc7ce.png”
The penLayer-image should be renamed to 0.png, otherwise if the value of the line above the filename is not -1 then one renames the file to that number plus the extension. If the value is -1 one can rename the file to the next available integer (I guess) plus the extension and change the corresponding -1 in the project.json file to the chosen integer (search for the MD5 code to find the line). This last step could also be done automatically but it is more advanced programming.
Then you zip:
This file can be opened in Scratch 2.
It's a bit strange that the project.json downloaded using internalapi isn't the same as you get when you download the project using the Scratch 2 GUI.
I did not read the sb2-file specification but guessed the above. It could be wrong but it worked for my test-case.
proj=12612306;wget -O - http://scratch.mit.edu/internalapi/project/$proj/get/ 2> /dev/null | tee project.json | grep -i MD5 | cut -d' ' -f2 | sed 's/[\",]//g' | while read file; do wget -O $file http://scratch.mit.edu/internalapi/asset/$file/get 2> /dev/null;done;sed -n '/MD5/I !{h}; /MD5/I {H;x;p}' project.json | cut -d',' -f1 | sed 's/\t*//'This downloads the files that should go in the sb2-file and prints:
“baseLayerID”: -1
“baseLayerMD5”: “05e2b7746ab497f60bec60ee2621d017.png”
“currentCostumeIndex”: 0
“penLayerMD5”: “279467d0d49e152706ed66539b577c00.png”
“baseLayerID”: -1
“baseLayerMD5”: “0b1f9ac7a60b2b39dea39d3299cfa9e4.png”
“baseLayerID”: -1
“baseLayerMD5”: “6f90a2a29a9c14631f253ba1a30dc7ce.png”
The penLayer-image should be renamed to 0.png, otherwise if the value of the line above the filename is not -1 then one renames the file to that number plus the extension. If the value is -1 one can rename the file to the next available integer (I guess) plus the extension and change the corresponding -1 in the project.json file to the chosen integer (search for the MD5 code to find the line). This last step could also be done automatically but it is more advanced programming.
Then you zip:
zip 12612306.sb2 *
This file can be opened in Scratch 2.
It's a bit strange that the project.json downloaded using internalapi isn't the same as you get when you download the project using the Scratch 2 GUI.
I did not read the sb2-file specification but guessed the above. It could be wrong but it worked for my test-case.
Last edited by mobluse (Oct. 1, 2013 23:13:05)