Skip to main content

Calling Scripts from Toolbar Icons

You can provide your own toolbar in the CODESYS user interface with up to 32 icons for calling script files. For this you need an ICO file where the icon is stored, and a PY file where the Python script to be called is stored.

In the installation directory or in the program files directory under CODESYS, you create a subdirectory named Script Commands. In that directory, you create the config.json configuration file. Specify the call information outline for each icon in the file. Here you can configure a maximum of 16 icons. You can also store the ICO and PY files in the same directory.

. Location
  • <CODESYS installation directory>\CODESYS\Script Commands

  • Users\<username>\AppData\Local\CODESYS\Script Commands

Example 2. Example

Default installation on Windows 10

C:\Program Files (x86)\CODESYS 3.5.14.0\CODESYS\Script Commands\
C:\Users\r.smith\AppData\Local\CODESYS\Script Commands


Tip

If you store a config.json file with different call information at each of the storage locations, then you can configure up to 32 different icons.

Configuration file

Outline of the configuration file for 2 icons

[
    {
            <icon call information>
    },
    {
            <last icon call information>
    }
]

Outline of the call information <icon call information>

"Name": "<tooltip of the symbol button>",
"Desc": "<description of the symbol button>",
"Icon": "<icon file name>",
"Path": "<path of the script file>"
Table 1. Call information

"Name"

Required

Displayed as symbol tooltip

Example: "Name": "Pause"

"Desc"

Optional

Comment for the icon

Example: "Desc": "Operation pause"

Note: Not yet displayed in the user interface

"Icon"

Required

File path: <directory path>\<icon name>.ico of the icon

Example: "Icon": "pause.ico"

Hint: If the file is in the same folder as the config.json file, then the file name is enough.

"Path"

Required

Path of the Python script <directory path>\<script name>.ico

Example: "Path": "stop.py"

Hint: If the file is in the same folder as the config.json file, then the file name is enough.

"Params"

Note: These parameters are not currently assessed.



Example 3. Example

File config.json

[
        {
                "Name": "Start",
                "Desc": "Starts processing",
                "Icon": "start.ico",
                "Path": "goon.py"
        },
        {
                "Name": "Pause",
                "Desc": "Pause operation",
                "Icon": "pause.ico",
                "Path": "stop.py"
        },
        {
                "Name": "Processing",
                "Desc": "Process again",
                "Icon": "VarStatSmall.ico",
                "Path": "process.py"
        }
]

The following files are located in the Script Commands:

config.json
goon.py
stop.py
process.py
start.ico
pause.ico
VarStatSmall.ico


Creating script calls for a toolbar button

  1. Create the Script Commands folder in one of the storage locations.

    C:\Users\<username>\AppData\Local\CODESYS
  2. Create executable Python files there.

    Example:

    File copy.py

    print("The script COPY.PY is executed")

    File delete.py

    print("The script DELETE.PY is executed")
  3. Create the ICO files for the scripts.

    Example: Copy_before.ico, Copy_below.ico, CopyAll.ico

  4. Create a configuration file config.json there.

    The folder Users\<username>\AppData\Local\CODESYS\Script Commands has the following content:

    _cds_configuration_folder_for_scripts.png
  5. Open config.json and add the outlined call information.

    [
     {
             "Name": "Copy Before",
             "Desc": "Copy something",
             "Icon": "Copy_before.ico",
             "Path": "copy.py
     },
     {
             "Name": "Copy Below",
             "Desc": "Copy something",
             "Icon": "Copy_below.ico",
             "Path": "copy.py"
     },
     {
             "Name": "Copy All",
             "Desc": "Copy something",
             "Icon": "CopyAll.ico",
             "Path": "copy.py"
     },
     {
             "Name": "Delete",
             "Desc": "Delete something",
             "Icon": "Delete.ico",
             "Path": "delete.py"
     }
    ]
  6. Start CODESYS.

    The script files, configuration file, and symbol files are read and provided in the Tools → Customize dialog on the Command Icons tab, in ScriptEngine Commands category.

  7. Open the dialog Tools → Customize and click on the tab toolbars.

  8. Select the empty toolbar there and click the Add Toolbar button.

    A line editor opens at the empty toolbar.

  9. Type in a name (example: User defined toolbar).

    The custom toolbar is displayed in the CODESYS window.

  10. Add the recently imported commands and close the dialog.

    _cds_user_defined_tool_bar.png
  11. Click one of the icons.

    The following output is displayed in the message view.

    _cds_user_defined_tool_bar_messages.png