-- Credit: https://stackoverflow.com/a/75779406

use framework "Vision"

on run(argv)
    set ap to current application

    -- Read image content
    set img to ap's NSImage's alloc()'s initWithContentsOfFile:item 1 of argv

    -- Set up request handler using image's raw data
    set handle to ap's VNImageRequestHandler's alloc()'s Â
        initWithData:(img's TIFFRepresentation()) Â
        options:(ap's NSDictionary's dictionary())
    
    -- Initialize text request
    set request to ap's VNRecognizeTextRequest's alloc()'s init()

    -- Set recognition level to accurate
    request's setRecognitionLevel:(ap's VNRequestTextRecognitionLevelAccurate)

    -- Set recognition language to Korean
    request's setRecognitionLanguages:(ap's NSArray's arrayWithObject:"ko-KR")

    -- Perform the request and get the results
    handle's performRequests:{request} |error|:(missing value)

    -- Obtain and return the string values of the results
    set res to ""
    repeat with ocr in request's results()
        set res to res & ((first item in (ocr's topCandidates:1))'s |string|()) & linefeed
    end repeat
    return res
end run
