クリップボードからテキストを受け取り、クリップボードに自動的に書き込むstrwrap
関数をラップしました。strWrap
ここでは、Mac のシステム名が Darwin であると想定しています。この関数は Windows マシンで動作します (申し訳ありませんが、Linux はバリエーションが多すぎて実現できません。また、この関数の対象となるパッケージを使用するほとんどの人は Linux ユーザーではありません)。
psych
パッケージのread.clipboard関数をモデルにして関数を作成しました。残念ながら、 talkstats.comで Mac を持っている人に試してもらいましたが、うまくいきませんでした。これをMacでも機能させるにはどうすればよいですか?このSOの投稿によると、私のコードはMacユーザーにも機能するはずです。
これが期待どおりに機能する場合、Mac ユーザーの場合はクリップボードからの読み取りと、終了時のクリップボードへの書き込みの両方ができるはずです。#
問題を理解しやすくするために、Mac 固有の行の最後に を付けました。
strWrap <-
function(text = "clipboard", width = 70) {
if (text == "clipboard") {
if (Sys.info()["sysname"] == "Darwin") { #
text <- paste(pipe("pbpaste"), collapse=" ")#
} #
if (Sys.info()["sysname"] == "Windows") {
text <- paste(readClipboard(), collapse=" ")
}
}
x <- gsub("\\s+", " ", gsub("\n|\t", " ", text))
x <- strwrap(x, width = width)
if (Sys.info()["sysname"] == "Windows") {
writeClipboard(x, format = 1)
}
if (Sys.info()["sysname"] == "Darwin") { #
j <- pipe("pbcopy", "w") #
cat(x, file = j) #
close(j) #
} #
writeLines(x)
}
X <- "Two households, both alike in dignity, In fair Verona, where we lay
our scene, From ancient grudge break to new mutiny, Where civil blood
makes civil hands unclean. From forth the fatal loins of these two
foes A pair of star-cross'd lovers take their life; Whose
misadventured piteous overthrows Do with their death bury their
parents' strife. The fearful passage of their death-mark'd love, And
the continuance of their parents' rage, Which, but their children's
end, nought could remove, Is now the two hours' traffic of our stage;
The which if you with patient ears attend"
strWrap(X, 70)