Display copy/paste buffer in hex and character

When copying and pasting you may occasionally find a strange non-printable characters in the pasted result, This apple script program will display the copy/paste buffer in hex and ASCII.


(* 

Display copy buffer in hex and character. 
for example: 
4865726520697320616E206173207965    'Here is an as ye'

    Author: rccharles
    
    Run in the Script Editor to see the complete output.
      1) Click on the Messages tab to see the complete output from the log 
          statement
      2) Click on Run
      
 *)


on run
    
    (* ... supports three file formats: 
                unix and macOS (lines end with LF),
                  dos (lines end with CR LF), and 
                  mac classic (lines end with CR).
            http://vim.wikia.com/wiki/Change_end-of-line_format_for_dos-mac-unix *)
    
    
    set lf to ASCII character 10
    
    -- Write a message into the event log.
    log "  --- Starting on " & ((current date) as string) & " --- "
    set theClip to the clipboard
    log length of theClip
    if length of theClip = 0 then
        display dialog "Please copy something to the clip board." giving up after 20
        return
    end if
    
    -- a little sub-hacking.
    
    set theCount to 1
    set theRep to 16
    
    set displayChars to "
    unix (lines end with LF),
    dos (lines end with CR LF), and 
    mac classic  (lines end with CR).
    where lf is '0A'x and cr is '0D'x
    http://dc.org/files/asciitable.pdf
                
    " & return
    
    set theLoops to (length of theClip) div theRep
    set theRemainder to (length of theClip) mod theRep
    log "theLoops is " & theLoops
    log "theRemainder is " & theRemainder
    
    repeat with i from 1 to theLoops
        set allChars to ""
        set printChars to ""
        repeat with j from 1 to theRep
            set theChar to character theCount of theClip
            set allChars to allChars & encode_char(theChar)
            if (the (ASCII number (theChar)) ≥ 32) or (the (ASCII number (theChar)) = 255) then
                set printChars to printChars & "" & theChar
            else
                -- non printable 
                set printChars to printChars & "."
            end if
            set theCount to theCount + 1
        end repeat
        log allChars
        set displayChars to displayChars & allChars & "    '" & printChars & "'" & return
    end repeat
    
    set allChars to ""
    set printChars to ""
    repeat with k from 1 to theRemainder
        set theChar to character theCount of theClip
        set allChars to allChars & encode_char(theChar)
        if (the (ASCII number (theChar)) ≥ 32) or (the (ASCII number (theChar)) = 255) then
            set printChars to printChars & "" & theChar
        else
            -- non printable 
            set printChars to printChars & "."
        end if
        set theCount to theCount + 1
    end repeat
    
    if theRemainder > 0 then
        set displayChars to displayChars & allChars & "    '" & printChars & "'" & return
    end if
    log displayChars
    
    display dialog displayChars
    
    log "that all"
    
end run

-- --------------------------------------------------------------------------
-- https://www.macosxautomation.com/applescript/sbrt/sbrt-08.html
on encode_char(this_char)
    set the ASCII_num to (the ASCII number this_char)
    set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
    set x to item ((ASCII_num div 16) + 1) of the hex_list
    set y to item ((ASCII_num mod 16) + 1) of the hex_list
    return (x & y) as string
end encode_char

-- --------------------------------------------------------------------------
(* -- In reserve
    try
        set fromUnix to do shell script "pbpaste -Prefer ascii | pbcopy "
    on error errMsg
        log "ls -l error..." & errMsg
    end try
    -- set this_text to characters (x + 1) thru -1 of this_text as string
    *)

Mac mini, OS X Yosemite (10.10.5), Fall 2014; iPhone 4 7.1.2

Posted on Apr 5, 2017 6:55 PM

Reply

There are no replies.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Display copy/paste buffer in hex and character

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.