SHARE
TWEET

Display macOS clipboard data

rccharles May 13th, 2019 (edited) 175 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (*
  2.   Displays some of the macOS clipboard data.
  3.  
  4.   Includes a Demonstration of how dropping files on AppleScript icon works and shows items added to folder.
  5.   Shows how to debug via on run path.  
  6.   Save as an Application Bundle.  Don't check anything.
  7.  
  8.  It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on.  Here is an example.
  9.    
  10. For testing, run in the Script Editor.
  11.     1) Click on the Event Log tab to see the output from the log statement
  12.     2) Click on Run
  13.  
  14.  
  15. Author: rccharles
  16.  
  17. Internet version:
  18. May 13, 2019  https://pastebin.com/raw/3CDNBUJF
  19.  
  20. Run a script from the terminal.
  21.  
  22. open ~/Desktop/rtn_email.app
  23. osascript ~/Desktop/rtn_email.app
  24.  
  25. https://apple.stackexchange.com/questions/251466/how-to-use-applescript-to-create-a-new-rich-text-format-file-from-clipboard-text
  26.  
  27.  *)
  28.  
  29.  
  30. -- Gets invoked here when you run in AppleScript editor.
  31.  
  32. on run
  33.     --  debug lines
  34.    
  35.     -- Simulate dropped items list.
  36.     set dropped_items to {}
  37.    
  38.     common(dropped_items)
  39.    
  40. end run
  41.  
  42. -- ------------------------------------------------------
  43. -- Folder actions.
  44. -- Gets invoked here when something is dropped on the folder that this script is monitoring.
  45. -- Right click on the folder to be monitored. services > Folder Action Settup...
  46.  
  47. on adding folder items to this_folder after receiving added_items
  48.    
  49.     common(added_items)
  50.    
  51. end adding folder items to
  52.  
  53. -- ------------------------------------------------------
  54.  
  55. -- Gets invoked here when something is dropped on this AppleScript icon
  56.  
  57. on open dropped_items
  58.    
  59.     common(dropped_items)
  60.    
  61. end open
  62.  
  63.  
  64. -- ------------------------------------------------------
  65. on common(dropped_items)
  66.     global debug
  67.     set debug to 0
  68.     set theRC to 0
  69.    
  70.     -- Write a message into the event log.
  71.     log "  --- Starting on " & ((current date) as string) & " --- "
  72.     log return & "Should you wish to read the hex dump output from the Script Editor, " & return & "copy the output to a simple text editor like TextEdit, TextWrangler or BBedit." & return & " "
  73.     tell application "Script Editor"
  74.         activate
  75.     end tell
  76.    
  77.     log "class of dropped_items is " & class of dropped_items
  78.    
  79.     set theList to clipboard info
  80.     printClipboardInfo(theList)
  81.     log " "
  82.     set cbInfo to get (clipboard info) as string
  83.    
  84.     try
  85.         log "check for «class furl» data"
  86.         if cbInfo contains "«class furl»" then
  87.             log "the clipboard contains a file named " & (the clipboard as string)
  88.         end if
  89.     on error errStr number errorNumber
  90.         log "===> We didn't find furl data.   errStr is " & errStr & " errorNumber is " & errorNumber
  91.         return
  92.     end try
  93.    
  94.     try
  95.         log "check for html data"
  96.         if cbInfo contains "HTML" then
  97.            
  98.             set theBoard to the clipboard as «class HTML»
  99.             log "Print out the HTML class data on the clipboard"
  100.             --log theBoard
  101.             -- | textutil -convert rtf -stdin -stdout > " & quoted form of theRichTextFileName
  102.            
  103.             set outUnix to do shell script "osascript -e 'try' -e 'get the clipboard as «class HTML»' -e 'end try' | awk '{sub(/«data HTML/, \"3C68746D6C3E\") sub(/»/, \"3C2F68746D6C3E\")} {print}' | xxd -r -p "
  104.             log outUnix
  105.             hexDumpFormat("translated HTML class input", outUnix)
  106.         end if
  107.     on error errStr number errorNumber
  108.         log "===> We didn't find HTML data.   errStr is " & errStr & " errorNumber is " & errorNumber
  109.         return
  110.     end try
  111.    
  112.     try
  113.         log "check for utf8"
  114.         if cbInfo contains "utf8" then
  115.            
  116.             set theBoard to the clipboard as «class utf8»
  117.             log "Print out the utf8 data on the clipboard"
  118.             log theBoard
  119.             hexDumpFormat("Hex dump of utf8 class input", theBoard)
  120.            
  121.         end if
  122.     on error errStr number errorNumber
  123.         log "===> We didn't find utf8 data.   errStr is " & errStr & " errorNumber is " & errorNumber
  124.         return
  125.     end try
  126.    
  127.     try
  128.         log "check for html uniform styles"
  129.         if cbInfo contains "uniform styles" then
  130.            
  131.             set theBoard to the clipboard as string
  132.             log "Print out the uniform styles data on the clipboard"
  133.             log theBoard
  134.             -- | textutil -convert rtf -stdin -stdout > " & quoted form of theRichTextFileName
  135.            
  136.             set outUnix to do shell script "osascript -e 'try' -e 'get the clipboard as «class uniform styles»' -e 'end try' | awk '{sub(/«data HTML/, \"3C68746D6C3E\") sub(/»/, \"3C2F68746D6C3E\")} {print}' | xxd -r -p "
  137.             log outUnix
  138.             --set hexPart to text 9 thru lenght of theBoard of theBoard
  139.             --log hexPart
  140.             hexDumpFormat("hex dump of uniform styles class data", outUnix)
  141.         end if
  142.     on error errStr number errorNumber
  143.         log "===> We didn't find data.   errStr is " & errStr & " errorNumber is " & errorNumber
  144.         return
  145.     end try
  146.    
  147.     try
  148.         log "check for string"
  149.         if cbInfo contains "string" then
  150.            
  151.             set theBoard to the clipboard as string
  152.            
  153.             log "Print out the string data on the clipboard"
  154.             log theBoard
  155.            
  156.         end if
  157.     on error errStr number errorNumber
  158.         log "===> We didn't find string data.   errStr is " & errStr & " errorNumber is " & errorNumber
  159.         return
  160.     end try
  161.    
  162.     try
  163.         if cbInfo contains "RTF" then
  164.            
  165.             try
  166.                 set richTextfromClipboard to get the clipboard as «class RTF »
  167.                 log richTextfromClipboard
  168.             on error eStr number eNum
  169.                 display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with icon caution
  170.                 return
  171.             end try
  172.             try
  173.                 (*
  174.                 set theRichTextFileName to POSIX path of (path to desktop as text) & "New RichText Filename-2.rtf"
  175.                 tell application "Finder"
  176.                    
  177.                     if exists theRichTextFileName as POSIX file then
  178.                         tell current application
  179.                             display dialog "The file \"" & theRichTextFileName & "\" already exists!" & "
  180.  
  181. " & "Do you want to overwrite the file?" buttons {"No", "Yes"} default button 1 with title "File Already Exists..." with icon caution
  182.                            
  183.                             if the button returned of the result is "No" then
  184.                                 return
  185.                             else
  186.                                 tell application "Finder"
  187.                                     delete the file (theRichTextFileName as POSIX file)
  188.                                 end tell
  189.                             end if
  190.                         end tell
  191.                     end if
  192.                 end tell
  193.                
  194.                 set fileHandle to open for access theRichTextFileName with write permission
  195.                 write richTextfromClipboard to fileHandle
  196.                 close access fileHandle
  197.                 *)
  198.             on error eStr number eNum
  199.                 display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with title " Error..." with icon caution
  200.                
  201.             end try
  202.         end if
  203.     on error errStr number errorNumber
  204.         log "===> We didn't find RTF data.   errStr is " & errStr & " errorNumber is " & errorNumber
  205.         return
  206.     end try
  207.     return theRC
  208.    
  209. end common
  210. -- ------------------------------------------------------
  211. on printClipboardInfo(theList)
  212.     log (clipboard info)
  213.     log "Data types on the clipboard ... "
  214.     repeat with theCurrentListItem in theList
  215.         -- Process the current list item
  216.         --log "  class of theCurrentListItem is " & class of theCurrentListItem
  217.         log theCurrentListItem
  218.     end repeat
  219.     log "... end data types on the clipboard info."
  220.    
  221.    
  222. end printClipboardInfo
  223.  
  224. -- ------------------------------------------------------
  225. (*
  226.   http://krypted.com/mac-os-x/to-hex-and-back/
  227. *)
  228. on hexToString(hex)
  229.     log "in hexToString"
  230.     log "hex string is " & hex
  231.     set toUnix to "echo " & hex & " | xxd -r -p "
  232.     log "toUnix is " & toUnix
  233.     try
  234.         set fromUnix to do shell script toUnix
  235.         log "fromUnix is " & fromUnix
  236.     on error errMsg number n
  237.         log "convert hex string to string failed. " & errMsg & " with number " & n
  238.     end try
  239. end hexToString
  240.  
  241. -- ------------------------------------------------------
  242. (*
  243.   http://krypted.com/mac-os-x/to-hex-and-back/
  244.   0000000: 3c68 746d 6c3e 3c68 6561 643e 3c6d 6574  <html><head><met
  245. "         0    2    4    6    8    a    c    e     0 2 4 6 8 a c e"
  246.  
  247.  
  248.   *)
  249. on hexDumpFormat(textMessage, hex)
  250.     global debug
  251.     if debug ≥ 3 then log "in hexDumpFormat"
  252.     if debug ≥ 3 then log "hex string is " & hex
  253.     -- -r -p
  254.     set toUnix to "/bin/echo -n " & (quoted form of hex) & " | xxd  "
  255.     if debug ≥ 3 then log "toUnix is " & toUnix
  256.     try
  257.         set fromUnix to do shell script toUnix
  258.         log "variable " & textMessage & " in hex is " & return & "         0    2    4    6    8    a    c    e     0 2 4 6 8 a c e" & return & fromUnix
  259.     on error errMsg number n
  260.         log "==> convert hex string to string failed. " & errMsg & " with number " & n
  261.     end try
  262. end hexDumpFormat
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top