-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacos_ocr.py
executable file
·78 lines (62 loc) · 2 KB
/
macos_ocr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
"""
Enhanced script to run native OCR using the OCR Text shortcut on MacOS/iOS with improved error handling and logging.
"""
import subprocess
import argparse
import sys
import platform
import logging
import os
SHORTCUT_NAME = "Extract Text"
# Configure logging
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
def is_macos():
"""
Check if the operating system is MacOS.
Returns:
bool: True if the OS is MacOS, False otherwise.
"""
return platform.system() == "Darwin"
def validate_file_path(file_path):
"""
Validate if the provided file path exists and is accessible.
Args:
file_path (str): Path of the file to check.
Raises:
ValueError: If the file path is invalid or inaccessible.
"""
if not os.path.exists(file_path):
raise ValueError(f"File path does not exist: {file_path}")
if not os.path.isfile(file_path):
raise ValueError(f"Path is not a file: {file_path}")
def run_ocr(file_path):
"""
Run OCR on a file using the OCR Text shortcut.
Args:
file_path (str): Path to the file for OCR processing.
"""
if not is_macos():
logging.error("This script can only be run on MacOS.")
sys.exit(1)
try:
validate_file_path(file_path)
command = ["shortcuts", "run", SHORTCUT_NAME, "-i", file_path]
ocr_out = subprocess.check_output(command).decode("utf-8")
logging.info(ocr_out)
except subprocess.CalledProcessError as e:
logging.error(f"An error occurred while running the OCR: {str(e)}")
sys.exit(1)
except ValueError as e:
logging.error(e)
sys.exit(1)
def main():
"""Putting it all together."""
parser = argparse.ArgumentParser(
description="Run OCR on a file using MacOS's OCR Text shortcut."
)
parser.add_argument("file", help="Path to the file for OCR.")
args = parser.parse_args()
run_ocr(args.file)
if __name__ == "__main__":
main()