Copy IDE Script then paste to TextEdit Colour(Color) problem

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Simon Knight
Posts: 845
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Copy IDE Script then paste to TextEdit Colour(Color) problem

Post by Simon Knight » Sun Jul 30, 2017 12:56 pm

I am working on a Mac and would like to save useful snips of scripts in some form of searchable document. I would also like to preserve the colours of the font that are used in the IDE. The problem is that when a copy and paste action is performed the text is displayed with garish colours as can be seen in the screen shot
Screen Shot 2017-07-30 at 12.27.02.png
Source and pasted text colours
I suspect that most apps are using some form of RTF when pasting from the global system clipboard, so I had a look first at what Livecode reads:

Code: Select all

{\rtf1\mac {\fonttbl{\f0\fnil \fcharset77 (Text);}}
{\colortbl;\red0\green0\blue0;\red0\green32639\blue0;\red0\green0\blue0;\red32639\green32639\blue0;\red32639\green0\blue32639;\red0\green32639\blue32639;}
\pard \f0 \fs24 \cf2 # Returns a list of files with the full paths
\par \pard \cf3 function listFilesWithPaths pFolder, pRecurse
\par \pard   \cf4 local\cf3  tTotalFiles, tCurrentFiles, tFolders
\par \pard   
\par \pard   \cf4 set\cf3  \cf5 the\cf3  \cf6 defaultFolder\cf3  \cf5 to\cf3  pFolder
\par \pard   \cf4 put\cf3  filteredFilesWithPaths() \cf5 into\cf3  tCurrentFiles
\par \pard   
\par \pard   if \cf5 not\cf3  pRecurse then 
\par \pard     \cf4 return\cf3  tCurrentFiles
\par \pard   end if
\par \pard   if tCurrentFiles \cf5 is\cf3  \cf5 not\cf3  \cf5 empty\cf3  then
\par \pard     \cf4 put\cf3  tCurrentFiles & \cf4 return\cf3  \cf5 after\cf3  tTotalFiles\cf2 #
\par \pard \cf3   end if
Next I saved the wrong colour version as RTF and looked at the code in a plain text editor:

Code: Select all

{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
\cocoascreenfonts1{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;\red0\green255\blue0;\red255\green255\blue0;\red255\green0\blue255;
\red0\green255\blue255;}
{\info
{\keywords cursorLocation=384, fitsPagesWidth=1}}\paperw11900\paperh16840\margl1008\margr1008\margt283\vieww15520\viewh18460\viewkind1\viewscale125
\deftab720
\pard\pardeftab720

\f0\fs24 \cf2 # Returns a list of files with the full paths\
\pard\pardeftab720
\cf0 function listFilesWithPaths pFolder, pRecurse\
  \cf3 local\cf0  tTotalFiles, tCurrentFiles, tFolders\
  \
  \cf3 set\cf0  \cf4 the\cf0  \cf5 defaultFolder\cf0  \cf4 to\cf0  pFolder\
  \cf3 put\cf0  filteredFilesWithPaths() \cf4 into\cf0  tCurrentFiles\
  \
  if \cf4 not\cf0  pRecurse then \
    \cf3 return\cf0  tCurrentFiles\
  end if\
  if tCurrentFiles \cf4 is\cf0  \cf4 not\cf0  \cf4 empty\cf0  then\
    \cf3 put\cf0  tCurrentFiles & \cf3 return\cf0  \cf4 after\cf0  tTotalFiles\cf2 #\
\cf0   end if\
    }
Looking at the lines of RTF that define the colours :

Original from Livecode IDE

Code: Select all

{\colortbl;\red0\green0\blue0;\red0\green32639\blue0;\red0\green0\blue0;\red32639\green32639\blue0;\red32639\green0\blue32639;\red0\green32639\blue32639;}
Wrong colours save by app "Bean"

Code: Select all

{\colortbl;\red255\green255\blue255;\red0\green255\blue0;\red255\green255\blue0;\red255\green0\blue255;
\red0\green255\blue255;}
It seems that the problem of wrong colours is being caused by a problem with bit depth i.e. the target applications only understand 8 bits so interpret 32639 e.t.c as 255.

Is there a simple solution? The complex one is to parse the RTF text, setting the colour table to values, less than 255, that represent colours that look similar to the originals and write the new version of the text back to the clipboard before pasting elsewhere. However, despite reading the dictionary I suspect that I may have missed some thing.

Any thoughts?

Edit:
Just found this in the Terminal man pbpaste
* Encoding:

pbcopy and pbpaste use locale environment variables to determine the
encoding to be used for input and output. For example, absent other
locale settings, setting the environment variable LANG=en_US.UTF-8 will
cause pbcopy and pbpaste to use UTF-8 for input and output. If an
encoding cannot be determined from the locale, the standard C encoding
will be used. Use of UTF-8 is recommended. Note that by default the
Terminal application uses the UTF-8 encoding and automatically sets the
appropriate locale environment variable.
I'm not sure if the UTF8 also restricts the colours to 8 bit.
best wishes
Skids

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Copy IDE Script then paste to TextEdit Colour(Color) pro

Post by Klaus » Sun Jul 30, 2017 2:10 pm

Hi Simon,

obviously this is a known bug and I added a comment to it referring to this thread.
http://quality.livecode.com/show_bug.cgi?id=19270

Best

Klaus

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Copy IDE Script then paste to TextEdit Colour(Color) pro

Post by bogs » Sun Jul 30, 2017 3:21 pm

Pardon me if I've completely misread what your trying to accomplish, but it seems an ideal use of MaxV's Code sharer, which has the ability to save to html, which most rtf programs that I have come across can parse or, alternately, you could just save the snippets to html and use anchors/ref tags to make it searchable with a simple toc or db (storing urls/file locations).

This doesn't solve the rtf mis-reading the colors, but does give you an easy way to accomplish what your trying to do, again, unless I completely misread it.
Image

Simon Knight
Posts: 845
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Copy IDE Script then paste to TextEdit Colour(Color) pro

Post by Simon Knight » Mon Jul 31, 2017 9:32 am

Hi Klaus, it was not obvious to me! I have looked at the bug reports and there are two that are similar but refer to the background colour. So I have decided to raise a bug report No. 20209 http://quality.livecode.com/show_bug.cgi?id=20209. It could be working as intended although I now think things are going wrong.

In my bug report I also note that the value 32639 is hex &7F7F and that &007F i.e. &7F is very close to the correct or expected colour values. I suspect that Livecode is incorrectly duplicating the correct value and converting to decimal.

Bogs, you prompted me to look at the HTML text :

Code: Select all

<p><font color="#007F00"># Returns a list of files with the full paths</font></p>
<p><font color="#000000">function listFilesWithPaths pFolder, pRecurse</font></p>
<p><font color="#000000">  </font><font color="#7F7F00">local</font><font color="#000000"> tTotalFiles, tCurrentFiles, tFolders</font></p>
<p><font color="#000000">  </font></p>
<p><font color="#000000">  </font><font color="#7F7F00">set</font><font color="#000000"> </font><font color="#7F007F">the</font><font color="#000000"> </font><font color="#007F7F">defaultFolder</font><font color="#000000"> </font><font color="#7F007F">to</font><font color="#000000"> pFolder</font></p>
The colours are in 24 bit format e.g. &7F &7F 00 which supports the idea that Livecode is making an error converting the colour values into rich text format e,g, RGB &7F &7F 00 is RGB 127,127,000 Not 32639, 32639, 0
best wishes
Skids

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Copy IDE Script then paste to TextEdit Colour(Color) pro

Post by [-hh] » Tue Aug 01, 2017 12:48 pm

[a] I have seen in the bug report that you intend to work around. For that you could try to use the fullClipboardData.
Probably the styledText bears the same problem, the htmltext seems to be OK. So you could try to use a browser widget for the display of your snippets?
shiftLock happens

Simon Knight
Posts: 845
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Copy IDE Script then paste to TextEdit Colour(Color) pro

Post by Simon Knight » Tue Aug 01, 2017 1:58 pm

I tried using all three commands that give access to the clipboard data as well as the various array keys that have RTF in their name. I also attempted to use an Applescript to write to the clipboard but I could only get plain text to copy as my applescript is not good enough to use the class «class RTF» . In every case LC altered the colour values.

When I write my corrected RTF back to the clipboard Livecode writes two bytes whereas all other apps seem to expect one. I suspect that the feature / bug was introduced with Unicode. The HTML may work because the internet as a whole uses 8 bits but who knows?

At present it seems that it is not possible to export rich text from a LC field via the clipboard for use in other non LC applications.
So you could try to use a browser widget for the display of your snippets?
I was attempting to use either OmniOutliner or Scrivener. All is well if you remain within Livecode.

I've just tried to write html to see if LC creates the RTF as well but I can't get it to work.

I have attached my experimental stack should anyone be interested in having a play. All the code is in the card script.
Attachments
ClipboardExperiments.zip
Zip of stack file
(3.45 KiB) Downloaded 154 times
best wishes
Skids

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Copy IDE Script then paste to TextEdit Colour(Color) pro

Post by [-hh] » Tue Aug 01, 2017 2:22 pm

What works here correctly regarding colors is to set the htmltext of the browser widget and then copy (as usual) from it and paste into TextEdit (or any other app that can display htmltext).
[Though you may have to correct line endings ...]
shiftLock happens

Simon Knight
Posts: 845
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Copy IDE Script then paste to TextEdit Colour(Color) pro

Post by Simon Knight » Tue Aug 01, 2017 3:25 pm

Ah, now I understand, thanks I will try that out.
best wishes
Skids

Simon Knight
Posts: 845
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Copy IDE Script then paste to TextEdit Colour(Color) pro

Post by Simon Knight » Wed Aug 02, 2017 7:34 am

Works a treat.

Odd that the copy from the Browser widget works where the copy as html from field does not.
best wishes
Skids

Post Reply

Return to “Talking LiveCode”