The Omni Group
These forums are now read-only. Please visit our new forums to participate in discussion. A new account will be required to post in the new forums. For more info on the switch, see this post. Thank you!

Go Back   The Omni Group Forums > Developer > AppleScripting Omni Apps
FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
Getting RTF and HTML strings from formatted text in the Clipboard Thread Tools Search this Thread Display Modes
Applescript is ill-equipped to transform formatted text.

(Assigning a text/string variable to a stretch of formatted text coerces it to plain text).

There is sometimes scope for working around this limitation by getting the formatted text into the clipboard, from which it is possible to extract RTF or HTML strings. You can transform these strings, and then put modified formatted text back into the clipboard with pbcopy.

Some illustrative code:

Code:
property pstrTempFile : "tmp.rtf"

tell application id "com.apple.finder"
	
	-- TRY TO GET SOME RTF DATA FROM THE CLIPBOARD
	try
		set dataRTF to the clipboard as «class RTF »
	on error
		display dialog "No RTF in clipboard ..." with title "Get RTF & HTML strings"
		return
	end try
	
	-- RETRIEVE THE RAW RTF STRINGS INTO AN APPLESCRIPT VARIABLE USING PBPASTE
	-- (PBCOPY can be used later to put a transformed version back into the clipboard)
	set the clipboard to dataRTF
	set strRTF to do shell script "pbpaste -Prefer rtf"
	
	
	-- WRITE THE RTF OUT TO A TEMPORARY FILE
	set strTempFolder to (container of (path to me)) as string
	set strTempFile to strTempFolder & pstrTempFile
	set oFile to strTempFile as file specification
	open for access oFile with write permission
	write dataRTF to oFile as «class RTF »
	close access oFile
	
	-- USE TEXTUTIL TO GET AN HTML VERSION OF THE RTF
	-- (HTML can be easier to read and transform,
	-- and TEXTUTIL can be used again later to convert from HTML back to RTF)
	set strTempFile to POSIX path of strTempFile
	set strHTML to do shell script "textutil -convert html -stdout " & strTempFile
	
	{strRTF, strHTML}
end tell

Last edited by RobTrew; 2010-11-04 at 07:08 AM..
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
ofexport - export to text, taskpaper, html, opml psidnell OmniFocus Extras 110 2014-04-01 08:36 AM
Send via Mail, Simple HTML- text flow hampered SWCarson OmniOutliner for iPad 3 2011-06-30 10:45 AM
OPML exporting and formatted text macula OmniOutliner 3 for Mac 6 2010-06-09 06:53 AM
Export Properties:Notes as HTML Roll Over Text? jeffreykennedy OmniGraffle General 6 2009-10-26 07:40 AM
copy tabular html data as tab-delimited text Gregory OmniWeb Feature Requests 1 2006-05-14 10:10 AM


All times are GMT -8. The time now is 02:10 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2020, vBulletin Solutions, Inc.