2012-12-08 71 views
3

問題是plain text URL to HTML code (Automator/AppleScript)的續集。替換文本文件中,使用AppleScript和sed

假設我有一個簡單的txt文件/Users/myname/Desktop/URLlist.txt:(http://..

title 1 
http://a.b/c 

title 2 
http://d.e/f 

... 

我想(1)將所有的URL到HTML代碼, (2)添加

&nbsp;<br /> 

到每個空行,所以,前述內容將變得:

title 1 
<a href="http://a.b/c">http://a.b/c</a> 
&nbsp;<br /> 
title 2 
<a href="http://d.e/f">http://d.e/f</a> 
&nbsp;<br /> 
... 

我來到以下Applescript:

set inFile to "/Users/myname/Desktop/URLlist.txt" 
set middleFile to "/Users/myname/Desktop/URLlist2.txt" 
set outFile to "/Users/myname/Desktop/URLlist3.txt" 

do shell script "sed 's/\\(http[^ ]*\\)/<a href=\"\\1\">\\1<\\/a>/g' " & quoted form of inFile & " >" & quoted form of middleFile 
do shell script "sed 's/^$/\\&nbsp;<br \\/>/g' " & quoted form of middleFile & " >" & quoted form of outFile 

它的工作,但它是多餘的(和愚蠢?)。任何人都可以使它更簡潔嗎?可以只涉及一個文本文件而不是三個(即/Users/myname/Desktop/URLlist.txt中的原始內容被最終結果覆蓋)?

非常感謝您提前。

+0

+1對於模型樣本問題,帶有樣本輸入,需要的輸出和樣本代碼(gasp)。繼續發帖,祝你好運! – shellter

回答

2

嘗試:

set inFile to "/Users/myname/Desktop/URLlist.txt" 

set myData to (do shell script "sed ' 
/\\(http[^ ]*\\)/ a\\ 
&nbsp;<br /> 
' " & quoted form of inFile & " | sed 's/\\(http[^ ]*\\)/<a href=\"\\1\">\\1<\\/a>/g' ") 

do shell script "echo " & quoted form of myData & " > " & quoted form of inFile 

這將讓你在你的腳本以後使用MYDATA的變量。如果這不是一個更大的腳本的一部分,並且您只是簡單地修改文件,請使用-j選項,正如jackjr300所示。而且,這個腳本查找原始模式並追加新行,而不是簡單地查找空行。

編輯:

set inFile to "/Users/myname/Desktop/URLlist.txt" 
set myData to (do shell script "sed 's/\\(http[^ ]*\\)/<a href=\"\\1\">\\1<\\/a>/g; s/^$/\\&nbsp;<br \\/>/g' " & quoted form of inFile) 
do shell script "echo " & quoted form of myData & " > " & quoted form of inFile 
+0

謝謝。你的代碼在邏輯上與我所要求的不同。實際上,我想處理的真實文本文件比上述示例輸入(例如涉及一些引用的句子)更復雜。 (順便說一句,你的代碼與其他兩個答案不同,沒有文本編碼的問題,我不知道爲什麼......) – user1870433

+0

我以爲你想要 
來關注每個URL。對於每一個空行看到我編輯的版本。 – adayzdone

+0

對不起,我沒有澄清它足夠....對於您的編輯版本,我想你的意思是:**第1行** '設置inFile爲「/Users/myname/Desktop/URLlist.txt 「' **第2行**'將myData設置爲(do shell script」sed's/\\(http [^] * \\)/ \\ 1 <\\/a>/g; s/^ $/\\  
/g'「&inFile的引用形式)' ** line 3 **'do shell script」echo「&quoted form of myData&」>「&quoted form of inFile' 對於第二行,我添加了'將myData設置爲('在開始處和'\\\'之前的第一個&. 非常感謝! –
user1870433

2

使用-i ''選項編輯科幻LES原地。

set inFile to "/Users/myname/Desktop/URLlist.txt" 

do shell script "sed -i '' 's:^$:\\&nbsp;<br />:; s:\\(http[^ ]*\\):<a href=\"\\1\">\\1</a>:g' " & quoted form of inFile 

如果你想原始文件的副本,請使用一個特定的編輯等擴展sed -i ' copy'

- 更新時間:

一個`DOCTYPE是必需的序言。 由於遺留原因,DOCTYPE是必需的。省略時,瀏覽器傾向於使用與某些規範不兼容的不同渲染模式。在文檔中包含DOCTYPE可確保瀏覽器盡最大努力遵循相關規範。

HTML lang屬性可用於聲明網頁的語言或網頁的一部分。這是爲了幫助搜索引擎和瀏覽器。根據W3C建議,您應該爲<html>標籤內的lang屬性聲明每個網頁的主要語言

<meta>標籤提供有關HTML文檔的元數據。 <meta>標籤總是進入<head>元素。 http-equiv屬性爲內容屬性的信息/值提供HTTP標頭。 content:與http-equiv或name屬性關聯的值。 charset:要正確顯示HTML頁面,瀏覽器必須知道要使用的字符集。

在此腳本中:我將「utf-8」作爲編碼,通過原始文件的編碼進行更改。

set inFile to "/Users/myname/Desktop/URLlist.html" -- text file with a ".html" extension 
set nL to linefeed 
set prepandHTML to "<!DOCTYPE html>\\" & nL & "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en-US\" lang=\"en-US\">\\" & nL & tab & "<head><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\\" & nL & "</head>\\" & nL 

do shell script "sed -i '' 's:^$:\\&nbsp;<br />:; s:\\(http[^ ]*\\):<a href=\"\\1\">\\1</a>:g; 1s~^~" & prepandHTML & "~' " & quoted form of inFile 
do shell script "echo '</html>' " & quoted form of inFile -- write last HTML tag 
+0

功能和簡潔。謝謝。唯一的問題是無法在Finder中通過雙擊打開新的URLlist.txt(但可以通過將其圖標拖到Safari/Firefox來打開)。我該如何解決這個問題? – user1870433

+0

要通過雙擊打開文檔,請使用「**。html **」擴展名而不是「** .txt **」保存文檔。 「HTML」文檔並不像您想象的那麼簡單,您必須指定一個DOCTYPE,Namespaces(xmlns,xml:lang,lang)和Meta(http-equiv,內容,** charset **)以正確顯示瀏覽器。 看看我更新的答案。 – jackjr300

+0

感謝您的詳細更新。有用! – user1870433

1

我無法很好地理解sed命令(它會讓我的大腦受傷),所以這裏是執行此任務的applescript方式。希望能幫助到你。

set f to (path to desktop as text) & "URLlist.txt" 

set emptyLine to "&nbsp;<br />" 
set htmlLine1 to "<a href=\"" 
set htmlLine2 to "\">" 
set htmlLine3 to "</a>" 

-- read the file into a list 
set fileList to paragraphs of (read file f) 

-- modify the file as required into a new list 
set newList to {} 
repeat with i from 1 to count of fileList 
    set thisItem to item i of fileList 
    if thisItem is "" then 
     set end of newList to emptyLine 
    else if thisItem starts with "http" then 
     set end of newList to htmlLine1 & thisItem & htmlLine2 & thisItem & htmlLine3 
    else 
     set end of newList to thisItem 
    end if 
end repeat 

-- make the new list into a string 
set text item delimiters to return 
set newFile to newList as text 
set text item delimiters to "" 

-- write the new string back to the file overwriting its contents 
set openFile to open for access file f with write permission 
write newFile to openFile starting at 0 as text 
close access openFile 

編輯:如果你有麻煩的編碼這兩個處理器將處理讀/寫正確。所以只需將它們插入代碼中並調整這些行以使用處理程序。祝你好運。

注意:使用文本編輯打開文件時,使用文件菜單和特別爲UTF-8打開。

on writeTo_UTF8(targetFile, theText, appendText) 
    try 
     set targetFile to targetFile as text 
     set openFile to open for access file targetFile with write permission 
     if appendText is false then 
      set eof of openFile to 0 
      write «data rdatEFBBBF» to openFile starting at eof -- UTF-8 BOM 
     else 
      tell application "Finder" to set fileExists to exists file targetFile 
      if fileExists is false then 
       set eof of openFile to 0 
       write «data rdatEFBBBF» to openFile starting at eof -- UTF-8 BOM 
      end if 
     end if 
     write theText as «class utf8» to openFile starting at eof 
     close access openFile 
     return true 
    on error theError 
     try 
      close access file targetFile 
     end try 
     return theError 
    end try 
end writeTo_UTF8 

on readFrom_UTF8(targetFile) 
    try 
     set targetFile to targetFile as text 
     targetFile as alias -- if file doesn't exist then you get an error 
     set openFile to open for access file targetFile 
     set theText to read openFile as «class utf8» 
     close access openFile 
     return theText 
    on error 
     try 
      close access file targetFile 
     end try 
     return false 
    end try 
end readFrom_UTF8 
+0

謝謝。 Applescript的工作原理除了(1)某些中文字符會變成亂碼,(2)新的URLlist.txt無法在Finder中通過雙擊打開(但可以通過將其圖標拖到Safari/Firefox 文本編碼的問題是否可以糾正? – user1870433

+0

好吧,如果這是一個問題,你可以只讀/寫文件爲「class utf8」,我在文章中增加了一個「編輯部分」,爲你提供處理這些文件的操作。 – regulus6633

+0

非常感謝您的專注時間。由於adayzdone的腳本沒有文本編碼的問題,我很好奇Applescript和sed之間處理文本編碼的差異。 – user1870433