Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (*
- Demonstration of how dropping files on AppleScript icon works. Shows how to debug via on run path. Shows items added to folder.
- Save as an Application Bundle. Don't check anything.
- Shows log statement.
- 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.
- For testing, run in the Script Editor.
- 1) Click on the Event Log tab to see the output from the log statement
- 2) Click on Run
- Author: rccharles
- Run a script from the terminal.
- open ~/Desktop/rtn_email.app
- osascript ~/Desktop/rtn_email.app
- https://apple.stackexchange.com/questions/251466/how-to-use-applescript-to-create-a-new-rich-text-format-file-from-clipboard-text
- *)
- -- Gets invoked here when you run in AppleScript editor.
- on run
- -- debug lines
- -- Simulate dropped items list.
- set dropped_items to {}
- common(dropped_items)
- end run
- -- Folder actions.
- -- Gets invoked here when something is dropped on the folder that this script is monitoring.
- -- Right click on the folder to be monitored. services > Folder Action Settup...
- on adding folder items to this_folder after receiving added_items
- common(added_items)
- end adding folder items to
- -- Gets invoked here when something is dropped on this AppleScript icon
- on open dropped_items
- common(dropped_items)
- end open
- on common(dropped_items)
- -- Write a message into the event log.
- log " --- Starting on " & ((current date) as string) & " --- "
- tell application "Script Editor"
- activate
- end tell
- log "class of dropped_items is " & class of dropped_items
- set theList to clipboard info
- printClipboardInfo(theList)
- log " "
- set cbInfo to get (clipboard info) as string
- try
- log "check for «class furl» data"
- if cbInfo contains "«class furl»" then
- log "the clipboard contains a file named " & (the clipboard as string)
- end if
- on error errStr number errorNumber
- log "===> We didn't find furl data. errStr is " & errStr & " errorNumber is " & errorNumber
- return
- end try
- try
- log "check for html data"
- if cbInfo contains "HTML" then
- set theBoard to the clipboard as «class HTML»
- log "Print out the HTML data on the clipboard"
- log theBoard
- -- | textutil -convert rtf -stdin -stdout > " & quoted form of theRichTextFileName
- 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 "
- log outUnix
- --set hexPart to text 9 thru lenght of theBoard of theBoard
- --log hexPart
- --hexToString(hexPart)
- end if
- on error errStr number errorNumber
- log "===> We didn't find HTML data. errStr is " & errStr & " errorNumber is " & errorNumber
- return
- end try
- try
- log "check for utf8"
- if cbInfo contains "utf8" then
- set theBoard to the clipboard as «class utf8»
- log "Print out the utf8 data on the clipboard"
- log theBoard
- end if
- on error errStr number errorNumber
- log "===> We didn't find utf8 data. errStr is " & errStr & " errorNumber is " & errorNumber
- return
- end try
- try
- log "check for html uniform styles"
- if cbInfo contains "uniform styles" then
- set theBoard to the clipboard as string
- log "Print out the uniform styles data on the clipboard"
- log theBoard
- -- | textutil -convert rtf -stdin -stdout > " & quoted form of theRichTextFileName
- 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 "
- log outUnix
- --set hexPart to text 9 thru lenght of theBoard of theBoard
- --log hexPart
- --hexToString(hexPart)
- end if
- on error errStr number errorNumber
- log "===> We didn't find data. errStr is " & errStr & " errorNumber is " & errorNumber
- return
- end try
- try
- log "check for string"
- if cbInfo contains "string" then
- set theBoard to the clipboard as string
- log "Print out the string data on the clipboard"
- log theBoard
- end if
- on error errStr number errorNumber
- log "===> We didn't find string data. errStr is " & errStr & " errorNumber is " & errorNumber
- return
- end try
- try
- if cbInfo contains "RTF" then
- try
- set richTextfromClipboard to get the clipboard as «class RTF »
- log richTextfromClipboard
- on error eStr number eNum
- display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with icon caution
- return
- end try
- try
- (*
- set theRichTextFileName to POSIX path of (path to desktop as text) & "New RichText Filename-2.rtf"
- tell application "Finder"
- if exists theRichTextFileName as POSIX file then
- tell current application
- display dialog "The file \"" & theRichTextFileName & "\" already exists!" & "
- " & "Do you want to overwrite the file?" buttons {"No", "Yes"} default button 1 with title "File Already Exists..." with icon caution
- if the button returned of the result is "No" then
- return
- else
- tell application "Finder"
- delete the file (theRichTextFileName as POSIX file)
- end tell
- end if
- end tell
- end if
- end tell
- set fileHandle to open for access theRichTextFileName with write permission
- write richTextfromClipboard to fileHandle
- close access fileHandle
- *)
- on error eStr number eNum
- display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with title " Error..." with icon caution
- end try
- end if
- on error errStr number errorNumber
- log "===> We didn't find RTF data. errStr is " & errStr & " errorNumber is " & errorNumber
- return
- end try
- return
- end common
- on printClipboardInfo(theList)
- log (clipboard info)
- log "Data types on the clipboard ... "
- repeat with theCurrentListItem in theList
- -- Process the current list item
- --log " class of theCurrentListItem is " & class of theCurrentListItem
- log theCurrentListItem
- end repeat
- log "... end data types on the clipboard info."
- end printClipboardInfo
- (*
- http://krypted.com/mac-os-x/to-hex-and-back/
- *)
- on hexToString(hex)
- log "in hexToString"
- log "hex string is " & hex
- set toUnix to "echo " & hex & " | xxd -r -p "
- log "toUnix is " & toUnix
- try
- set fromUnix to do shell script toUnix
- log "fromUnix is " & fromUnix
- on error errMsg number n
- log "convert hex string to string failed. " & errMsg & " with number " & n
- end try
- end hexToString
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement