[[BackLinksMenu]] = 10. Working with Scripting = This release of Sophie 2 brings scripting functionality to Sophie. Scripting in Sophie 2 uses JavaScript. Scripts used in Sophie books can either be written in Sophie or outside of Sophie (and saved as ''.js'' text files). They can be run either using the '''script editor''' or invoked from a link. You don't need to have a book open to use scripting: scripts can be run from the book desktop. Sophie books, pages, and frames can be accessed using the objects, methods, and properties in Sophie's document object model. Basic documentation of the document object model that Sophie uses can be found [wiki:SCRIPTING_ACTIONS_API_R0#BasicUserDocumentation here]. This will be expanded as scripting support is extended inside Sophie. You can find [http://www.javascript.com/ introductions] to JavaScript available elsewhere on the web. Tutorials on using scripting in Sophie will be added as Sophie development continues. == The script editor and attaching scripts to books == To access the script editor, choose '''Insert > Script''' from the menu bars. The script editor will open. It looks like this: [[Image(source:/branches/private/danvisel/sophie2-platform/doc/userdoc-images/RELEASE_11/ScriptEditor.png)]] Enter your script in the top half of this window; click the '''Run''' button to try out your script. If there's a result from your script, it will be displayed in the bottom half of the window. To use a JavaScript that you've created outside of Sophie, choose '''Insert > Script File''' from the menu bars. The script will be opened in the script editor window. Closing the script editor window will save the script. If you want to re-edit a script that you've already added to the book, go to the '''Resources''' tab in the right flap. Your scripts will be listed as resources of the book; if you have too many resources in the book, click the '''Script''' button in the top palette so you only see scripts. When you find the script you want to edit, select it; a preview will be displayed in the bottom palette: [[Image(source:/branches/private/danvisel/sophie2-platform/doc/userdoc-images/RELEASE_11/ScriptsInResources.png)]] This screenshot shows both a script created inside Sophie (''Script A'') and one created outside of Sophie (''myscript.js''). To open a script in the script editor, click the '''Open''' button. If you want to delete the script from the book, click the '''Delete''' button. == Invoking scripts using a link == To invoke a script from a link, make a link as you normally would and choose the action '''Run script...''' in the link HUD. You'll then see a drop-down menu listing all the scripts in a book; choose the script that you want to run. It looks like this: [[Image(source:/branches/private/danvisel/sophie2-platform/doc/userdoc-images/RELEASE_11/LinkInvokesScript.png)]] == Sample scripts == This script, which can be run with no books open, creates a new book, then gives that new book the title "My New Book": {{{ var b = app.newBook() b.title = 'My New Book' }}} This script goes through all the frames on the first page of an already existing book and resizes them to 50 pixels by 50 pixels: {{{ for (i = 0; i < book.pages[0].frames.length; i++) book.pages[0].frames[i].size = new ImmSize(50, 50) }}}