There are 3 ways to interact with a Database through the Scripting API:
-
Directly through the Scripting API
-
Through the
hyperxPython package -
Through the Script Runner
If directly referencing the Scripting API, a user should first see How to Set Up Your Environment to ensure they’ve referenced all necessary assemblies and instantiated the Application class. Next, they need to open a Database using the OpenDatabase method on the Application object, using a fully-qualified, absolute path to the Database. Finally, they should select a Project by passing its name, to access all properties and methods on said Project.
Example Script:
# Script assumes environment has already been set up,
# and application has been instantiated
# as variable `application`.
# Open the database
databasePath = r"C:\absolute\path\to\database.hdb3"
application.OpenDatabase(databasePath)
# If the database version does not match your version of HyperX, you can migrate the database
newDatabasePath = application.Migrate(databasePath)
application.OpenDatabase(newDatabasePath)
# Open a project
projectName = "MyProject"
project = application.SelectProject(projectName)
# Close database
application.CloseDatabase()
The hyperx Python package comes with a utility method for opening a Database, which returns a fully wrapped Database after opening.
Example Script:
import hyperx as hx
if __name__ == "__main__":
dbPath = r"C:\Path\To\Database.hdb3"
projectName = “Project Name”
app = hx.Application(hx._api.Application())
if not app.CheckDatabaseIsUpToDate(dbPath):
dbPath = app.Migrate(dbPath)
print(f"Migrated db to {dbPath}")
with hx.OpenManagedDatabase(dbPath) as db:
db.SelectProject(projectName)
project = db.ActiveProject
print(f"Active project = {project.Name}")
If executing a Script through the Script Runner, the Database (and a Project within that Database) is already open. To access the Project that is actively open in the Database while using the Script Runner, simply reference the ActiveProject property on the Database.
Example Script:
def Run(database):
project = database.ActiveProject
If a user wants to use the hyperx Python package while executing their script through the Script Runner, they will need to wrap their Database in the package’s Application class before accessing any methods or properties on the Database object.
Example Script:
import hyperx as hx
def Run(database_):
# Wrap database in Python wrapper class
database = hx.Application(database_)
project = database.ActiveProject