Почему TextEdit открывает html-файлы как заблокированные?

У меня есть applescript, который создает 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), тогда все будет хорошо. Я ненавижу предоставлять root-доступ к textedit только для редактирования html-файлов, но я думаю, это то, что нужно? Полагаю, я мог бы переключиться на другой текстовый редактор, но у меня остался вопрос, почему TextEdit действует таким образом с файлами html?


person aquagremlin    schedule 13.09.2014    source источник


Ответы (1)


Чтобы открыть пустой файл HTML без блокировки:

Первое решение: проверьте кнопку "Display HTML files as HTML code instead of formatted text" в настройках TextEdit. Но вы должны написать 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
person jackjr300    schedule 25.09.2014
comment
Спасибо за ваши решения, но я скорее надеялся на объяснение этого поведения «под капотом» с помощью textedit. - person aquagremlin; 26.09.2014
comment
Это связано с тем, что TextEdit должен перезаписать файл (формат HTML недействителен), поэтому у него должно быть ваше разрешение на перезапись файла. Когда документ заблокирован, просто введите символ, Textedit спросит вас, хотите ли вы перезаписать файл HTML, вот и все, root-доступ не требуется. - person jackjr300; 29.09.2014