How to load a default project at startup

I have no other solutions :-/ sorry...

Just to doublecheck: you can create an installed Snap! with the following link Snap! Build Your Own Blocks?

I've found out how to load a default project. It's pretty simple actually. Note: all the line numbers that I say, are from unmodified snap. They may be different if the file has already been modified, but it shouldn't too far.

In the source code, go into src/gui.js, and find the IDE_Morph.prototype.openIn (line: 309) function. Then find the function function interpretUrlAnchors (line: 430) inside that. This is where it loads the project from the url.

At the end of this if statement, if (location.hash.substr(0, 6) === '#open:') {} (line: 653), add this script

 else {
    this.getURL(
        '/Examples/vee.xml',
        projectData => {
            var msg;
            this.nextSteps([
                () => msg = this.showMessage('Opening project...'),
                () => {
                    if (projectData.indexOf('<snapdata') === 0) {
                        this.rawOpenCloudDataString(projectData);
                    } else if (
                        projectData.indexOf('<project') === 0
                    ) {
                        this.rawOpenProjectString(projectData);
                    }
                    this.hasChangedMedia = true;
                },
                () => {
                    this.shield.destroy();
                    this.shield = null;
                    msg.destroy();
                    this.toggleAppMode(false);
                }
            ]);
        }
    );
}

Replace '/Examples/vee.xml' with the path to your project. Note, it has to be within the snap folder in order to load a local file. This also only works if you're running snap in a localhost server.

From my test, '/Examples/vee.xml' loaded instantly. I'm genuinely surprised it was that fast.

Here's my modification code

oh, and by the way, this should only load the default project if you don't have any url options (such as #open:, and #present:).

Thanks a lot for all the work you have done for me!! I will try it first thing tomorrow morning.

gui.js must localhost server.
try "--allow-file-access-from-files ", like this: chrome --allow-file-access-from-files file:///D:/Snap-8.0.0/snap.html#open:./Examples/vee.xml

Unfortunately it doesn't work. I have put my default.xml project in my Examples folder, but when I run index.html (or snap.html) I get an "unable to retrieve /Examples/default.xml" error message

Folks, my eyes are bleeding having to read so much "Gepfriemel" as we say in German. Stefano, if you really must at all means make a version of Snap that always loads a "default" project, and if you must make it so you don't use a localhost webserver but that you feel it must be loaded by double-clicking on the "snap.html" file, then the only way to do it is to modify that downloaded version of Snap.

This is NOT anything "advanced" or clever, but simply the rules of how the web works. So, for a moment let me overcome my disdain for this kind of pointless dabbling with "expert" hacks and simply show you how you can do it. Again, there's not the least bit of secret knowledge or "advanced" hackery in here. Three steps:

  1. add another JS file to the sources folder, name it startup.js. define a single function named startup() that returns a string representing the contents of the project you want to show at startup. You can simply open the XML of the project you like and literally copy and paste it into your startup() function. Like this:

  1. edit snap.html and add your startup.js script to the list of scripts the page loads:

  1. at the bottom of the onload() function where it creates the instance of the IDE, pass in the result of calling startup with the openProjectString() method, like this:

That's it.
Enjoy!

Thanks a lot Jens!

:joy: yes, I'm sorry, but I really feel that I must

I'll try your suggestion as soon as I go back to my laptop. But I'm sure it is going to work.

Out of curiosity. Would I get the same result by doing

default = '<project name=etc etc

and then

new IDE_Morph().openIn(world).openProjectString(default);

right?

sure, Stefano.

you can even directly paste the project string into the "openProjectString('here') argument :slight_smile:

Since we cannot programmatically access files with JS we need to use xhr (a webserver) or we have to put the literal data into a JS file that we can load into the page. However, we can't just load an XML file as HTML script, because the src property will be ignored. Because the web is terrible, which in turn is the whole point because folks are terrible and would otherwise abuse everything for scams and vandalism.

Very true

I got the following error:
Uncaught TypeError: Cannot read properties of undefined (reading 'openProjectString')
at window.onload (snap.html:49:46)
:frowning: help~ thx

I downloaded the latest resources through git, and now it can be implemented. But only after the language is initialized, if the language is not English, the interface will be refreshed.

It doesn't work for me either. When I run snap.html I get a blank page, and the Firefox debugger says

Uncaught TypeError: (new IDE_Morph()).openIn(...) is undefined
    onload file:///Users/stefanofederici/Documents/0-AAA-Ambra/Algoritmi/snap.html:50

The end of my onload functions is slightly different from the one shown in Jens' picture. Mine ends with


        world = new WorldMorph(document.getElementById('world'));
        new IDE_Morph().openIn(world);
        loop();
    };

I did exactly as you said, but I got this error

Uncaught TypeError: Cannot read properties of undefined (reading 'openProjectString')
    at window.onload (snap.html:49:46)

Here's my code

<script src="src/startup.js"></script>
        <script>
            var world,
                FPS = 67,
                lastTime = 0;
            window.onload = function () {
                if ('serviceWorker' in navigator) {
                    navigator.serviceWorker.register('sw.js');
                }
                world = new WorldMorph(document.getElementById('world'));
                new IDE_Morph().openIn(world).openProjectString(startup());
                requestAnimationFrame(loop);
            };

man, download the current version of the repo.

huh, it worked when I updated it to the latest version. Is there something you added recently to make it work? It was on this commit before, which was 11 days ago. The only reason I thought it was on the current version, was because I set it up on github desktop to create new branches based off of the snap source, not my main branch.

only around 125 commits in the last 11 days :wink:

This is what I thought, but at GitHub - jmoenig/Snap at v8.0.0 all folders say months/years ago. Is there another branch where to download the development version from?

Look at commits :slight_smile: