Skip to main content

Move the live text script into its own repo

ID
b9728da
date
2024-06-06 06:54:13+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
43be4c4
message
Move the live text script into its own repo

Closes https://github.com/alexwlchan/scripts/issues/50
changed files
3 files, 1 addition, 108 deletions

Changed files

macos/.gitattributes (339) → macos/.gitattributes (301)

diff --git a/macos/.gitattributes b/macos/.gitattributes
index f0023d0..d9b2eab 100644
--- a/macos/.gitattributes
+++ b/macos/.gitattributes
@@ -2,7 +2,6 @@ close_tabs linguist-language=JavaScript
 close_specific_tabs linguist-language=JavaScript
 count_tabs linguist-language=JavaScript
 get_focus_mode linguist-language=JavaScript
-get_live_text linguist-language=Swift
 get_photo_sizes linguist-language=Swift
 list_safari_tabs linguist-language=JavaScript
 set_accent_colour linguist-language=Swift

macos/README.md (6406) → macos/README.md (6059)

diff --git a/macos/README.md b/macos/README.md
index f8de8b7..6327862 100644
--- a/macos/README.md
+++ b/macos/README.md
@@ -49,10 +49,6 @@ scripts = [
         "description": "print the size of every item in my Photos Library."
     },
     {
-        "name": "get_live_text [image]",
-        "description": "get OCR'd text for a single image using Live Text"
-    },
-    {
         "name": "list_safari_tabs",
         "description": "print the URL of every tab I have open in Safari"
     },
@@ -156,15 +152,6 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
   </dd>
 
   <dt>
-    <a href="https://github.com/alexwlchan/scripts/blob/main/macos/get_live_text">
-      <code>get_live_text [image]</code>
-    </a>
-  </dt>
-  <dd>
-    get OCR'd text for a single image using Live Text
-  </dd>
-
-  <dt>
     <a href="https://github.com/alexwlchan/scripts/blob/main/macos/list_safari_tabs">
       <code>list_safari_tabs</code>
     </a>
@@ -212,4 +199,4 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
     </p>
   </dd>
 </dl>
-<!-- [[[end]]] (checksum: bb331272b982f8cfc69dea52a1e7f558) -->
+<!-- [[[end]]] (checksum: 548c914e7cb2a6c9d296697e3b067b5c) -->

macos/get_live_text (2766) → macos/get_live_text (0)

diff --git a/macos/get_live_text b/macos/get_live_text
deleted file mode 100755
index d796656..0000000
--- a/macos/get_live_text
+++ /dev/null
@@ -1,93 +0,0 @@
-#!/usr/bin/env swift
-// Get OCR'd text from an image using Live Text in macOS.
-//
-// If you're using Preview, you can use Live Text to copy and paste text
-// that's in an image.  This script allows you to access that text
-// programatically, which is useful if you want to do bulk analysis of
-// the text in your images.
-//
-// You can search text in the Photos app, but this is useful if you:
-//
-//    - want to search images that aren't in your Photos library
-//    - want to do analysis which isn't just searching
-//
-// This is based on https://developer.apple.com/documentation/vision/recognizing_text_in_images
-//
-// Tested on macOS Monterey.
-//
-// You may need to run `chmod +x get_live_text` first and install the Xcode
-// command-line tools.
-//
-// == Usage ==
-//
-// Pass the path to your image as a single command-line argument.  Any text
-// in the image will be returned as a JSON list:
-//
-//      $ get_live_text railway-sign.jpg
-//      ["Passengers must","not pass this point","or cross the line"]
-//
-// If the image doesn't contain any text, it returns an empty list:
-//
-//      $ get_live_text dancers.jpg
-//      []
-//
-
-import Vision
-
-// Process the results of the text-recognition request.
-//
-// This is based on the code in Apple's documentation, and prints the
-// recognized text as a list of JSON strings.
-//
-// See https://developer.apple.com/documentation/vision/recognizing_text_in_images#3601255
-func recognizeTextHandler(request: VNRequest, error: Error?) {
-  guard
-    let observations =
-      request.results as? [VNRecognizedTextObservation]
-  else {
-    return
-  }
-  let recognizedStrings = observations.compactMap { observation in
-    // Return the string of the top VNRecognizedText instance.
-    return observation.topCandidates(1).first?.string
-  }
-
-  // Serialise to JSON
-  do {
-    let jsonData = try JSONSerialization.data(withJSONObject: recognizedStrings)
-
-    if let JSONString = String(data: jsonData, encoding: String.Encoding.utf8) {
-      print(JSONString)
-    }
-  } catch {
-    fputs("Unable to serialise the result as JSON: \(error).\n", stderr)
-    exit(1)
-  }
-}
-
-// Given the path to an image, print a JSON array of text it contains.
-func printTextInImage(imagePath: String) {
-  let requestHandler = VNImageRequestHandler(
-    url: URL(fileURLWithPath: imagePath),
-    options: [:]
-  )
-
-  let request = VNRecognizeTextRequest(completionHandler: recognizeTextHandler)
-
-  do {
-    // Perform the text-recognition request.
-    try requestHandler.perform([request])
-  } catch {
-    fputs("Unable to recognise text: \(error).\n", stderr)
-    exit(1)
-  }
-}
-
-let arguments = CommandLine.arguments
-
-if arguments.count != 2 {
-  fputs("Usage: \(arguments[0]) PATH\n", stderr)
-  exit(1)
-}
-
-printTextInImage(imagePath: arguments[1])