2012-12-19 50 views
0

我正在使用拖放功能將郵件從郵件中拖放到共享服務器上的相關作業子文件夾中。然後我使用一個腳本按照創建的日期進行排序(這是我將它們拖過的日期),然後根據它的容器名稱重命名該文件。使用蘋果腳本提取.eml文件發送的發件人和日期

但是我想用腳本從每個.eml文件中提取發送的日期,發件人和主題,然後使用該信息重命名文件。

有一個以前的帖子回答了類似的問題,但我不清楚(因爲我是一個腳本新手)如何將它放入我的腳本。 它可以發現: 「的AppleScript - 獲取.eml文件信息」

答案是:

set fromField to text 7 thru -1 of (do shell script "cat /test.eml | grep From:") 
set dateField to text 7 thru -1 of (do shell script "cat test.eml | grep Date:") 
set toField to text 5 thru -1 of (do shell script "cat /test.eml | grep To:") 
set subjectField to text 10 thru -1 of (do shell script "cat /test.eml | grep Subject:") 

我想我需要有人寫實際shell腳本,並告訴我在什麼地方它。

回答

0

嘗試:

set delim to " - " 

set myFiles to (choose file with multiple selections allowed) 
set TID to text item delimiters 

repeat with aFile in myFiles 
    set pFile to quoted form of (POSIX path of aFile) 
    set {mDate, mFrom, mSubject} to every paragraph of (do shell script "cat " & pFile & " | grep -e Date -e From: -e Subject:") 

    set mDate to trim(mDate, 7) 
    set AppleScript's text item delimiters to " " 
    set myDate to text item 3 of mDate & space & text item 2 of mDate & ", " & text item 4 of mDate 
    set AppleScript's text item delimiters to {""} 
    set myDate to (date myDate) 
    set myDate to day of myDate & (month of myDate as integer) & year of myDate as text 

    set mFrom to trim(mFrom, 7) 
    set mSubject to trim(mSubject, 10) 

    set newName to myDate & delim & mFrom & delim & mSubject 

    -- You will have to edit newName so it meets file naming standards 
    --tell application "System Events" to set aFile's name to mSubject 
end repeat 
set text item delimiters to TID 

on trim(myText, firstChar) 
    try 
     set myText to text firstChar thru -1 of myText 
    on error 
     set myText to "None" 
    end try 
end trim 
+0

首先感謝您的幫助安裝Satimage Osax擴展。我嘗試了腳本,它正在獲取信息。然而,當我添加一行將aFile重命名爲newName時,它出現了堆棧溢出錯誤。還有一種方法可以將日期轉換爲ddmmyyyy格式? –

+0

請參閱編輯日期格式。您需要操作newName以符合文件命名標準以避免錯誤。 – adayzdone

+0

我收到一個Finder錯誤:無法在第一個修剪(mDate,7)處繼續修剪。如果我刪除所有修剪,它將重命名文件(大量文本),但主題丟失。所有你的幫助非常感謝 –

相關問題