There are many threads on the forum about ways to read binary files, but I hope someone can tell me what is the simplest method to perform this particular task.
I am writing a script that will convert files in WordPerfect format to RTF format. WordPerfect files created by the old DOS and Unix versions of WordPerfect have (of course) no creator codes, and their filenames can have any extension.
The way to test whether such files are actually WordPerfect document files is to read the first four bytes, which will be the hex characters: FF 57 50 43 (FF, followed by WPC).
I would like my script to test for creator codes and certain file extensions, and, if it doesn’t find any matching code or extension, test the first four bytes of the file.
Is there a fast and efficient way to do this? If so, I would be very grateful for any help. (A shell script using grep might do the job, but I can’t figure out the syntax.)
Hi,
the shell-command “tail” may help … have a look at the manpagefor the options …
this should read the first four bytes of a file:
set Read4Bytes to (do shell script "tail -c " & "4" & space & quoted form of POSIX path of (choose file))
Thank you, Hans-Gerd! That seems to work. I also took a closer look at an another posting on this site and put together this, which also seems to work:
set doFile to false
set openFile to read file ((choose file) as text)
set readBytes to {}
repeat with i from 1 to 4
set end of readBytes to (ASCII number of character i of openFile)
end repeat
set byteString to (readBytes as string)
if byteString is "255878067" then
set doFile to true
end if
display dialog doFile
I doubt this is the most efficient way to do this, but it works.
Thank you again.
set isWPdoc to ((read (choose file) from 1 to 4 as data) is «data rdatFF575043»)
Amazing. One line of code. Perfect. And thank you for the contribution to my education in AppleScript!