Benches Uncategorized

4

Getting raw data of a variable

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

Is anyone aware of a handy way to get the raw (hex) data of an AppleScript variable into a text string? For example:

«data rdatEFBBBF»
«data isot323030372D31322D31325431373A34393A3130»

I can do some comparisons, and put the raw data as text into a variable, but trying to get the actual raw data as text either fails or gets coerced into whatever it is supposed to be (such as ISOT). Well, actually I can do the "force an error and grab the error message" trick, but there has got to be a better way. Perhaps something along the lines of set X to do shell script "whattheheck " & (SomeVariable as data)?

4 replies

Sign in or join Beachball Help to reply.

  1. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hello

    Under OS9/Classic, we can use a mighty 'cast' command of 'programmer's tool' OSAX made by Ed Lai for this kind of task.
    http://osaxen.com/files/programmerstool1.0a10.html

    Under OSX, the ladder has been removed.
    One dirty way I can think of is to use write and read commands to force to replace the class of given data. Somethnig like this, though it won't work properly with AppleScript 2.0 if the input or output data is a sort of text object.

    --SCRIPT
    set x to «data isot323030372D31322D31325431373A34393A3130»
    --set x to «data rdatEFBBBF»
    --set x to current date
    --set x to {1, 2, 3}
    --set x to "ABC" as Unicode text
    set y to dump(x, string)
    set z to dump(y, data)
    return {x, y, z}

    on dump(x, cls)
    (*
      anything x : anything to be dumped [1]
      type class cls : resulting class of dumped data. e.g. string, data etc.
      return anything : dumped data as given class [2]
      
      * Notes.
      [1] x must be what 'write' osax can write to file. (E.g. script object etc cannot be processed by this method)
      [2] It won't return exact raw byte sequence in AppleScript 2.0 if
        - x's class is one of {Unicode text, string, text}; or
        - cls is one of {Unicode text, «class utf8», string, text}.
    *)
    set fp to (path to "desk" from user domain as Unicode text) & "scptdump_tempfile" & (random number 1000000)
    try
    set fref to open for access file fp with write permission
    set eof fref to 0
    write x to fref
    close access fref
    set d to read file fp as cls
    on error errs number errn
    try
    close access file fp
    end try
    error "dump():" & errs number errn
    end try
    do shell script "rm -f " & quoted form of POSIX path of fp
    return d
    end dump
    --END OF SCRIPT

    Hope this may be of some help,
    Hiroto

    PS.
    In pre-AS 2.0, we can use 'string' to hold raw byte sequence of any kind of data, although it might be indeed an abuse of 'string' object. In AS 2.0, we seem to have lost this final way to abuse string and I'm yet to know any alternative way to process raw byte sequence safely.

    Now, I think, it would be really nice to see the following basic features being implemented with «class rdat» in order to process raw byte sequence in AppleScript 2.0 or later, where we cannot 'abuse' string anymore:
    • length property; e.g. length of «data rdat0032» (count «data rdat0032») returning 2.
    • way to extract element(s); e.g. item 4 of «data rdat32303037» returning «data rdat37»
    • way to concatenate two data; e.g. «data rdat32» & «data rdat30» returing «data rdat3230»
    • way to perform valid coercion to other class, e.g. «data rdat0032» as Unicode text returning "2" (= U+0032).
    • way to perform coercion from other class, e.g. ("2" as Unicode text) as data returning «data rdat0032»

    Just my daydream. ; )

    Message was edited by: Hiroto (corrected typo)

  2. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Well, I can do most of those daydreams, it is just ugly. There just doesn't seem to be a clean way to get the raw data into some form that it can be manipulated, so I was hoping someone could give me a clue. This started with the whole ISO time to Unicode coersion problem, but I've been looking for a way to manipulate various raw data types.

    What I have come up with so far (the examples convert class isot to Unicode text):

    <pre title="this text can be pasted into the Script Editor" style="font-family: Monaco, 'Courier New', Courier, monospace; font-size: 10px; padding: 5px; width: 720px; color: #000000; background-color: #E0E0E0; overflow: auto">-- get the data
    set x to (current date) as «class isot»
    log {class of x, x}

    -- extract the data as text
    try -- force an error
    x as number
    on error errmess -- extract the data from the error message
    set x to text ((offset of "«" in errmess) + 10) thru ((offset of "»" in errmess) - 1) of errmess
    end try
    log {class of x, x}

    -- break the data into chunks (add data to make Unicode in this example)
    set DataList to {}
    repeat with Chunk from 1 to (count x) by 2
    set the end of DataList to "00" & text Chunk thru (Chunk + 1) of x
    end repeat
    set x to DataList as text

    -- assign the data to a class
    set x to (run script "get «data utxt" & x & "»")
    log {class of x, x}
    </pre>

  3. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hello,

    My daydream is to see them implemented as built-in features of «class rdat» object in AppleScript language.

    Anyway, this will perform «class isot» to «class utxt» conversion as well.
    Cheers,
    H

    --SCRIPT1
    (* For OS9/Classic, using 'cast' command of 'programmer's tool' OSAX *)
    set x to «data isot323030372D31322D31325431373A34393A3130»
    return (cast x to string) as Unicode text
    --SCRIPT1

    --SCRIPT2
    (* For OSX - In this specific case, it will return the same result in both pre-AS 2.0 and AS 2.0 *)
    --set x to (current date) as «class isot»
    set x to «data isot323030372D31322D31325431373A34393A3130»
    return dump(x, string) as Unicode text

    on dump(x, cls)
    (*
      anything x : anything to be dumped [1]
      type class cls : resulting class of dumped data. e.g. string, data etc
      return anything : dumped data as given class [2]
      
      * Notes.
        [1] x must be what 'write' osax can write to file. (E.g. script object etc cannot be processed by this method)
        [2] It won't return exact raw byte sequence in AppleScript 2.0 or later if
          - x's class is one of {Unicode text, string, text}; or
          - cls is one of {Unicode text, «class utf8», string, text}.
    *)
    set fp to (path to "desk" from user domain as Unicode text) & "scptdump_tempfile" & (random number 1000000)
    try
    set fref to open for access file fp with write permission
    set eof fref to 0
    write x to fref
    close access fref
    set d to read file fp as cls
    on error errs number errn
    try
    close access file fp
    end try
    error "dump():" & errs number errn
    end try
    do shell script "rm -f " & quoted form of POSIX path of fp
    return d
    end dump
    --END OF SCRIPT2

  4. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I was looking for something a bit cleaner than an error message (although that works) to get the data into some form that could be worked with. Writing to a temporary file seems to be a little more involved than it should be, but I guess it will do until something built-in comes along (not that I am holding my breath or anything). Your examples got me thinking about using a shell script to do something with the temporary file, and a plain hex dump using xxd seems to get the results I was looking for (although I haven't tried it in AppleScript 2).

    My handler:
    <pre title="this text can be pasted into the Script Editor" style="font-family: Monaco, 'Courier New', Courier, monospace; font-size: 10px; padding: 5px; width: 720px; color: #000000; background-color: #E0E0E0; overflow: auto">on run -- examples
    set X to (current date) as «class isot»
    set X to GetRawData from X
    choose from list X with prompt "Raw Data:" with empty selection allowed
    display dialog (run script ("get «data ctxt" & rest of X as text) & "»") with title "Converted to text"

    set X to 1.234
    set X to GetRawData from X
    choose from list X with prompt "Raw Data:" with empty selection allowed
    display dialog (run script ("get «data doub" & rest of X as text) & "»") with title "Converted to real"

    set X to "this is an example of some Unicode text" as Unicode text
    set X to GetRawData from X
    choose from list X with prompt "Raw Data:" with empty selection allowed
    display dialog (run script ("get «data utxt" & rest of X as text) & "»") with title "Converted to Unicode text"
    end run

    to GetRawData from SomeVariable
    (*
    get the data class and raw byte sequence of a variable
    parameters - SomeVariable [mixed]: the variable to get the data from
    returns [list]: item 1 [text]: the class of the variable
            item(s) 2+ [text]: the raw (hex) data
    *)
    set DataList to {(class of SomeVariable) as text}
    set TempFile to "/tmp/GetRawData_Handler.tmp"

    try -- get the raw data
    set MyOpenFile to open for access (TempFile as POSIX file) with write permission
    set eof of MyOpenFile to 0 -- empty any previous temp file
    write SomeVariable to MyOpenFile
    close access MyOpenFile
    set TheData to (paragraphs of (do shell script "xxd -plain " & quoted form of TempFile)) as text
    on error ErrorMessage number ErrorNumber
    try
    close access file TempFile
    end try
    error "GetRawData handler error" & return & ErrorMessage number ErrorNumber
    end try

    repeat with TheByte from 1 to (count TheData) by 2 -- break the data into bytes
    set the end of DataList to text TheByte thru (TheByte + 1) of TheData
    end repeat

    return DataList
    end GetRawData
    </pre>