How to preserve text formatting when copying into the clipboard

Hi guys,

What I’m trying to accomplish sounds pretty simple but couldn’t find the way/answer so far:

After retrieving the value of a static text from a UI element (it is a plain text containing some paragraphs) and assigning it into a variable, then I set the clipboard to that variable.

ie.
[format]
tell text field 1 of group 1 of UI element 1 …
set theBody to its value
end tell
display dialog theBody – displays the entire body keeping its original format
display dialog class of theBody – displays ‘txt’
display dialog every paragraph of theBody – displays just 1 record which contains all the paragraphs
set the clipboard to theBody
[/format]
Once the clipboard is assigned the content of the ‘theBody’ variable, here is where it comes to me the real issue, when I go ahead and paste the content of the clipboard into any application like TextEdit, Sublime Text …

The original format is lost, instead what it yields is an unformatted unique string, like a long sentence.

Surprisingly, if I reassign the clipboard content into “theBody” variable, this variable keeps the original formatting in the script:

[format]
set theBody to the clipboard
display dialog theBody – displays the entire body keeping its original format again
[/format]

How I could make the clipboard to keep the original formatting so I can paste it in any application?

Thank you in advance

Marius

The clipboard retains format. It is how you past formatted text. In the Textedit, for example, you can use Paste and Match Style instead of Paste menu item. Otherway Textedit will convert the style to style defined as default in the preferences of TextEdit.

Hi KniazidisR,

Thank you for your answer.

After a further investigation, I found that the clipboard preserves the original format of the block of text but the way in which its content is pasted differs in each application I tried.

Here they are the apps I’ve tried to paste the clipboard content to recreate the issue:

[format]Block of text example:
Morbi condimentum, enim a mollis sollicitudin.

Sem magna cursus nunc, suscipit luctus turpis ex et libero.
Etiam sodales neque eget ante accumsan, euismod finibus tortor molestie.
Curabitur vulputate, nunc at ornare sagittis, enim lacus molestie erat.

Non feugiat ex dui a ligula.
Nam molestie sagittis porta. Morbi egestas tempor magna nec commodo.

Suspendisse potenti.[/format]

Script Editor: it preserves the original format

TextEdit: it preserves the original format

Sublime Text: the line breaks are replaced by this string <0x2028>
[format]Morbi condimentum. <0x2028><0x2028>enim a mollis sollicitudin, sem magna cursus nunc, suscipit luctus turpis ex et libero.<0x2028>Etiam sodales neque eget ante accumsan, euismod finibus tortor molestie. Curabitur vulputate, nunc at ornare sagittis, enim lacus molestie erat, non feugiat ex dui a ligula.<0x2028><0x2028>[/format]

VS Code: the line breaks are replaced by this character’[b]<?>[/b]' [format]Morbi condimentum.??? enim a mollis sollicitudin, sem magna cursus nunc, suscipit luctus turpis ex et libero.??Etiam sodales neque eget ante accumsan, euismod finibus tortor molestie.<?>Curabitur vulputate, nunc at ornare sagittis, enim lacus molestie erat, non feugiat ex dui a ligula.[/format]
.
Word: the line breaks are replaced by “”
[format]Morbi condimentum, enim a mollis sollicitudin, sem magna cursus nunc, suscipit luctus turpis ex et libero. Etiam sodales neque eget ante accumsan, euismod finibus tortor molestie.Curabitur vulputate, nunc at ornare sagittis, enim lacus molestie erat, non feugiat ex dui a ligula. Nam molestie sagittis porta. Morbi egestas tempor magna nec commodo. Suspendisse potenti. Phasellus ac imperdiet turpis, ut dictum neque.[/format]

And the script that I’m developing is meant for an internal application at my work and the way that the clipboard content is pasted in that app is similar to the one I got in Word

I wonder if there is a way I could modify/manipulate the value assigned to the clipboard so it preserves its formatting in all scenarios

You can replace LineSeparator character with character which understand your target application. Example:


-- tell text field 1 of group 1 of UI element 1 ... to set theBody to its value
set theBody to "This is test with" & character id 8232 & "replacing LineSeparatorCharacter"

set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to character id 8232
set textItems to text items of theBody
set AppleScript's text item delimiters to return -- or linefeed
set theBody to textItems as text
set AppleScript's text item delimiters to ATID

set the clipboard to theBody
return theBody

Other option is installing all Unicode fonts from HERE

I don’t have Sublime Text to find out exactly what the problem is, so experiment yourself and share the solution with us.

When you paste it into Word, it is likely pasting as HTML†. If you ‘paste special’ you should get multiple options. When I select your example text from this page, and paste special in word, I get two choices… unformatted text and HTML format. If I choose the former, I get your line breaks, with the latter I get zero line breaks.

Try clipboard info to see what kinds of formatting the clipboard possesses for your selection (so ‘original formatting’ may not mean what you think it means). I get the following with the provided example:

[format]{{«class HTML», 507}, {«class utf8», 377}, {«class ut16», 756}, {string, 377}, {Unicode text, 754}}[/format]

As an aside, when you do a standard paste (i.e. command-v) you get whatever the system thinks is appropriate for the situation.

Within Word, if your script uses Word’s own pasting, you get the line breaks… try:

tell document 1 of application "Microsoft Word" to set content of text object to the clipboard

In the apps that are giving odd results, try appending ‘as text’ or as ‘«class utf8»’ (which should be the same). Also, check in their dictionaries to see if they have their own clipboard functions… perhaps there would also be some clarification as to how to get the results you desire.

† Obviously, I can only work with the text as formatted by this website so I can’t say specifically what you’re experiencing.

Thank you guys for looking into this issue.

I was able to solve the problem adding to the KniazidisR’s solution the following handler:


set theBody to my replace_chars(theBody, character id 8232, linefeed)
set the clipboard to  theBody -- now the formatting is preserved in any app I've tried :)
...

on replace_chars(theText, search_string, replacement_string)
  set AppleScript's text item delimiters to the search_string
  set the item_list to every text item of theText
  set AppleScript's text item delimiters to the replacement_string
  set theText to the item_list as string
  set AppleScript's text item delimiters to ""
  return theText
end replace_chars

I love this forum, you all are the best

Marius