我想知道是否可以在TextEdit中更改某些数字的字体颜色。
例如,所有的'1's为蓝色,'2's红色等...
如果这在TextEdit中是不可能的,那么有人可以建议一个类似的软件来读取.txt文件吗?
答案 0 :(得分:2)
这很有趣! macOS 包括 Perl 和textutil
,用于更改文档格式,因此您可以执行所需的操作而无需安装任何内容... >
#!/bin/bash
perl -pe 'BEGIN{ $/=\1; }
s/0/<font color="red" >0<font color="black">/ ||
s/1/<font color="green" >1<font color="black">/ ||
s/2/<font color="blue" >2<font color="black">/ ||
s/3/<font color="cyan" >3<font color="black">/ ||
s/4/<font color="magenta">4<font color="black">/ ||
s/5/<font color="yellow" >5<font color="black">/ ||
s/6/<font color="pink" >6<font color="black">/ ||
s/7/<font color="orange" >7<font color="black">/ ||
s/8/<font color="teal" >8<font color="black">/ ||
s/9/<font color="gray" >9<font color="black">/ ||
s/\n/<br>/;
' "$1" | textutil -format html -convert rtf -stdin -stdout > output.rtf
将其另存为go
并使其可执行,只需执行一次:
chmod +x go
然后,您可以像这样转换任何文档:
./go input.txt
,它将变成output.rtf
。
因此,如果我从input.txt
中开始以下内容:
1234567890 I love RTF and HTML but not 2 much and I can't spell, or count 1,000,222
1 man had 2 dogs and 333 cats
我最终得到:
由于$/=\1;
, Perl 一次读取1个字符。然后,它用具有不同颜色的HTML font
标签替换任何数字,然后将数字设置为黑色。然后将输出传递到textutil
,让其将输入视为html
并进行RTF输出。
我已经花了一整天的时间了,但我仍然不知道为什么其中一个文档有效而另一个文档无效。我唯一能说的是,textutil
在我注释掉以下将文档中1
的颜色更改为绿色的行时似乎没有崩溃!我不知道为什么!
s/1/<font color="green" >1<font color="black">/ ||
我甚至以不同的方式重写了它,但是它仍然是相同的。我将代码放在下面,以便其他人可以使用它:
#!/usr/bin/env perl
use strict;
use warnings;
my ($i, $x, %HofH);
# Initialise table mapping input bytes to output/actions
# So it's a hash of hashes, each hash stores the corresponding output string and whether it set colour
foreach $i ('A'..'Z', 'a'..'z' ){
$x = ord($i);
$HofH{$x}{out} = $i;
}
# Set up the coloured digits 0-9
$i=48; $HofH{$i}{out} = sprintf("<font color='red'>0") ; $HofH{$i}{colour}=1;
#$i=49; $HofH{$i}{out} = sprintf("1") ; $HofH{$i}{colour}=1; # THIS CRASHES TEXTUTIL ???
$i=50; $HofH{$i}{out} = sprintf("<font color='blue'>2") ; $HofH{$i}{colour}=1;
$i=51; $HofH{$i}{out} = sprintf("<font color='cyan'>3") ; $HofH{$i}{colour}=1;
$i=52; $HofH{$i}{out} = sprintf("<font color='magenta'>4"); $HofH{$i}{colour}=1;
$i=53; $HofH{$i}{out} = sprintf("<font color='yellow'>5") ; $HofH{$i}{colour}=1;
$i=54; $HofH{$i}{out} = sprintf("<font color='pink'>6") ; $HofH{$i}{colour}=1;
$i=55; $HofH{$i}{out} = sprintf("<font color='orange'>7") ; $HofH{$i}{colour}=1;
$i=56; $HofH{$i}{out} = sprintf("<font color='teal'>8") ; $HofH{$i}{colour}=1;
$i=57; $HofH{$i}{out} = sprintf("<font color='grey'>9") ; $HofH{$i}{colour}=1;
# Special cases - CR, LF, ampersand
$i=9; $HofH{$i}{out} = " ";
$i=10; $HofH{$i}{out} = "<br>\n";
$i=13; $HofH{$i}{out} = "<br>\n";
$i=39; $HofH{$i}{out} = "&";
$i=47; $HofH{$i}{out} = "/";
$i=61; $HofH{$i}{out} = "=";
$i=63; $HofH{$i}{out} = "?";
$i=40; $HofH{$i}{out} = "(";
$i=41; $HofH{$i}{out} = ")";
$i=45; $HofH{$i}{out} = "-";
$i=58; $HofH{$i}{out} = ":";
$i=59; $HofH{$i}{out} = ";";
$i=46; $HofH{$i}{out} = ".";
$i=44; $HofH{$i}{out} = ",";
$i=32; $HofH{$i}{out} = " ";
$i=60; $HofH{$i}{out} = "<";
$i=62; $HofH{$i}{out} = ">";
open(my $FILE, $ARGV[0]) or die $!;
binmode($FILE);
local $/ = \1;
my $cnt=0;
while ( my $byte = <$FILE> ) {
my $x = ord($byte);
if(defined $HofH{$x}){
printf("%s",$HofH{$x}{out});
printf("<font color='black'>") if defined $HofH{$x}{colour}
} else {
printf STDERR "ERROR: Character number %d ignored at byte offset %d\n",$x,$cnt;
}
$cnt++;
}
close $FILE;
您通过以下方式运行它:
thisScript.pl someFile.txt | textutil -format html -convert rtf -stdin -stdout > output.rtf