AppleScript では、.rtf ファイルの特定の行をどのように読み書きできますか。これまでのところ、テキストをファイルに追加/置換できます
tell application "TextEdit"
set text of document 1 to "test"
end tell
しかし、どうすれば特定の行を選択できますか。また、varを4行目のテキストに設定するようなことはできますか?
AppleScript では、.rtf ファイルの特定の行をどのように読み書きできますか。これまでのところ、テキストをファイルに追加/置換できます
tell application "TextEdit"
set text of document 1 to "test"
end tell
しかし、どうすれば特定の行を選択できますか。また、varを4行目のテキストに設定するようなことはできますか?
次のように、4 番目の段落 (ラインフィード、キャリッジ リターン、またはキャリッジ リターン + ラインフィードで区切られた項目) を取得できます。
tell application "TextEdit"
paragraph 4 of document 1
end tell
TextEdit では、実際に段落を変更できます。
tell application "TextEdit"
set paragraph 4 of document 1 to "zz" & linefeed
end tell
通常、段落を変更しようとすると、エラーが発生します。
set paragraph 1 of "aa" & linefeed & "bb" to "zz"
-- error "Can’t set paragraph 1 of \"aa
-- bb\" to \"zz\"."
他の人がプレーン テキスト ファイルにテキストを追加する方法を検索している場合は、次のコマンドでstarting at eof
指定子を使用できます。write
set fd to open for access "/tmp/a" with write permission
write "aa" & linefeed to fd as «class utf8» starting at eof
close access fd
これにより、4 行目が次のように置き換えられzz
ます。
set input to read "/tmp/a" as «class utf8»
set text item delimiters to linefeed
set ti to text items of input
set item 4 of ti to "zz"
set fd to open for access "/tmp/a" with write permission
set eof fd to 0
write (ti as text) to fd as «class utf8»
close access fd
もちろん、それはできます。
tell application "TextEdit"
set abc to text of document 1
end tell
set text item delimiters to "
"
set abc to item 4 of text items of abc
まず、ドキュメント 1 のテキストを取得します (可変テキストに名前を付けることはできません。これは予約語です)。
tell application "TextEdit"
set abc to text of document 1
end tell
次に、テキスト項目の区切り文字を改行に設定します。(「」内で戻るボタンを押した場合のみ動作します)
set text item delimiters to "
"
最後に、4 番目のテキスト項目を取得します。(区切り文字の改行を設定したため、4 行目です)
set abc to item 4 of text items of abc
(このスクリプトは、ユーザーがテキスト編集で Enter キーを押して新しい行を開始した場合にのみ機能します)