-
-
Save mvandermeulen/b52b2a1f90ede4cfa821f2ce6ef71b24 to your computer and use it in GitHub Desktop.
Raycast script command that extracts a UUID from selected text or clipboard and copies it to the clipboard. See https://www.raycast.com/blog/getting-started-with-script-commands for more info.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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.") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment