如何在 AppleScript 中将文件作为字节数组读取 [英] How do I read a file as a byte array in AppleScript

查看:20
本文介绍了如何在 AppleScript 中将文件作为字节数组读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 AppleScript 或 JXA 读取文件字节(我不知道哪个更好).我已经试过这个代码:

I am trying to read filebytes using AppleScript or JXA (I don't know which one is better yet). I already have tried this code:

set theFile to (choose file with prompt "Select a file to read:")
open for access theFile
set fileContents to (read theFile)
close access theFile

但是,该代码会将文件作为字符串读取并将其存储在 fileContents 中.我需要这是一个字节数组.

However that code will read the file as a string and store it in fileContents. I need this to be a byte array.

推荐答案

我知道我以前在某处见过这个.有一个 MacScripter 上的旧帖子,人们在其中深入探讨了这个问题.如果您有这种倾向,这本书非常值得一读,但最简单的版本似乎是这样的:

I knew I'd seen this somewhere before. There's an old post at MacScripter where people dive into this problem fairly deeply. It's well worth a read if you're inclined that way, but the simplest version seems to be this:

set theFile to choose file
set theBytes to getByteValues(theFile)

on getByteValues(thisFile) -- thisFile's an alias or a file specifier.
    script o
        property integerValues : {}
        property byteValues : {}

        on convertBytesToHex()
            repeat with thisItem in byteValues
                set s to ""
                repeat until contents of thisItem = 0
                    tell (thisItem mod 16)
                        if it > 9 then
                            set s to character (it - 9) of "ABCDEF" & s
                        else
                            set s to (it as string) & s
                        end if
                    end tell
                    set contents of thisItem to thisItem div 16
                end repeat
                set contents of thisItem to s
            end repeat
        end convertBytesToHex
    end script

    set fRef to (open for access thisFile)
    try
        -- The file will be read as a set of 4-byte integers, but does it contain an exact multiple of 4 bytes?
        set oddByteCount to (get eof fRef) mod 4
        set thereAreOddBytes to (oddByteCount > 0)
        -- If the number of bytes isn't a multiple of 4, treat the odd ones as being in the first four, then …
        if (thereAreOddBytes) then set end of o's integerValues to (read fRef from 1 for 4 as unsigned integer)
        -- … read integers from after the odd bytes (if any) to the end of the file.
        set o's integerValues to o's integerValues & (read fRef from (oddByteCount + 1) as unsigned integer)
        close access fRef
    on error errMsg number errNum
        close access fRef
        error errMsg number errNum
    end try

    -- Extract the odd-byte values (if any) from the first integer.
    if (thereAreOddBytes) then
        set n to beginning of o's integerValues
        repeat oddByteCount times
            set end of o's byteValues to n div 16777216
            set n to n mod 16777216 * 256
        end repeat
    end if
    -- Extract the 4 byte values from each of the remaining integers.
    repeat with i from 1 + ((thereAreOddBytes) as integer) to (count o's integerValues)
        set n to item i of o's integerValues
        set end of o's byteValues to n div 16777216
        set end of o's byteValues to n mod 16777216 div 65536
        set end of o's byteValues to n mod 65536 div 256
        set end of o's byteValues to n mod 256 div 1
    end repeat

    o's convertBytesToHex()

    return o's byteValues
end getByteValues

on convertNumberToHex(aNumber)
    set s to ""
    set n to get aNumber
    repeat until n is 0
        tell (n mod 16)
            if it > 9 then
                set s to character (it - 9) of "ABCDEF" & s
            else
                set s to (it as string) & s
            end if
        end tell
        set n to n div 16
    end repeat
    set contents of aNumber to s
end convertNumberToHex

我添加了一个例程来将整数值转换为十六进制值字符串;不确定您喜欢哪种形式.

I've added a routine to convert the integer values to hex-value strings; not sure which form you prefer.

这篇关于如何在 AppleScript 中将文件作为字节数组读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆