Toad World® Forums

How to run own script that is stored in a package


#1

Hello,

I created a function “ActualizeTriggers” in a script “ScriptActualizeTriggers” in a package “MyMsSqlPackage”.
But I don’t know how to run such a function. I see I can start scripts that aren’t stored in packages that have a method main(). I tried also to call my function directly or from such a script?
I tried to call my function “ActualizeTriggers” from a new script (that is out of packages, created only via Scripting Window) in the ways:

  • ScriptActualizeTriggers.ActualizeTriggers();
  • MyMsSqlPackage.ActualizeTriggers();
    But without any success.

Please how to do that?

Thanks.

Regards,

Lada


#2

Hello Lada,

Let me provide you with some helfpul information.

Firstly, TDM has two types of scripts.
A. Script that you create via Tools menu | Scripting Window. Here you can write or modify simple scripts and execute them via the toolbar available in the Scripting Window. Method main() is always executed.The disadvantage of the Scripting Windows is that it is impossible to save the script inside (workaround - copy and past).

B. Script that you create via Script or Package Explorer. These scripts are usually executed in certain context (e.g. method of an object, Get or Set method of property, Event forms etc.)

-> This is the introduction. Before you start, you should consider which script you want to use.

Now, if we understand well, you want to execute a method that will perform a one-shot action with triggers. For this purpose, both types of scripts can be used. We will try to show them, please see the examples below.

A. Execute Script in Scripting Window
There is one problem here - keyword Instance etc. is not valid here. So, firstly, it is necessary to “put” a model to the script. There are two possible ways how to do it:

a) Via option Show Registered Objects

  1. Open Scripting Window.
  2. From the main menu, select View | Show Registered Objects. -> A new windows will appear (see the attached screenshot).
  3. In the Available Objects box, select the model and click the arrow to add it to the Selected Objects box.
  4. In the Name in Script column or via F2, you can modify the model name the way you call the model from your script.

b) Via the System object this way:
var Application = System.GetInterface(‘Application’);
var Model = Application.Models.GetObjectByName(‘MyModel’);//By name

or

var Application = System.GetInterface(‘Application’);
var Model = Application.Models.GetObject(0);//The parameter in GetObject determines with which model the script should work. 0 is the first model listed in the Application View. 1 is the second model listed in the Application View etc.

Tip! - See the Reference Guide, object List.

B. Execute Script from a Form

Here is a brief list of steps to do. Daril created a sample package for you that demonstrates it and where these instructions are also written - see the Properties of the attached package | Description tab.

If you like to check out the attached package, please copy it to your user packages (where My Package and other add-ons are stored). Default path is:
C:\Documents and Settings\user name\My Documents\Toad Data Modeler\Packages{DCB5CB9B-CF65-4350-86B5-285D246FC5AC}

Note: Open TDM then. If it was opened, remember to restart it. Open MS SQL 2005 model, add entity and click the Triggers tab - see the new button Actualize Triggers.

==============
Steps to do to execute script from a form in a model:

A: Create an add-on package first.
File | New Package | enter a package name and select a Package Extension - a package that you like to extend. Usually, it should be Database XXX where XXX is the name of a database platform. You can also extend more databases at one jump.

B: Create a button on a form.

  1. In Options | Expert Mode | the checkbox Save the definitions to the “My Package” checkbox must be clear. - To be able to save modifications to other packages than My Package.
  2. Open the form where you like to add the button.
  3. Right-click the form and select Customize Form As.
  4. From the Package List, select the add-on package.
  5. Add a button for which you define a name and caption.
  6. Click the x button on the form to close the edit mode and select Save Form To…
  7. Save the add-on package (via Package Explorer).
  8. Restart TDM.

C: Create a new script.

  1. Open Package Explorer and find the add-on package.
  2. Right-click the Scripts folder and select Add Script.
  3. Create a new script of the same name as the form has, e.g. FmPEREntityEdit.
  4. In the script, create a new function of a name that will consists of the button name and event name, e.g. MyButtonOnClick. This function is without any parameter.
  5. Write code of this function and confirm Commit.
  6. Save the package.

If you have any questions, please write us back. Thanks.

Regards,

Vladka + Daril

Message was edited by: vladka
Example How to Run Method of Object.txg (4.17 KB)


#3

Hello Lada,

Let me provide you with some helfpul information.

Firstly, TDM has two types of scripts.
A. Script that you create via Tools menu | Scripting Window. Here you can write or modify simple scripts and execute them via the toolbar available in the Scripting Window. Method main() is always executed.The disadvantage of the Scripting Windows is that it is impossible to save the script inside (workaround - copy and past).

B. Script that you create via Script or Package Explorer. These scripts are usually executed in certain context (e.g. method of an object, Get or Set method of property, Event forms etc.)

-> This is the introduction. Before you start, you should consider which script you want to use.

Now, if we understand well, you want to execute a method that will perform a one-shot action with triggers. For this purpose, both types of scripts can be used. We will try to show them, please see the examples below.

A. Execute Script in Scripting Window
There is one problem here - keyword Instance etc. is not valid here. So, firstly, it is necessary to “put” a model to the script. There are two possible ways how to do it:

a) Via option Show Registered Objects

  1. Open Scripting Window.
  2. From the main menu, select View | Show Registered Objects. -> A new windows will appear (see the attached screenshot).
  3. In the Available Objects box, select the model and click the arrow to add it to the Selected Objects box.
  4. In the Name in Script column or via F2, you can modify the model name the way you call the model from your script.

b) Via the System object this way:
var Application = System.GetInterface(‘Application’);
var Model = Application.Models.GetObjectByName(‘MyModel’);//By name

or

var Application = System.GetInterface(‘Application’);
var Model = Application.Models.GetObject(0);//The parameter in GetObject determines with which model the script should work. 0 is the first model listed in the Application View. 1 is the second model listed in the Application View etc.

Tip! - See the Reference Guide, object List.

B. Execute Script from a Form

Here is a brief list of steps to do. Daril created a sample package for you that demonstrates it and where these instructions are also written - see the Properties of the attached package | Description tab.

If you like to check out the attached package, please copy it to your user packages (where My Package and other add-ons are stored). Default path is:
C:\Documents and Settings\user name\My Documents\Toad Data Modeler\Packages{DCB5CB9B-CF65-4350-86B5-285D246FC5AC}

Note: Open TDM then. If it was opened, remember to restart it. Open MS SQL 2005 model, add entity and click the Triggers tab - see the new button Actualize Triggers.

==============
Steps to do to execute script from a form in a model:

A: Create an add-on package first.
File | New Package | enter a package name and select a Package Extension - a package that you like to extend. Usually, it should be Database XXX where XXX is the name of a database platform. You can also extend more databases at one jump.

B: Create a button on a form.

  1. In Options | Expert Mode | the checkbox Save the definitions to the “My Package” checkbox must be clear. - To be able to save modifications to other packages than My Package.
  2. Open the form where you like to add the button.
  3. Right-click the form and select Customize Form As.
  4. From the Package List, select the add-on package.
  5. Add a button for which you define a name and caption.
  6. Click the x button on the form to close the edit mode and select Save Form To…
  7. Save the add-on package (via Package Explorer).
  8. Restart TDM.

C: Create a new script.

  1. Open Package Explorer and find the add-on package.
  2. Right-click the Scripts folder and select Add Script.
  3. Create a new script of the same name as the form has, e.g. FmPEREntityEdit.
  4. In the script, create a new function of a name that will consists of the button name and event name, e.g. MyButtonOnClick. This function is without any parameter.
  5. Write code of this function and confirm Commit.
  6. Save the package.

If you have any questions, please write us back. Thanks.

Regards,

Vladka + Daril

Message was edited by: vladka
scripting window.png


#4

Hello Vladka + Daril,

thank you for your answer.
It is an interessting summary of the interesting information, but it doesn’t answer my question.

The question was whether and how I can start a function created in a B-script from an A-script.

Let’s imagine a TDM guru in a firm creates few useful functions and saves them to some packages (B-scripts). He connects some of these functions to some buttons or events (I understand that the connection is realised via name of the button and function) and some of them let ready for ad hoc scripts (= A-scripts) and for calling from other B-scripts.
Let’s image there exists a function ActualizeAllTriggers in a package and now I would like to start it from an ad hoc script (A-script). Is it possible? How to do that?

Thank you.

Regards,

Lada


#5

If you want us to make a simple example, please do not hesitate to contact us. You can write directly to Daril. Thanks.

Regards,

Vladka + Daril

Hello Lada,

Unfortunately, it is not possible to call a script from package (type B) from ad hock script (type A). The reason is that script B has set visibility (for which databases it is valid), language and other properties that cannot be determined in script A.

It is possible to call another script only in combination type B - type B. There are several ways how to do it, the most common is “Script Name.FunctionName()”.
Your example could be realized the way that TDM guru would create the functions as functions of an object, e.g. a model. Then it would be possible to call the function via Model.FunctionName().

In future we plan to implement options allowing users to save simple scripts of type A and use them repeatedly.
It will be also possible to add another option to call script B from script A - if users are interested. Let us know what you think of it.