AppleScript 改行 コード 置換

Quick Edit Pencil

改行を置換

returnキーはLF、ASCII character (10)と\nはLF、ASCII character (13)と\rはCR、controlキー + returnキーはラインブレーク

 

(*テスト文字列を変数にセット*)

set str to "1LINE(LF)" & (ASCII character (10)) & "2LINE(CR)" & (ASCII character (13)) & "3LINE(LineBrake)
3LINE_2"

log "★生str: " & str & ""

(*★生str: 1LINE(LF)

2LINE(CR)

3LINE(LineBrake)
3LINE_2*)

 

(*LF置換*)

set LFstr to do shell script "echo " & quoted form of str & " | perl -pe 's/\\n//g'"

log "LF置換str: " & LFstr & ""

(*LF置換str: 1LINE(LF)2LINE(CR)

3LINE(LineBrake)
3LINE_2*)

 

(*CR置換*)

set CRstr to do shell script "echo " & quoted form of str & " | perl -pe 's/\\r//g'"

log "CR置換str: " & CRstr & ""

(*CR置換str: 1LINE(LF)

2LINE(CR)3LINE(LineBrake)
3LINE_2*)

 

(*ラインブレーク置換*)

set LAINBLAKEstr to do shell script "echo " & quoted form of str & " | perl -pe 's/
//g'"

log "★ラインブレーク置換str: " & LAINBLAKEstr & ""

(*★ラインブレーク置換str: 1LINE(LF)

2LINE(CR)

3LINE(LineBrake)3LINE_2*)

 

(*LFCR、ラインブレークをまとめて置換*)

set str to do shell script "echo " & quoted form of str & " | perl -pe 's/\\n|\\r|
//g'"

log "★改行置換str: " & str & ""

(*★改行置換str: 1LINE(LF)2LINE(CR)3LINE(LineBrake)3LINE_2*)

 


 

 

アップルスクリプトでの改行の取り扱いメモ

 

不可視文字の取り扱い

SPC  半角スペース  0x0020  英数+space  space  ASCII character (32)

  非改行スペース  0x00A0 英数+option+space  ASCII character (160)

  全角スペース  0x3000     かな+space

HT  タブ  0x0009  tab  tab  ASCII character (9)

ラインブレーク  0x2028  英数+control+return  行区切りで改行ではない

LF  パラグラフブレーク  0x000a  return  return / option+return  ASCII character (10) \\n

CR  キャリッジリターン  0x000d  option+control+return   ASCII character (13) \\r

FF  改ページ  0x000c  command+return  ASCII character (12)

改行を再認識

改行コード - Wikipedia

3/3 箇条書きで[Enter]と[Shift]+[Enter]の使い分け [ワード(Word)の使い方] All About

アップルスクリプトの改行に関するサイト

AppleScriptでの改行コードの扱い

それぞれの改行コードの10進数と16進数表示

10進数16進数文字
10 0x0aLF
130x0dCR
returnキーはLF、\nはLF、\rはCRだった。

アップルスクリプトの改行の置換に関するサイト

クリップボードに入っている情報の改行を外してクリップボードに書き戻す

try
  set a to the clipboard
  set b to a as string
  set c to repChar(b, ASCII character 10, "") of me
  set c to repChar(c, ASCII character 13, "") of me
  set the clipboard to c
end try
–文字置換ルーチン
on repChar(origText, targStr, repStr)
  set {txdl, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, targStr}
  set temp to text items of origText
  set AppleScript’s text item delimiters to repStr
  set res to temp as text
  set AppleScript’s text item delimiters to txdl
  return res
end repChar

Apple Scriptの覚え書き

on replaceTxt(myText, searchTxt, replaceTxt) --置換専用
set oldDel to AppleScript's text item delimiters --デリミタを保存しておく
set AppleScript's text item delimiters to searchTxt --渡されたサーチワードをデリミタにする
set myText to every text item of myText --分解して
set AppleScript's text item delimiters to replaceTxt --渡された置換ワードをデリミタにする
set myText to myText as string --つなぐ
set AppleScript's text item delimiters to oldDel --デリミタを元に戻す
return myText
end replaceTxt
▲上のルーチンを使って改行コードを変換したりとか。文字列aaaを変換する例。
--まずは改行コードをセットして置いて
set cr to ASCII character(13)--CR
set lf to ASCII character(10)--LF
set crlf to cr & lf--CR+LF
set aaa to my replaceTxt(aaa, cr , lf) --CRをLFに変換/MAC→UNIX
set aaa to my replaceTxt(aaa, lf , cr) --LFをCRに変換/UNIX→MAC
set aaa to my replaceTxt(aaa, crlf , cr) --CR+LFをCRに変換/Windows→MAC
 
資料
[TIPs]不可視文字の入力: [FORCE]
鳶嶋工房 / AppleScript / Tips / 文字コード
ASCIIコード表
HTML Codes - Table of ascii characters and symbols
ASCII and Binary Characters (The Characters)

このブログの人気の投稿

書字方向 横書方向変換スクリプト 左書きから右書きへ(コピペ用途)

PowerShellのGetDetailsOf メソッドでプロパティの詳細情報のID番号と項目名を列挙します

PowerShellで複数ファイルのプロパティを取得する方法(準備編)

簡単 YouTube動画をダウンロード、音声のみ保存する方法 2019

DOMノードオブジェクトを文字列に変換する

PowerShellでJPG画像のリサイズとウォーターマーク画像との合成を同時に行う

Blender: 辺の長さを数値で指定するアドオン