Context
The Context API provides essential information about the current execution environment when an action is triggered in Anchorpoint. It gives you access to the selected files, folders, user information, project details, and input parameters defined in your action's YAML configuration.
Usage
The context is obtained using the get_context() function from the anchorpoint module:
import anchorpoint
ctx = anchorpoint.get_context()
Context Class
anchorpoint.context
Properties
File and Folder Information
path(str): The absolute path to the file or folder where the action was triggered. For file actions: path to the selected file. For folder actions: path to the selected folder.folder(str): The absolute path to the parent folder. Always represents the directory containing the selected item.filename(str): The name of the selected file (without path). Empty string for folder actions.suffix(str): The file extension including the dot (e.g.,.py,.txt). Empty string for folders or files without extensions.selected_files(List[str]): List of absolute paths to all selected files. Useful for batch operations on multiple files.type(class: Type): The type of object that triggered the action (file, folder, task, etc.).
User and Project Information
username(str): The username of the current Anchorpoint user.user_id(str): Unique identifier of the current user.workspace_id(str): Unique identifier of the current workspace.project_id(str): Unique identifier of the current project. Empty string if not in a project context.
Task Context
task_id(str): Unique identifier of the task if action was triggered from a task. Empty string if not triggered from a task.task_list_id(str): Unique identifier of the task list containing the task. Empty string if not triggered from a task.
Action Configuration
inputs(Dict[str, Any]): Dictionary containing input values defined in the action's YAML file inputs section. Access custom parameters passed to your action.icon(str): The icon path specified in the action's YAML configuration.module_path(str): Path to the directory containing the action's files.
Browser State
browser_path(str): Current path shown in the Anchorpoint browser. May differ frompathwhen action is triggered on specific items.browser_workspace_id(str): Workspace ID of the current browser view.browser_project_id(str): Project ID of the current browser view.
Methods
Execution Control
-
run_async(func, *args, **kwargs)Executes a function asynchronously with progress indication. Automatically shows progress dialog and handles UI updates. Use for long-running operations to keep UI responsive.Arguments
func: The function to execute asynchronously*args: Positional arguments to pass to the function**kwargs: Keyword arguments to pass to the function
Returns: The return value of the executed function
Examples
Basic File Information
import anchorpoint
ctx = anchorpoint.get_context()
print(f"Selected file: {ctx.filename}")
print(f"File path: {ctx.path}")
print(f"Parent folder: {ctx.folder}")
print(f"File extension: {ctx.suffix}")
Working with Multiple Files
import anchorpoint
import os
ctx = anchorpoint.get_context()
# Process all selected files
for file_path in ctx.selected_files:
filename = os.path.basename(file_path)
print(f"Processing: {filename}")
# Your file processing logic here
Using Input Parameters
In your YAML file:
inputs:
quality:
name: "Quality"
type: "dropdown"
options: ["Low", "Medium", "High"]
default: "Medium"
compress:
name: "Compress Output"
type: "checkbox"
default: true
In your Python script:
import anchorpoint
ctx = anchorpoint.get_context()
# Access the input values
quality = ctx.inputs.get("quality", "Medium")
compress = ctx.inputs.get("compress", True)
print(f"Quality setting: {quality}")
print(f"Compression enabled: {compress}")
Async Operations
import anchorpoint
import time
def long_running_task(file_path, output_path):
# Simulate a time-consuming operation
time.sleep(5)
# Your actual processing logic here
with open(output_path, 'w') as f:
f.write(f"Processed: {file_path}")
return f"Successfully processed {file_path}"
ctx = anchorpoint.get_context()
# Run the task asynchronously with automatic progress dialog
result = ctx.run_async(long_running_task, ctx.path, ctx.path + ".processed")
Project Context
import anchorpoint
import apsync
ctx = anchorpoint.get_context()
api = anchorpoint.get_api()
# Check if we're in a project
if ctx.project_id:
project = api.get_project()
if project:
print(f"Working in project: {project.name}")
print(f"Project path: {project.path}")
else:
print("Not in a project context")
# Access workspace information
print(f"Workspace ID: {ctx.workspace_id}")
print(f"Current user: {ctx.username}")