テキストファイルを作成

新規に(標準)テキストファイル または RTFファイルを作成するスクリプトです。
最前面のウインドウに作成されます。
RTFファイルは、一旦、標準テキストとしてテンポラリフォルダーに書き出しRTFファイルに変換、
それをリネームして最前面のウインドウに移動します。

新規(標準)テキストファイルを作成
tell application "Finder"
set prop_ to properties
set savePath to (insertion location of prop_) as alias
tell folder savePath
if not (exists file "名称未設定.txt") then
set theName to "名称未設定.txt"
else
repeat with i from 2 to 65536
if not (exists file ("名称未設定 " & i & ".txt")) then
set theName to "名称未設定 " & i & ".txt"
exit repeat
end if
end repeat
end if
end tell
try
set aDoc to make new file at savePath with properties {name:theName}
select aDoc
on error msg
beep
display alert msg
return
end try
end tell
新規RTFファイルを作成
-- https://force4u.cocolog-nifty.com/skywalker/2017/01/applescriptrtf-.html

tell application "Finder"
set anUUID to do shell script "uuidgen"
set aProp to properties
set savePath to (insertion location of aProp) as alias
set tmpFol to (path to temporary items)
tell folder savePath
if not (exists file "名称未設定.rtf") then
set theName to "名称未設定.rtf"
else
repeat with i from 2 to 65536
if not (exists file ("名称未設定 " & i & ".rtf")) then
set theName to "名称未設定 " & i & ".rtf"
exit repeat
end if
end repeat
end if
end tell
try
make new file at tmpFol with properties {name:(anUUID & ".txt")}
on error msg
beep
display alert msg
return
end try
set aliasNewFile to (((tmpFol) as text) & ((anUUID & ".txt") as text)) as alias
set theNewFilePath to quoted form of (POSIX path of aliasNewFile)
do shell script ("textutil -convert rtf " & " -extension rtf " & theNewFilePath)
repeat 100 times
delay 0.3
if exists file (anUUID & ".rtf") of tmpFol then exit repeat
end repeat
set rtfFilePath to (((tmpFol) as text) & ((anUUID & ".rtf") as text)) as alias
set savePath to (savePath as text) & theName
end tell

try
do shell script "mv " & (quoted form of POSIX path of rtfFilePath) & " " & (quoted form of POSIX path of savePath)
tell application "Finder" to select (savePath as alias)
on error msg
beep
display alert msg
return
end try
delay 0.2
do shell script "rm -f " & theNewFilePath

更新履歴

inserted by FC2 system