Advertisement
rccharles

display applescript clipboard

May 2nd, 2019
1,002
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (*
  2.   Demonstration of how dropping files on AppleScript icon works.  Shows how to debug via on run path. Shows items added to folder.
  3.  
  4.  Save as an Application Bundle.  Don't check anything.
  5.  
  6.  Shows log statement.
  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. Run a script from the terminal.
  18.  
  19. open ~/Desktop/rtn_email.app
  20. osascript ~/Desktop/rtn_email.app
  21.  
  22. https://apple.stackexchange.com/questions/251466/how-to-use-applescript-to-create-a-new-rich-text-format-file-from-clipboard-text
  23.  
  24.  *)
  25.  
  26.  
  27. -- Gets invoked here when you run in AppleScript editor.
  28.  
  29. on run
  30.     --  debug lines
  31.    
  32.     -- Simulate dropped items list.
  33.     set dropped_items to {}
  34.    
  35.     common(dropped_items)
  36.    
  37. end run
  38.  
  39.  
  40. -- Folder actions.
  41. -- Gets invoked here when something is dropped on the folder that this script is monitoring.
  42. -- Right click on the folder to be monitored. services > Folder Action Settup...
  43.  
  44. on adding folder items to this_folder after receiving added_items
  45.    
  46.     common(added_items)
  47.    
  48. end adding folder items to
  49.  
  50.  
  51.  
  52. -- Gets invoked here when something is dropped on this AppleScript icon
  53.  
  54. on open dropped_items
  55.    
  56.     common(dropped_items)
  57.    
  58. end open
  59.  
  60.  
  61.  
  62. on common(dropped_items)
  63.    
  64.     -- Write a message into the event log.
  65.     log "  --- Starting on " & ((current date) as string) & " --- "
  66.     tell application "Script Editor"
  67.         activate
  68.     end tell
  69.    
  70.     log "class of dropped_items is " & class of dropped_items
  71.    
  72.     set theList to clipboard info
  73.     printClipboardInfo(theList)
  74.     log " "
  75.     set cbInfo to get (clipboard info) as string
  76.    
  77.     try
  78.         log "check for «class furl» data"
  79.         if cbInfo contains "«class furl»" then
  80.             log "the clipboard contains a file named " & (the clipboard as string)
  81.         end if
  82.     on error errStr number errorNumber
  83.         log "===> We didn't find furl data.   errStr is " & errStr & " errorNumber is " & errorNumber
  84.         return
  85.     end try
  86.    
  87.     try
  88.         log "check for html data"
  89.         if cbInfo contains "HTML" then
  90.            
  91.             set theBoard to the clipboard as «class HTML»
  92.             log "Print out the HTML data on the clipboard"
  93.             log theBoard
  94.             -- | textutil -convert rtf -stdin -stdout > " & quoted form of theRichTextFileName
  95.            
  96.             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 "
  97.             log outUnix
  98.             --set hexPart to text 9 thru lenght of theBoard of theBoard
  99.             --log hexPart
  100.             --hexToString(hexPart)
  101.         end if
  102.     on error errStr number errorNumber
  103.         log "===> We didn't find HTML data.   errStr is " & errStr & " errorNumber is " & errorNumber
  104.         return
  105.     end try
  106.    
  107.     try
  108.         log "check for utf8"
  109.         if cbInfo contains "utf8" then
  110.            
  111.             set theBoard to the clipboard as «class utf8»
  112.             log "Print out the utf8 data on the clipboard"
  113.             log theBoard
  114.            
  115.         end if
  116.     on error errStr number errorNumber
  117.         log "===> We didn't find utf8 data.   errStr is " & errStr & " errorNumber is " & errorNumber
  118.         return
  119.     end try
  120.    
  121.     try
  122.         log "check for html uniform styles"
  123.         if cbInfo contains "uniform styles" then
  124.            
  125.             set theBoard to the clipboard as string
  126.             log "Print out the uniform styles data on the clipboard"
  127.             log theBoard
  128.             -- | textutil -convert rtf -stdin -stdout > " & quoted form of theRichTextFileName
  129.            
  130.             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 "
  131.             log outUnix
  132.             --set hexPart to text 9 thru lenght of theBoard of theBoard
  133.             --log hexPart
  134.             --hexToString(hexPart)
  135.         end if
  136.     on error errStr number errorNumber
  137.         log "===> We didn't find data.   errStr is " & errStr & " errorNumber is " & errorNumber
  138.         return
  139.     end try
  140.    
  141.     try
  142.         log "check for string"
  143.         if cbInfo contains "string" then
  144.            
  145.             set theBoard to the clipboard as string
  146.            
  147.             log "Print out the string data on the clipboard"
  148.             log theBoard
  149.            
  150.         end if
  151.     on error errStr number errorNumber
  152.         log "===> We didn't find string data.   errStr is " & errStr & " errorNumber is " & errorNumber
  153.         return
  154.     end try
  155.    
  156.     try
  157.         if cbInfo contains "RTF" then
  158.            
  159.             try
  160.                 set richTextfromClipboard to get the clipboard as «class RTF »
  161.                 log richTextfromClipboard
  162.             on error eStr number eNum
  163.                 display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with icon caution
  164.                 return
  165.             end try
  166.             try
  167.                 (*
  168.                 set theRichTextFileName to POSIX path of (path to desktop as text) & "New RichText Filename-2.rtf"
  169.                 tell application "Finder"
  170.                    
  171.                     if exists theRichTextFileName as POSIX file then
  172.                         tell current application
  173.                             display dialog "The file \"" & theRichTextFileName & "\" already exists!" & "
  174.  
  175. " & "Do you want to overwrite the file?" buttons {"No", "Yes"} default button 1 with title "File Already Exists..." with icon caution
  176.                            
  177.                             if the button returned of the result is "No" then
  178.                                 return
  179.                             else
  180.                                 tell application "Finder"
  181.                                     delete the file (theRichTextFileName as POSIX file)
  182.                                 end tell
  183.                             end if
  184.                         end tell
  185.                     end if
  186.                 end tell
  187.                
  188.                 set fileHandle to open for access theRichTextFileName with write permission
  189.                 write richTextfromClipboard to fileHandle
  190.                 close access fileHandle
  191.                 *)
  192.             on error eStr number eNum
  193.                 display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with title " Error..." with icon caution
  194.                
  195.             end try
  196.         end if
  197.     on error errStr number errorNumber
  198.         log "===> We didn't find RTF data.   errStr is " & errStr & " errorNumber is " & errorNumber
  199.         return
  200.     end try
  201.     return
  202.    
  203. end common
  204.  
  205. on printClipboardInfo(theList)
  206.     log (clipboard info)
  207.     log "Data types on the clipboard ... "
  208.     repeat with theCurrentListItem in theList
  209.         -- Process the current list item
  210.         --log "  class of theCurrentListItem is " & class of theCurrentListItem
  211.         log theCurrentListItem
  212.     end repeat
  213.     log "... end data types on the clipboard info."
  214.    
  215.    
  216. end printClipboardInfo
  217. (*
  218.   http://krypted.com/mac-os-x/to-hex-and-back/
  219. *)
  220. on hexToString(hex)
  221.     log "in hexToString"
  222.     log "hex string is " & hex
  223.     set toUnix to "echo " & hex & " | xxd -r -p "
  224.     log "toUnix is " & toUnix
  225.     try
  226.         set fromUnix to do shell script toUnix
  227.         log "fromUnix is " & fromUnix
  228.     on error errMsg number n
  229.         log "convert hex string to string failed. " & errMsg & " with number " & n
  230.     end try
  231. end hexToString
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement