Skip to content

Instantly share code, notes, and snippets.

@sambacha
Forked from pnc/ocr-shot.sh
Last active February 12, 2021 12:27
Show Gist options
  • Save sambacha/4a9fb2edd2190c5b37ae2d94ce832d19 to your computer and use it in GitHub Desktop.
Save sambacha/4a9fb2edd2190c5b37ae2d94ce832d19 to your computer and use it in GitHub Desktop.

Revisions

  1. sam bacha revised this gist Feb 12, 2021. 2 changed files with 394 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions Info.plist
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
    <key>AMFolderAction</key>
    <string>AMAddingFolderItems</string>
    </dict>
    </plist>
    386 changes: 386 additions & 0 deletions document.wflow
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,386 @@
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
    <key>AMApplicationBuild</key>
    <string>444.4</string>
    <key>AMApplicationVersion</key>
    <string>2.8</string>
    <key>AMDocumentVersion</key>
    <string>2</string>
    <key>actions</key>
    <array>
    <dict>
    <key>action</key>
    <dict>
    <key>AMAccepts</key>
    <dict>
    <key>Container</key>
    <string>List</string>
    <key>Optional</key>
    <false/>
    <key>Types</key>
    <array>
    <string>com.apple.cocoa.path</string>
    </array>
    </dict>
    <key>AMActionVersion</key>
    <string>2.1.1</string>
    <key>AMApplication</key>
    <array>
    <string>Finder</string>
    </array>
    <key>AMParameterProperties</key>
    <dict>
    <key>itemType</key>
    <dict/>
    <key>predicate</key>
    <dict/>
    </dict>
    <key>AMProvides</key>
    <dict>
    <key>Container</key>
    <string>List</string>
    <key>Types</key>
    <array>
    <string>com.apple.cocoa.path</string>
    </array>
    </dict>
    <key>AMRequiredResources</key>
    <array/>
    <key>AMSelectedInputType</key>
    <string>com.apple.cocoa.path</string>
    <key>AMSelectedOutputType</key>
    <string>com.apple.cocoa.path</string>
    <key>ActionBundlePath</key>
    <string>/System/Library/Automator/Filter Finder Items 2.action</string>
    <key>ActionName</key>
    <string>Filter Finder Items</string>
    <key>ActionParameters</key>
    <dict>
    <key>itemType</key>
    <string>com.apple.cocoa.path</string>
    <key>predicate</key>
    <data>
    YnBsaXN0MDDUAQIDBAUGYWJYJHZlcnNpb25Y
    JG9iamVjdHNZJGFyY2hpdmVyVCR0b3ASAAGG
    oK8QFgcIDxQcJicpMDQ5Oj1BRUpLTlRYXF5V
    JG51bGzTCQoLDA0OXxAXTlNDb21wb3VuZFBy
    ZWRpY2F0ZVR5cGVfEA9OU1N1YnByZWRpY2F0
    ZXNWJGNsYXNzEAGAAoAV0hALERNaTlMub2Jq
    ZWN0c6ESgAOAFNQLFRYXGBkaG18QEU5TUmln
    aHRFeHByZXNzaW9uXxAQTlNMZWZ0RXhwcmVz
    c2lvbl8QE05TUHJlZGljYXRlT3BlcmF0b3KA
    E4AOgASAEdUdHh8gCyEiIyQlWU5TT3BlcmFu
    ZF5OU1NlbGVjdG9yTmFtZV8QEE5TRXhwcmVz
    c2lvblR5cGVbTlNBcmd1bWVudHOABoAFEAOA
    CIANXHZhbHVlRm9yS2V5OtIfCwwogAfSKiss
    LVokY2xhc3NuYW1lWCRjbGFzc2VzXxAQTlNT
    ZWxmRXhwcmVzc2lvbqMsLi9cTlNFeHByZXNz
    aW9uWE5TT2JqZWN00hALMTOhMoAJgAzTCx81
    Njc4WU5TS2V5UGF0aIALEAqAClRuYW1l0ior
    OzxfEBxOU0tleVBhdGhTcGVjaWZpZXJFeHBy
    ZXNzaW9uozsuL9IqKz4/Xk5TTXV0YWJsZUFy
    cmF5oz5AL1dOU0FycmF50iorQkNfEBNOU0tl
    eVBhdGhFeHByZXNzaW9upEJELi9fEBROU0Z1
    bmN0aW9uRXhwcmVzc2lvbtNGHwtHSElfEA9O
    U0NvbnN0YW50VmFsdWWADxAAgBBbU2NyZWVu
    IFNob3TSKitMTV8QGU5TQ29uc3RhbnRWYWx1
    ZUV4cHJlc3Npb26jTC4v1AtPUFFSSCNTWk5T
    TW9kaWZpZXJXTlNGbGFnc15OU09wZXJhdG9y
    VHlwZYASEGPSKitVVl8QFU5TSW5QcmVkaWNh
    dGVPcGVyYXRvcqNVVy9fEBNOU1ByZWRpY2F0
    ZU9wZXJhdG9y0iorWVpfEBVOU0NvbXBhcmlz
    b25QcmVkaWNhdGWjWVsvW05TUHJlZGljYXRl
    0iorQF2iQC/SKitfYF8QE05TQ29tcG91bmRQ
    cmVkaWNhdGWjX1svXxAPTlNLZXllZEFyY2hp
    dmVy0WNkVHJvb3SAAQAIABEAGgAjAC0AMgA3
    AFAAVgBdAHcAiQCQAJIAlACWAJsApgCoAKoA
    rAC1AMkA3ADyAPQA9gD4APoBBQEPAR4BMQE9
    AT8BQQFDAUUBRwFUAVkBWwFgAWsBdAGHAYsB
    mAGhAaYBqAGqAawBswG9Ab8BwQHDAcgBzQHs
    AfAB9QIEAggCEAIVAisCMAJHAk4CYAJiAmQC
    ZgJyAncCkwKXAqACqwKzAsICxALGAssC4wLn
    Av0DAgMaAx4DKgMvAzIDNwNNA1EDYwNmA2sA
    AAAAAAACAQAAAAAAAABlAAAAAAAAAAAAAAAA
    AAADbQ==
    </data>
    </dict>
    <key>BundleIdentifier</key>
    <string>com.apple.Automator.FilterFinderItems2</string>
    <key>CFBundleVersion</key>
    <string>2.1.1</string>
    <key>CanShowSelectedItemsWhenRun</key>
    <false/>
    <key>CanShowWhenRun</key>
    <true/>
    <key>Category</key>
    <array>
    <string>AMCategoryFilesAndFolders</string>
    </array>
    <key>Class Name</key>
    <string>Filter_Finder_Items_2</string>
    <key>InputUUID</key>
    <string>140E93D6-5F6F-4FD7-9819-528F845F70D5</string>
    <key>Keywords</key>
    <array/>
    <key>OutputUUID</key>
    <string>B7559886-4293-409D-A45D-CDA9C762F4A1</string>
    <key>UUID</key>
    <string>5C5196EE-525C-4BAF-9B11-4DCE7905AAFD</string>
    <key>UnlocalizedApplications</key>
    <array>
    <string>Finder</string>
    </array>
    <key>arguments</key>
    <dict>
    <key>0</key>
    <dict>
    <key>default value</key>
    <string>com.apple.cocoa.path</string>
    <key>name</key>
    <string>itemType</string>
    <key>required</key>
    <string>0</string>
    <key>type</key>
    <string>0</string>
    <key>uuid</key>
    <string>0</string>
    </dict>
    <key>1</key>
    <dict>
    <key>default value</key>
    <data>
    </data>
    <key>name</key>
    <string>predicate</string>
    <key>required</key>
    <string>0</string>
    <key>type</key>
    <string>0</string>
    <key>uuid</key>
    <string>1</string>
    </dict>
    </dict>
    <key>conversionLabel</key>
    <integer>0</integer>
    <key>isViewVisible</key>
    <true/>
    <key>location</key>
    <string>324.500000:282.000000</string>
    <key>nibPath</key>
    <string>/System/Library/Automator/Filter Finder Items 2.action/Contents/Resources/Base.lproj/main.nib</string>
    </dict>
    <key>isViewVisible</key>
    <true/>
    </dict>
    <dict>
    <key>action</key>
    <dict>
    <key>AMAccepts</key>
    <dict>
    <key>Container</key>
    <string>List</string>
    <key>Optional</key>
    <true/>
    <key>Types</key>
    <array>
    <string>com.apple.cocoa.string</string>
    </array>
    </dict>
    <key>AMActionVersion</key>
    <string>2.0.3</string>
    <key>AMApplication</key>
    <array>
    <string>Automator</string>
    </array>
    <key>AMParameterProperties</key>
    <dict>
    <key>COMMAND_STRING</key>
    <dict/>
    <key>CheckedForUserDefaultShell</key>
    <dict/>
    <key>inputMethod</key>
    <dict/>
    <key>shell</key>
    <dict/>
    <key>source</key>
    <dict/>
    </dict>
    <key>AMProvides</key>
    <dict>
    <key>Container</key>
    <string>List</string>
    <key>Types</key>
    <array>
    <string>com.apple.cocoa.string</string>
    </array>
    </dict>
    <key>ActionBundlePath</key>
    <string>/System/Library/Automator/Run Shell Script.action</string>
    <key>ActionName</key>
    <string>Run Shell Script</string>
    <key>ActionParameters</key>
    <dict>
    <key>COMMAND_STRING</key>
    <string>set -e

    for f in "$@"
    do

    contents=$(/usr/local/bin/tesseract -c language_model_penalty_non_dict_word=0.8 --tessdata-dir /usr/local/share/ "$f" stdout -l eng | /usr/local/bin/xml esc)

    hex=$((cat &lt;&lt;EOF
    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
    &lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
    &lt;plist version="1.0"&gt;
    &lt;string&gt;$contents&lt;/string&gt;
    &lt;/plist&gt;
    EOF
    ) | plutil -convert binary1 - -o - | xxd -p | tr -d '\n')

    xattr -w -x com.apple.metadata:kMDItemFinderComment "$hex" "$f"
    mdimport "$f"

    done</string>
    <key>CheckedForUserDefaultShell</key>
    <true/>
    <key>inputMethod</key>
    <integer>1</integer>
    <key>shell</key>
    <string>/bin/bash</string>
    <key>source</key>
    <string></string>
    </dict>
    <key>BundleIdentifier</key>
    <string>com.apple.RunShellScript</string>
    <key>CFBundleVersion</key>
    <string>2.0.3</string>
    <key>CanShowSelectedItemsWhenRun</key>
    <false/>
    <key>CanShowWhenRun</key>
    <true/>
    <key>Category</key>
    <array>
    <string>AMCategoryUtilities</string>
    </array>
    <key>Class Name</key>
    <string>RunShellScriptAction</string>
    <key>InputUUID</key>
    <string>13FAB0E9-A2DE-4A5D-9029-BC87004CC6E5</string>
    <key>Keywords</key>
    <array>
    <string>Shell</string>
    <string>Script</string>
    <string>Command</string>
    <string>Run</string>
    <string>Unix</string>
    </array>
    <key>OutputUUID</key>
    <string>2DDA5606-67C7-4A16-B38E-DD2C6D74F720</string>
    <key>UUID</key>
    <string>9D7F6F74-06AE-41A6-9D04-A8C3F1391B3D</string>
    <key>UnlocalizedApplications</key>
    <array>
    <string>Automator</string>
    </array>
    <key>arguments</key>
    <dict>
    <key>0</key>
    <dict>
    <key>default value</key>
    <integer>0</integer>
    <key>name</key>
    <string>inputMethod</string>
    <key>required</key>
    <string>0</string>
    <key>type</key>
    <string>0</string>
    <key>uuid</key>
    <string>0</string>
    </dict>
    <key>1</key>
    <dict>
    <key>default value</key>
    <string></string>
    <key>name</key>
    <string>source</string>
    <key>required</key>
    <string>0</string>
    <key>type</key>
    <string>0</string>
    <key>uuid</key>
    <string>1</string>
    </dict>
    <key>2</key>
    <dict>
    <key>default value</key>
    <false/>
    <key>name</key>
    <string>CheckedForUserDefaultShell</string>
    <key>required</key>
    <string>0</string>
    <key>type</key>
    <string>0</string>
    <key>uuid</key>
    <string>2</string>
    </dict>
    <key>3</key>
    <dict>
    <key>default value</key>
    <string></string>
    <key>name</key>
    <string>COMMAND_STRING</string>
    <key>required</key>
    <string>0</string>
    <key>type</key>
    <string>0</string>
    <key>uuid</key>
    <string>3</string>
    </dict>
    <key>4</key>
    <dict>
    <key>default value</key>
    <string>/bin/sh</string>
    <key>name</key>
    <string>shell</string>
    <key>required</key>
    <string>0</string>
    <key>type</key>
    <string>0</string>
    <key>uuid</key>
    <string>4</string>
    </dict>
    </dict>
    <key>conversionLabel</key>
    <integer>0</integer>
    <key>isViewVisible</key>
    <true/>
    <key>location</key>
    <string>324.500000:834.000000</string>
    <key>nibPath</key>
    <string>/System/Library/Automator/Run Shell Script.action/Contents/Resources/English.lproj/main.nib</string>
    </dict>
    <key>isViewVisible</key>
    <true/>
    </dict>
    </array>
    <key>connectors</key>
    <dict>
    <key>787C8332-00F1-4477-8AF9-BC61E7FBFABB</key>
    <dict>
    <key>from</key>
    <string>5C5196EE-525C-4BAF-9B11-4DCE7905AAFD - 5C5196EE-525C-4BAF-9B11-4DCE7905AAFD</string>
    <key>to</key>
    <string>9D7F6F74-06AE-41A6-9D04-A8C3F1391B3D - 9D7F6F74-06AE-41A6-9D04-A8C3F1391B3D</string>
    </dict>
    </dict>
    <key>workflowMetaData</key>
    <dict>
    <key>folderActionFolderPath</key>
    <string>~/Desktop</string>
    <key>workflowTypeIdentifier</key>
    <string>com.apple.Automator.folderAction</string>
    </dict>
    </dict>
    </plist>
  2. sam bacha revised this gist Feb 12, 2021. 3 changed files with 23 additions and 1 deletion.
    17 changes: 17 additions & 0 deletions enhanced_ocr.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    #!/bin/bash

    set -e

    CONTENTS=$(convert "$1" -magnify -alpha remove - | tesseract -c language_model_penalty_non_dict_word=0.8 --tessdata-dir /usr/local/share/ stdin stdout -l eng | xml esc)

    hex=$( (cat <<EOF
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <string>$CONTENTS</string>
    </plist>
    EOF
    ) | plutil -convert binary1 - -o - | xxd -p | tr -d '\n')

    xattr -w -x com.apple.metadata:kMDItemFinderComment "$hex" "$1"
    mdimport "$1"
    4 changes: 4 additions & 0 deletions install.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    #!/bin/bash
    # install pre req's
    brew install tesseract xmlstarlet
    brew install ghostscript imagemagick
    3 changes: 2 additions & 1 deletion ocr-shot.sh
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,8 @@ set -e

    CONTENTS=$(tesseract -c language_model_penalty_non_dict_word=0.8 --tessdata-dir /usr/local/share/tessdata/ "$1" stdout -l eng | xml esc)

    hex=$((cat <<EOF
    # not an arithmetic expression, leave space for ( (
    hex=$( (cat <<EOF
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
  3. @pnc pnc revised this gist Nov 2, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ocr-shot.sh
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    set -e

    CONTENTS=$(tesseract -c language_model_penalty_non_dict_word=0.8 --tessdata-dir /usr/local/share/ "$1" stdout -l eng | xml esc)
    CONTENTS=$(tesseract -c language_model_penalty_non_dict_word=0.8 --tessdata-dir /usr/local/share/tessdata/ "$1" stdout -l eng | xml esc)

    hex=$((cat <<EOF
    <?xml version="1.0" encoding="UTF-8"?>
  4. @pnc pnc revised this gist Mar 17, 2018. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions ocr-shot.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/bash

    set -ev
    set -e

    CONTENTS=$(tesseract -c language_model_penalty_non_dict_word=0.8 --tessdata-dir /usr/local/share/ "$1" stdout -l eng | xml esc)

    @@ -13,7 +13,5 @@ hex=$((cat <<EOF
    EOF
    ) | plutil -convert binary1 - -o - | xxd -p | tr -d '\n')
    echo $hex
    xattr -w -x com.apple.metadata:kMDItemFinderComment "$hex" "$1"
    mdimport "$1"
  5. @pnc pnc created this gist Mar 17, 2018.
    19 changes: 19 additions & 0 deletions ocr-shot.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    #!/bin/bash

    set -ev

    CONTENTS=$(tesseract -c language_model_penalty_non_dict_word=0.8 --tessdata-dir /usr/local/share/ "$1" stdout -l eng | xml esc)

    hex=$((cat <<EOF
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <string>$CONTENTS</string>
    </plist>
    EOF
    ) | plutil -convert binary1 - -o - | xxd -p | tr -d '\n')
    echo $hex
    xattr -w -x com.apple.metadata:kMDItemFinderComment "$hex" "$1"
    mdimport "$1"