Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions Examples/StepTalk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# StepTalk Integration Examples

This directory contains examples demonstrating how to use StepTalk with the GNUstep NSScripting framework through the GSScriptingStepTalkBridge.

## Overview

The GSScriptingStepTalkBridge allows StepTalk (Smalltalk-based scripting for GNUstep) to interact with scriptable applications using the NSScripting framework. This provides a powerful, dynamic scripting environment that can control applications supporting Apple Event-style scripting.

## Prerequisites

1. GNUstep Base Library with NSScripting support
2. StepTalk framework installed
3. A scriptable application or test target

## Examples

### 1. Basic Command Execution (basic-commands.st)

Demonstrates how to:

- Get the shared bridge instance
- Create and execute simple commands
- Work with command results

### 2. Object Specifiers (object-specifiers.st)

Shows how to:

- Build object specifiers for targeting specific objects
- Chain specifiers for nested objects
- Use different specifier types (index, name, property)

### 3. Complete Application Control (app-control.st)

Demonstrates:

- Creating new objects in an application
- Getting and setting properties
- Querying object counts
- Deleting objects

## Running the Examples

From StepTalk:

```smalltalk
"Load a script file"
Transcript loadScript: 'basic-commands.st'
```

Or execute StepTalk code directly in your application.

## Integration Pattern

The typical pattern for using the bridge is:

1. Get the shared bridge instance
2. Create object specifiers to identify target objects
3. Create commands with appropriate arguments
4. Execute commands and handle results
5. Check for errors in command execution

## See Also

- GSScriptingStepTalkBridge.h - Bridge interface documentation
- NSScriptCommand.h - Command execution
- NSScriptObjectSpecifier.h - Object specification
- StepTalk documentation
177 changes: 177 additions & 0 deletions Examples/StepTalk/advanced-features.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
"============================================
Advanced Features Example
Demonstrates advanced bridge features like
custom command handlers and caching
============================================"

| bridge command result handler |

"Get the shared bridge instance"
bridge := GSScriptingStepTalkBridge sharedBridge.

Transcript show: 'Advanced Bridge Features'; cr.
Transcript show: '======================='; cr; cr.

"Example 1: Custom command handler"
Transcript show: 'Example 1: Custom Command Handler'; cr.

"Create a block that handles custom commands"
handler := [:cmd |
Transcript show: 'Custom handler invoked for command: ',
cmd asString; cr.
'Custom result from StepTalk'.
].

"Register the handler"
bridge
registerCommandHandler: handler
forCommand: 'myCustomCommand'
inSuite: 'CustomSuite'.

Transcript show: 'Registered custom command handler'; cr; cr.

"Example 2: Multiple specifier types"
Transcript show: 'Example 2: Using Multiple Specifier Types'; cr.

"Middle specifier"
| middleSpec |
middleSpec := bridge
createSpecifier: 'middle'
inContainer: nil
forKey: 'documents'
withValue: nil.

Transcript show: 'Created middle specifier'; cr.

"Random specifier"
| randomSpec |
randomSpec := bridge
createSpecifier: 'random'
inContainer: nil
forKey: 'documents'
withValue: nil.

Transcript show: 'Created random specifier'; cr; cr.

"Example 3: Relative specifiers"
Transcript show: 'Example 3: Relative Specifiers'; cr.

| baseSpec relativeSpec |
baseSpec := bridge
createSpecifier: 'name'
inContainer: nil
forKey: 'documents'
withValue: 'MyDocument'.

relativeSpec := bridge
createSpecifier: 'relative'
inContainer: nil
forKey: 'documents'
withValue: (Dictionary new
at: 'base' put: baseSpec;
at: 'position' put: 'after';
yourself).

Transcript show: 'Created relative specifier (document after MyDocument)'; cr; cr.

"Example 4: UniqueID specifier"
Transcript show: 'Example 4: UniqueID Specifier'; cr.

| uidSpec |
uidSpec := bridge
createSpecifier: 'uniqueID'
inContainer: nil
forKey: 'documents'
withValue: 'DOC-12345'.

Transcript show: 'Created uniqueID specifier'; cr; cr.

"Example 5: Complex command with multiple arguments"
Transcript show: 'Example 5: Complex Command Arguments'; cr.

| locationSpec |
locationSpec := bridge
createSpecifier: 'position'
inContainer: nil
forKey: 'documents'
withValue: 'end'.

properties := Dictionary new
at: 'name' put: 'NewDocument';
at: 'modified' put: false;
at: 'visible' put: true;
yourself.

result := bridge
executeCommand: 'create'
forSuite: 'CoreSuite'
withArguments: (Dictionary new
at: 'ObjectClass' put: 'document';
at: 'Location' put: locationSpec;
at: 'WithProperties' put: properties;
yourself).

Transcript show: 'Created document with complex arguments'; cr; cr.

"Example 6: Batch operations"
Transcript show: 'Example 6: Batch Operations'; cr.

| docs |
docs := Array new: 5.

1 to: 5 do: [:i |
| docName |
docName := 'Document', i asString.

docs at: i put: (bridge
createObject: 'document'
atLocation: nil
withProperties: (Dictionary new
at: 'name' put: docName;
yourself)).

Transcript show: 'Created ', docName; cr.
].

Transcript show: 'Created 5 documents in batch'; cr; cr.

"Example 7: Query and filter"
Transcript show: 'Example 7: Querying Objects'; cr.

count := bridge countObjects: 'document' inContainer: nil.
Transcript show: 'Total documents: ', count asString; cr.

"Get all documents (this would need a whose specifier in real use)"
Transcript show: 'To filter, use NSWhoseSpecifier in production'; cr; cr.

"Example 8: Clear cache"
Transcript show: 'Example 8: Cache Management'; cr.
bridge clearCache.
Transcript show: 'Cleared bridge cache'; cr; cr.

"Example 9: Error handling patterns"
Transcript show: 'Example 9: Robust Error Handling'; cr.

[
command := bridge
createCommand: 'get'
forSuite: 'CoreSuite'
withArguments: nil.

result := command executeCommand.

(command scriptErrorNumber = 0)
ifTrue: [
Transcript show: 'Success: ', result asString; cr.
]
ifFalse: [
Transcript show: 'Error ',
command scriptErrorNumber asString,
': ',
command scriptErrorString; cr.
].
] on: Error do: [:ex |
Transcript show: 'Exception caught: ', ex messageText; cr.
].

Transcript cr; show: 'Advanced examples complete!'; cr.
127 changes: 127 additions & 0 deletions Examples/StepTalk/app-control.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
"============================================
Application Control Example
Demonstrates complete application control
using the convenience methods
============================================"

| bridge docSpec windowSpec count result properties |

"Get the shared bridge instance"
bridge := GSScriptingStepTalkBridge sharedBridge.

Transcript show: 'Application Control Examples'; cr.
Transcript show: '==========================='; cr; cr.

"Example 1: Count objects"
Transcript show: 'Example 1: Counting Documents'; cr.
count := bridge countObjects: 'document' inContainer: nil.
Transcript show: 'Current document count: ', count asString; cr; cr.

"Example 2: Create a new document"
Transcript show: 'Example 2: Creating New Document'; cr.
properties := Dictionary new
at: 'name' put: 'UntitledDocument';
at: 'modified' put: false;
yourself.

result := bridge
createObject: 'document'
atLocation: nil
withProperties: properties.

Transcript show: 'Created new document: ', result asString; cr; cr.

"Example 3: Get object by name"
Transcript show: 'Example 3: Getting Object by Name'; cr.
result := bridge
getObject: 'UntitledDocument'
ofType: 'document'
fromContainer: nil.

Transcript show: 'Retrieved document: ', result asString; cr; cr.

"Example 4: Set a property"
Transcript show: 'Example 4: Setting Property'; cr.
docSpec := bridge
createSpecifier: 'name'
inContainer: nil
forKey: 'documents'
withValue: 'UntitledDocument'.

bridge
setProperty: 'name'
ofObject: docSpec
toValue: 'MyDocument'.

Transcript show: 'Changed document name to "MyDocument"'; cr; cr.

"Example 5: Get modified property value"
Transcript show: 'Example 5: Getting Property Value'; cr.
result := bridge
getObject: 'name'
ofType: 'property'
fromContainer: docSpec.

Transcript show: 'Document name is now: ', result asString; cr; cr.

"Example 6: Count again to verify"
Transcript show: 'Example 6: Verify Count Increased'; cr.
count := bridge countObjects: 'document' inContainer: nil.
Transcript show: 'New document count: ', count asString; cr; cr.

"Example 7: Delete object"
Transcript show: 'Example 7: Deleting Object'; cr.
bridge deleteObject: docSpec.
Transcript show: 'Deleted document'; cr; cr.

"Example 8: Final count"
Transcript show: 'Example 8: Final Count'; cr.
count := bridge countObjects: 'document' inContainer: nil.
Transcript show: 'Final document count: ', count asString; cr; cr.

"Example 9: Working with windows"
Transcript show: 'Example 9: Working with Windows'; cr.
count := bridge countObjects: 'window' inContainer: nil.
Transcript show: 'Window count: ', count asString; cr.

windowSpec := bridge
createSpecifier: 'index'
inContainer: nil
forKey: 'windows'
withValue: 1.

"Get window name"
result := bridge
getObject: 'name'
ofType: 'property'
fromContainer: windowSpec.

Transcript show: 'First window name: ', result asString; cr; cr.

"Example 10: Complex nested access"
Transcript show: 'Example 10: Complex Nested Access'; cr.

"Get the third paragraph of the first document"
docSpec := bridge
createSpecifier: 'index'
inContainer: nil
forKey: 'documents'
withValue: 1.

| paraSpec |
paraSpec := bridge
createSpecifier: 'index'
inContainer: docSpec
forKey: 'paragraphs'
withValue: 3.

result := bridge
executeCommand: 'get'
forSuite: 'CoreSuite'
withArguments: (Dictionary new
at: 'ObjectSpecifier' put: paraSpec;
yourself).

Transcript show: 'Third paragraph: ', result asString; cr; cr.

Transcript show: 'Application control examples complete!'; cr.
Loading