2014-09-13 42 views
0

我有一個創建的HTML文件,並使用文本編輯打開它們一個AppleScript:爲什麼TextEdit將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 "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文件的方式?

回答

2

要打開空的HTML文件無鎖:

首先解決方法:檢查 「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 
+0

謝謝您的解決方案,但我相當希望通過textedit – aquagremlin 2014-09-26 03:46:15

+0

這個行爲的'under hood'解釋這是因爲TextEdit必須重寫文件(HTML格式無效),所以它必須有你的權限來覆蓋文件。 當文檔被鎖定時,只需輸入一個字符,Textedit會詢問您是否要覆蓋HTML文件,就是這樣,不需要root訪問 – jackjr300 2014-09-29 03:12:19

相關問題