如何在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.