#!/usr/bin/env swift // Required parameters: // @raycast.schemaVersion 1 // @raycast.title Copy UUID // @raycast.mode silent // @raycast.packageName Developer Tools // Optional parameters: // @raycast.icon ./copy-uuid.png // @raycast.author Rix1 // @raycast.description Extracts a UUID from selected text or clipboard and copies it to the clipboard. import AppKit import Foundation func extractUUID(from text: String) -> String? { let pattern = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}" let regex = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive) let range = NSRange(location: 0, length: text.utf16.count) return regex?.firstMatch(in: text, options: [], range: range).flatMap { Range($0.range, in: text).map { String(text[$0]) } } } // Get text from pasteboard func getPasteboardText() -> String? { return NSPasteboard.general.string(forType: .string) } func copyToPasteboard(_ text: String) { let pasteboard = NSPasteboard.general pasteboard.clearContents() pasteboard.setString(text, forType: .string) } // Simulate CMD+C to get selected text func getSelectedText() -> String? { // Save current pasteboard content let pasteboard = NSPasteboard.general let oldContent = pasteboard.string(forType: .string) // Simulate CMD+C let source = CGEventSource(stateID: .combinedSessionState) let keyDown = CGEvent(keyboardEventSource: source, virtualKey: 0x08, keyDown: true) let keyUp = CGEvent(keyboardEventSource: source, virtualKey: 0x08, keyDown: false) keyDown?.flags = .maskCommand keyUp?.flags = .maskCommand keyDown?.post(tap: .cghidEventTap) keyUp?.post(tap: .cghidEventTap) // Small delay to ensure the pasteboard is updated usleep(50000) // 50ms delay let newContent = pasteboard.string(forType: .string) // Restore original content if it existed if let oldContent = oldContent { pasteboard.clearContents() pasteboard.setString(oldContent, forType: .string) } return newContent } // Main logic if let selectedText = getSelectedText(), !selectedText.isEmpty { if let uuid = extractUUID(from: selectedText) { copyToPasteboard(uuid) print("Copied UUID: \(uuid)") } else { print("No UUID found in selected text.") } } else if let clipboardText = getPasteboardText(), !clipboardText.isEmpty { if let uuid = extractUUID(from: clipboardText) { copyToPasteboard(uuid) print("Copied UUID: \(uuid)") } else { print("No UUID found in clipboard.") } } else { print("No text in selected text or clipboard.") }