Friday, October 22, 2021

#1 2021-10-01 03:05:27 pm

Marius_Magnificus
Member
Registered: 2019-05-05
Posts: 19

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.

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

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:

set theBody to the clipboard
display dialog theBody -- displays the entire body keeping its original format again

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

Thank you in advance

Marius

Offline

 

#2 2021-10-01 10:12:07 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2091

Re: How to preserve text formatting when copying into the clipboard

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.

Last edited by KniazidisR (2021-10-01 10:15:15 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#3 2021-10-02 01:31:01 pm

Marius_Magnificus
Member
Registered: 2019-05-05
Posts: 19

Re: How to preserve text formatting when copying into the clipboard

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:

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.

Script Editor:  it preserves the original format

TextEdit: it preserves the original format

Sublime Text: the line breaks are replaced by this string <0x2028>

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>

VS Code: the line breaks are replaced by this character'<?>'

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.

.
Word: the line breaks are replaced by ""

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.

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

Offline

 

#4 2021-10-03 09:49:34 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2091

Re: How to preserve text formatting when copying into the clipboard

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

Applescript:


-- 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.

Last edited by KniazidisR (2021-10-03 10:51:23 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#5 2021-10-04 05:28:41 am

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 134

Re: How to preserve text formatting when copying into the clipboard

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:

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

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:

Applescript:

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.

Offline

 

#6 2021-10-04 08:10:16 am

Marius_Magnificus
Member
Registered: 2019-05-05
Posts: 19

Re: How to preserve text formatting when copying into the clipboard

Thank you guys for looking into this issue.

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

Applescript:


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

Last edited by Marius_Magnificus (2021-10-04 08:11:45 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)