Scripting Example

Index of All Documentation » Wing Pro Reference Manual » Scripting and Extending Wing »

The scripting facility is documented in detail in the sections that follow, but in most cases it is easiest simply to work from the examples in the scripts directory in the Wing installation, using the rest of this chapter as a reference.

User scripts are usually placed inside a directory named scripts within the User Settings Directory. They can also be placed in scripts inside the Wing installation but this may be harder to manage across updates of Wing.

Try adding a very simple script now by pasting the following into a file called within one of the scripts directories:

import wingapi
def test_script(test_str):
  app = wingapi.gApplication
  v = "Product info is: " + str(app.GetProductInfo())
  v += "\nAnd you typed: %s" % test_str
  wingapi.gApplication.ShowMessageDialog("Test Message", v)

Then select Reload All Scripts from the Edit menu. This is only needed the first time a new script file is added, in order to get Wing to discover it. Afterward, Wing automatically reloads scripts whenever they are saved to disk.

Next execute the script with the Command by Name item in the Edit menu and then type test-script followed by pressing the Enter key in the text entry that appears at the bottom of the IDE window. Wing will ask for the argument test_str using it's builtin argument collection facility. Type a string and then Enter. The script will pop up a modal message dialog.

Next make a trivial edit to the script (e.g., change "And you typed" to "Then you typed"). Save the script and execute the script again. You will see that Wing has automatically reloaded the script and the new text appears in the message dialog.

Finally, make an edit to the script that introduces an error into it. For example, change import wingapi to import wingapi2. Save the script and Wing will show a clickable traceback in the Scripts channel of the Messages tool. This makes it easy to quickly find and fixed errors in scripts during their development.

Enabling Auto-Completion in Extension Scripts

To make life easier, you may want to create a project for your scripting work, and then add WINGHOME/bin to your Python Path in Project Properties, where WINGHOME is replaced with the installation location of Wing or on OS X the full path of the Contents/Resources folder inside of Wing's .app folder. This will make it possible for Wing to show auto-completion and call tips for items inside the module wingapi.

With some additional project setup, it is also possible to debug scripts using Wing IDE. This is described in Debugging Extension Scripts.

That's all there is to basic scripting. The most relevant examples for most simple scripts can be found in in the scripts directory inside the Wing installation. This shows how to access and alter text in the current editor, among other things.