Skip to content

Instantly share code, notes, and snippets.

@rix1
Last active September 2, 2025 20:55
Show Gist options
  • Select an option

  • Save rix1/8b387af0dd5f6602620bca8a855c960c to your computer and use it in GitHub Desktop.

Select an option

Save rix1/8b387af0dd5f6602620bca8a855c960c 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.
#!/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.")
}
@rix1
Copy link
Author

rix1 commented Dec 1, 2024

Download this icon and place it in the same folder as the script command (or somewhere else, just update the relative path in L9:

// @raycast.icon ./copy-uuid.png

copy-uuid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment