为什么TextEdit以锁定状态打开html文件



我有一个appdescription,它创建html文件并使用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文件采取这种方式?

打开没有锁的空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

相关内容

  • 没有找到相关文章