Difference between revisions of "Startup script"

From CodeBlocks
Jump to: navigation, search
(Added example)
(Updated info for latest changes)
 
Line 1: Line 1:
 
[[Category:Scripting Code::Blocks]]
 
[[Category:Scripting Code::Blocks]]
  
When Code::Blocks finishes loading, it tries to load a script named ''startup.script''. If it is found it then tries to execute the ''"function main()"'' inside it (if there). The user can edit this script to run arbitrary customization commands on application startup.
+
When Code::Blocks finishes loading, it tries to load a script named ''startup.script''. If it is found it then executes it (no need for ''"function main()"'' inside it anymore). The user can edit this script to run arbitrary customization commands on application startup.
The shipped ''startup.script'' just logs its presence in the application's debug log.
+
The shipped ''startup.script'' demonstrates a couple of the newest scripting features.
  
  function main()
+
  Log(_T("Running startup script"));
  {
+
   
    Log(_T("Running startup script"));
+
// #include any script plugins; this will register them too
  }
+
// un-comment the line below to register a pointless sample plugin
 +
// Include(_T("sample_plugin.script"));
 +
   
 +
// add a menu entry to edit this script
 +
GetScriptingManager().RegisterScriptMenu(_T("edit_startup_script.script"), _T("Settings/-Edit startup script"));
  
  
 
== Example ==
 
== Example ==
As an example, you could register some scripts you have written and bind them to Code::Blocks' menus.
+
An example script is now shipping with Code::Blocks. Please read the last line of the startup.script above:
 
+
To do this, you will need to access the ''[[Scripting commands#Application object|application object]]''. It provides the '''<tt>RegisterScript</tt>''' function which comes handy for our little example :).
+
 
+
First thing you need to do is either:
+
* edit the global startup script (which can be found in the <tt>share/codeblocks/scripts</tt> subfolder of the folder you installed Code::Blocks, or
+
* create you own startup.script in your home folder (if Code::Blocks "sees" the script in your home folder, it will always favor it over the globally installed one). For unix systems this folder would be "<tt>~/.codeblocks/share/codeblocks/scripts</tt>" while for windows XP "<tt>C:\Documents and Settings\''username''\Application Data\codeblocks\Share\codeblocks\scripts</tt>".
+
 
+
Now, let's edit our startup script:
+
 
+
function main()
+
{
+
    Log(_T("Running startup script"));
+
+
    // register a script with Code::Blocks' menus
+
    App.RegisterScript("sample.script", "Scripts/Sample script");
+
}
+
 
+
That wasn't hard, was it? We added a single line which will add a menu "Scripts" in the menu bar. Inside this "Scripts" menu it will add a "Sample script" item. When you click on this item, the script "sample.script" will be executed. Simple :).
+
  
All you have to do now, is create the "sample.script" file in the same folder. Here's an example:
+
// add a menu entry to edit this script
 +
GetScriptingManager().RegisterScriptMenu(_T("edit_startup_script.script"), _T("Settings/-Edit startup script"));
  
// A sample script
+
This actually binds the script named <tt>edit_startup_script.script</tt> to the menu entry <tt>"Settings/-Edit startup script"</tt>.
ShowMessage(_T("Hello from inside the sample script!"));
+
  
And don't forget that Code::Blocks conveniently allows you to edit the "sample.script" at any time. All you have to do is keep SHIFT pressed while clicking on the "Sample script" menu item ;).
+
And don't forget that Code::Blocks conveniently allows you to edit the <tt>edit_startup_script.script</tt> at any time. All you have to do is keep SHIFT pressed while clicking on the <tt>"Edit startup script"</tt> menu item ;).
  
  

Latest revision as of 14:29, 15 December 2006


When Code::Blocks finishes loading, it tries to load a script named startup.script. If it is found it then executes it (no need for "function main()" inside it anymore). The user can edit this script to run arbitrary customization commands on application startup. The shipped startup.script demonstrates a couple of the newest scripting features.

Log(_T("Running startup script"));

// #include any script plugins; this will register them too
// un-comment the line below to register a pointless sample plugin
// Include(_T("sample_plugin.script"));

// add a menu entry to edit this script
GetScriptingManager().RegisterScriptMenu(_T("edit_startup_script.script"), _T("Settings/-Edit startup script"));


Example

An example script is now shipping with Code::Blocks. Please read the last line of the startup.script above:

// add a menu entry to edit this script
GetScriptingManager().RegisterScriptMenu(_T("edit_startup_script.script"), _T("Settings/-Edit startup script"));

This actually binds the script named edit_startup_script.script to the menu entry "Settings/-Edit startup script".

And don't forget that Code::Blocks conveniently allows you to edit the edit_startup_script.script at any time. All you have to do is keep SHIFT pressed while clicking on the "Edit startup script" menu item ;).


See also