为什么TextEdit将html文件打开为已锁定?

时间:2014-09-13 19:53:45

标签: html osx-mavericks textedit locked-files

我有一个创建html文件的AppleScript,并使用textedit打开它们:

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文件而给root用户访问textedit,但我想那是什么需要?我可以切换到另一个文本编辑器,但我想问的是为什么TextEdit用html文件这样做?

1 个答案:

答案 0 :(得分:2)

打开没有锁定的空HTML文件:

第一个解决方案:检查 TextEdit 首选项中的“Display HTML files as HTML code instead of formatted text”按钮。 但是您必须在文档中编写HTML代码

-

第二个解决方案:为空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