Quick Script Writing Tutorial

Writing a script

your account must have right-edit-scripts to do this tutorial

For this tutorial we will focus on the simplest kind of script, which is one directly associated to a thought. To make one, the script file name is simply the ID of the thought.

In your thought write:


where nnnnn is the ID of the thought that contains this script. You can find the ID number in the footer at the bottom of the thought. Now your done with the thought. Make sure to check the draft checkbox and then save the thought. If you don't have a thought for this yet, create one and get it's ID before you add the


reference.


The next step is editing the script itself. Since you are in draft mode you will notice that the script reference is visible in the page and that the ID argument is clickable. Click on the ID to edit the script. A javascript editor window will open allowing you to add code.


If your script is super simple and does not involve the content of the thought, you can simply write it direct at this point. However, most of the time you want the script to display something in the thought or do something related to the thought, or be in relation to other items in the page. So to enter a context where all the thought aspects are available, you use the myScript() adverb. This adverb asynchronously generates a thought context for you and executes your code in that context. It can also be used to load other scripts your code may be dependant on.

In the simple form the myScript adverb runs your code as the thought is displayed. Again nnnnn is the ID of the current thought.

myScript(nnnnn, function($, pr) {

   // $ - contains the jQuery hook.
   // pr - is a JSON object that contains the properties (context) for the thought.
   // pr = {
   //    0: the script name
   //    1: the first argument
   //    .... any more arguments by number
   //    id: the thought id
   //    sid: the html element id for the output area attached to this script
   //    cmd: the reference that invoked the script, either !script or !plugin
   //    script: the script name (same as argument 0)
   //    props: [an array of arguments 1 to n]
   //    body: the body of the reference (advanced concept)
   // }

   // -- Your code goes here. --

});

So with a little jQuery and the help of JSON.stringify, we can print our own context property object right into the thought. Useful to see what one looks like.

myScript(nnnnn, function($, pr) {

    $(pr.sid).html('<pre>' + JSON.stringify(pr, null, 4) + '</pre>');

});

In a more practical example, we can make a button and have it do something like put our picture in the thought.

myScript(nnnnn, function($, pr) {

    $('<button>show my image</button>')
    .appendTo(pr.sid)
    .click(function(e) {
        e.preventDefault();
        $('<img/>')
        .attr('src', '/image/user/' + myFbi.session.userid + '/profileimage')
        .appendTo(pr.sid);
    });

});

And there you are! (literally)

If you don't see output, check that you replaced nnnnn with the actual thought ID and that it is correct.

In this simple example each press of the button appends another image into the thought. I'll leave it up to you to code it so that it replaces the image instead of appending another.

All the code in these examples is very basic JavaScript and jQuery. If you don't have knowledge of these languages, many good tutorials for them can be found on the web.


Here is the button example live:



proudly scribed using markdown?

Tags

  1. scripts

Comments


wizzad says
Noted on this tutorial … don’t fork
  • A person does not know the thought number untill the thought is  first saved.  Need to make the thought first, and then edit in the nnnn id. 

See Also

  1. Thought The Dogs Of War with 2 viewings related by tag "scripts".
  2. Thought One man's propaganda is another man's truth is a third man's JOKE with 1 viewings related by tag "scripts".
  3. Thought A terrible question ... with 0 viewings related by tag "scripts".
  4. Thought What we do to each other matters, for that is the very being of our humanity. with 0 viewings related by tag "scripts".
  5. Thought about: Is Iran planning a cataclysmic strike for August 22? with 0 viewings related by tag "scripts".
  6. Thought Murder & Merde with 0 viewings related by tag "scripts".
  7. Thought about: The New Way Forward with 0 viewings related by tag "scripts".
  8. Thought about: Olmert gives IDF green light to expand ground offensive with 0 viewings related by tag "scripts".
  9. Thought Would that it was Otherwise with 0 viewings related by tag "scripts".
  10. Thought UN Resolution 1701 with 0 viewings related by tag "scripts".
  11. Thought The Quintessential Jewish Script with 0 viewings related by tag "scripts".
  12. Thought Other Side of the Story Anyone ? with 0 viewings related by tag "scripts".
  13. Thought about: Al-Qaeda planning militant Islamic state within Iraq with 0 viewings related by tag "scripts".
  14. Thought Where are we in the script ? with 0 viewings related by tag "scripts".
  15. Thought Tracking internal Israeli politics with 0 viewings related by tag "scripts".
  16. Thought One road to peace ... saying things that need to be said. with 0 viewings related by tag "scripts".
  17. Thought Mr President, to surge or not to surge ? with 0 viewings related by tag "scripts".
  18. Thought A fatwa against a potential invasion of Iran with 0 viewings related by tag "scripts".
  19. Thought A Winning Script for Iraq with 0 viewings related by tag "scripts".
  20. Thought Changing Dynamics in Iraq with 0 viewings related by tag "scripts".
  21. Thought A Faint Sound of Peace ... with 0 viewings related by tag "scripts".
  22. Thought Good info on plugins and scripting - not wanting it lost with 0 viewings related by tag "scripts".
  23. Thought Wise Advice From A Former President with 0 viewings related by tag "scripts".
  24. Thought Draft for UNIVERSAL DATA ACCESS syntax with 0 viewings related by tag "scripts".