0

HTML ファイルを作成し、textedit を使用してそれらを開く AppleScript があります。

try
    tell application "Finder" to set save_folder to (target of window 1) as alias
on error
    set save_folder to path to desktop
end try

set textFile to (choose file name with prompt "Enter file name:" default location save_folder default name "Comment.html") as text
if textFile does not end with ".html" then set textFile to textFile & ".html"
do shell script "touch " & quoted form of POSIX path of textFile
tell application "Finder"
    set file type of (textFile as alias) to "html"
    set creator type of (textFile as alias) to "sfri"
end tell
tell application "TextEdit"
    open file textFile
    activate
end tell

ファイルがロックされた状態で開くのは面倒です。しかし、ファイルタイプと作成者を TEXT と ttxt (TextEdit の識別子) に設定すると、すべて問題ありません。HTMLファイルを編集するためだけにtexteditへのルートアクセスを許可しなければならないのは嫌ですが、それが必要だと思いますか? 別のテキスト エディターに切り替えることもできると思いますが、なぜ TextEdit が html ファイルでこのように動作するのかという疑問が残ります。

4

1 に答える 1

2

ロックなしで空の HTML ファイルを開くには:

最初の解決策: TextEditDisplay HTML files as HTML code instead of formatted text設定の " " ボタンをチェックします。ただし、ドキュメントに HTML コードを記述する必要があります。

--

2 番目の解決策: 次のように、空の HTML ファイルに対して有効な HTML コードを記述します。

set base to "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\"><html><head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><meta http-equiv=\"Content-Style-Type\" content=\"text/css\">
<title></title><meta name=\"Generator\" content=\"Cocoa HTML Writer\"><meta name=\"CocoaVersion\" content=\"1265.21\">
<style type=\"text/css\"></style></head><body></body></HTML>"

try
    tell application "Finder" to set save_folder to (target of window 1) as alias
on error
    set save_folder to path to desktop
end try
set textFile to (choose file name with prompt "Enter file name:" default location save_folder default name "Comment.html") as text
if textFile does not end with ".html" then set textFile to textFile & ".html"
do shell script "printf  " & (quoted form of base) & " > " & quoted form of POSIX path of textFile
tell application "TextEdit"
    open file textFile
    activate
end tell
于 2014-09-25T02:51:34.170 に答える