### Get Selected Text using Auto Strategy Source: https://github.com/tisfeng/selectedtextkit/blob/main/README.md Use the shared SelectedTextManager instance to retrieve selected text with the recommended auto strategy, which intelligently falls back through multiple methods. ```swift import SelectedTextKit private let textManager = SelectedTextManager.shared func example() async { do { // Option 1: Use auto strategy (recommended for most cases) if let selectedText = try await textManager.getSelectedText(strategy: .auto) { print("Selected text: \(selectedText)") } // Option 2: Use custom strategy array with ordered fallbacks let strategies: [TextStrategy] = [.accessibility, .menuAction, .shortcut] if let text = try await textManager.getSelectedText(strategies: strategies) { print("Text from custom strategies: \(text)") } // Option 3: Use specific strategies for browsers (order matters) let browserStrategies: [TextStrategy] = [.appleScript, .accessibility] if let text = try await textManager.getSelectedText(strategies: browserStrategies) { print("Text from browser: \(text)") } // Option 4: Use individual strategy methods if let text = try await textManager.getSelectedText(strategy: .menuAction) { print("Text from menu copy: \(text)") } // Option 5: Use shortcut strategy with muted volume if let text = try await textManager.getSelectedText(strategy: .shortcut) { print("Text from shortcut copy: \(text)") } } catch { print("Error: \(error)") } } ``` -------------------------------- ### Define Available Text Strategies Source: https://github.com/tisfeng/selectedtextkit/blob/main/README.md Enumerate all available text retrieval strategies and demonstrate how to create ordered arrays for custom fallback sequences. ```swift // All available strategies public enum TextStrategy { case auto // Intelligent fallback (accessibility → menu action) case accessibility // Get text via Accessibility API case appleScript // Get text from browsers via AppleScript case menuAction // Get text via menu bar copy action case shortcut // Get text via Cmd+C (with muted volume) } // Create ordered strategy arrays (execution order matters!) let browserStrategies: [TextStrategy] = [.appleScript, .accessibility, .menuAction, .shortcut] let fallbackStrategies: [TextStrategy] = [.accessibility, .menuAction, .shortcut] ``` -------------------------------- ### Available Text Strategies Source: https://github.com/tisfeng/selectedtextkit/blob/main/README.md Lists and describes all available text retrieval strategies that can be used with the `getSelectedText` methods. Each strategy offers a different approach to accessing selected text. ```APIDOC ## Available Strategies ### Description Defines the different methods available for retrieving selected text. ### Enum Definition ```swift public enum TextStrategy { case auto // Intelligent fallback (accessibility → menu action) case accessibility // Get text via Accessibility API case appleScript // Get text from browsers via AppleScript case menuAction // Get text via menu bar copy action case shortcut // Get text via Cmd+C (with muted volume) } ``` ### Strategy Details | Strategy | Description | Best For | |----------|-------------|----------| | `.auto` | Smart fallback (accessibility → menu action) | **Recommended** - Most reliable | | `.accessibility` | Direct Accessibility API | Fast, lightweight access | | `.appleScript` | Browser automation via AppleScript | Safari, Chrome, Firefox | | `.menuAction` | System menu bar copy action | When accessibility is limited | | `.shortcut` | Cmd+C with muted system volume | Universal compatibility | ``` -------------------------------- ### Add SelectedTextKit to Package.swift Source: https://github.com/tisfeng/selectedtextkit/blob/main/README.md Add the SelectedTextKit Swift Package Manager dependency to your project's Package.swift file. ```swift dependencies: [ .package(url: "https://github.com/tisfeng/SelectedTextKit.git", from: "2.0.0") ] ``` -------------------------------- ### getSelectedText(strategies: [TextStrategy]) Source: https://github.com/tisfeng/selectedtextkit/blob/main/README.md Retrieves selected text by attempting a series of specified strategies in order. This is useful for creating custom fallback sequences tailored to specific application types or scenarios. ```APIDOC ## getSelectedText(strategies: [TextStrategy]) ### Description Get text using multiple strategies with ordered fallback. ### Method Signature `func getSelectedText(strategies: [TextStrategy]) async throws -> String?` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **strategies** (`[TextStrategy]`) - Required - An array of `TextStrategy` enums defining the order in which to attempt text retrieval. The first strategy that successfully retrieves text will be used. ``` -------------------------------- ### getSelectedText(strategy: TextStrategy) Source: https://github.com/tisfeng/selectedtextkit/blob/main/README.md Retrieves selected text from the active application using a specific, predefined strategy. This method is suitable when you want to enforce a particular method of text retrieval. ```APIDOC ## getSelectedText(strategy: TextStrategy) ### Description Get text using a specific strategy. ### Method Signature `func getSelectedText(strategy: TextStrategy) async throws -> String?` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **strategy** (`TextStrategy`) - Required - The specific strategy to use for retrieving text. Available options include `.auto`, `.accessibility`, `.appleScript`, `.menuAction`, and `.shortcut`. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.