简体   繁体   English

使用 Applescript 将邮件内容添加到 Excel

[英]Adding content from mail to Excel using Applescript

I have gone through and used some great code form the these forms for Adding content from mail to Excel using Applescript.我已经通过这些表单使用了一些很棒的代码,用于使用 Applescript 将内容从邮件添加到 Excel。 I am nearly there but have one more question.我快到了,但还有一个问题。

I have the following layout of an email我有以下电子邮件布局

 From: xxxxxx Gender: xxxxxx Age: >18 - 24 Phone Day: xxxxxx Mobile: Address: xxxxx xxxxx xxxx xxxx

Notice each line has an intro (eg "From:"), this content is being pulled in correctly.注意每一行都有一个介绍(例如“发件人:”),这个内容被正确地拉入。 My issue is adding line 2,3 and 4 of the "Address:" line as it has no intro.我的问题是添加“地址:”行的第 2,3 和 4 行,因为它没有介绍。

Any thoughts?有什么想法吗?

This is what I have used so far.这是我到目前为止所使用的。

Any help gratefully received.感激地收到任何帮助。

 --APPLESCRIPT _main() on _main() set mm to {} tell application "Mail" repeat with m in (get selection) set mm's end to m's content & linefeed end repeat end tell set r to my _retrieve_data(mm) do shell script "printf '%s' " & r's quoted form & " > ~/desktop/$(date +scores_%F_%H%M%S.csv)" return r end _main on _retrieve_data(mm) (* list mm : list of message text *) script o property pp : mm property qq : {} property rr : {} property boundary : do shell script "uuidgen" without altering line endings -- UUID & LF property batch : 50 -- number of messages to be processed at once; combined text should not exceed ca. 200K -- divide messages into batches repeat with i from 1 to count my pp by batch set j to i + batch - 1 if j > (count my pp) then set j to -1 set my qq's end to my pp's items i thru j end repeat -- retrieve data per batch repeat with q in my qq set my rr's end to do shell script "perl -CSDA -w <<'EOF' - " & boundary's quoted form & " use strict; local $\\\\ = qq(\\\\n); local $, = qq(,); my $boundary = shift; my @data = (); my ($new, $complete, $i) = (1, 0, -1); while (<DATA>) { next if ! ($new ||= $_ =~ /^$boundary$/) && $complete; if ( $new ) { ($new, $complete) = (0, 0); ++$i; } /^From:\\\\s*(.+?)\\\\s*$/o && do { $data[$i]{from} = $1; next; }; /^Gender:\\\\s*(.+?)\\\\s*$/o && do { $data[$i]{gender} = $1; next; }; /^Age:\\\\s*(.+?)\\\\s*$/o && do { $data[$i]{age} = $1; next; }; /^Mobile:\\\\s*(.+?)\\\\s*$/o && do { $data[$i]{mobile} = $1; next; }; /^Phone Day:\\\\s*(.+?)\\\\s*$/o && do { $data[$i]{phoneday} = $1; next; }; /^Address:\\\\s*(.+?)\\\\s*$/o && do { $data[$i]{address1} = $1; next; character id(200);}; /^Address:\\\\s*(.+?)\\\\s*$/o && do { $data[$i]{address2} = $1; next; }; /^Address:\\\\s*(.+?)\\\\s*$/o && do { $data[$i]{address3} = $1; next; }; /^Address:\\\\s*(.+?)\\\\s*$/o && do { $data[$i]{address4} = $1; next; }; /^Email:\\\\s*(.+?)\\\\s*$/o && do { $data[$i]{email} = $1; next; }; /^I would like to be kept up to date on future Eden events, competitions and special offers\\\\s*(.+?)\\\\s*$/o && do { $data[$i]{eden} = $1; next; }; /^I would like to be kept up to date on future Kuoni events, competitions and special offers\\\\s*(.+?)\\\\s*$/o && do { $data[$i]{Kuoni} = $1; next; }; $complete = (0 + keys %{$data[$i]} == 12); } my @keys = ('from', 'gender', 'age', 'mobile', 'phoneday', 'address1', 'address2','address3','address4','email', 'eden', 'kuoni' ); print map { s/\\"/\\"\\"/og; qq(\\"$_\\") } @keys; for (@data) { print map { $_ = '' unless defined $_; s/\\"/\\"\\"/og; qq(\\"$_\\") } @{$_}{@keys}; } __END__ " & _join(q, boundary) & " EOF" without altering line endings end repeat -- combine data from each batch set r to my rr's item 1 -- include header repeat with i from 2 to count my rr -- exclude header for rest set r to r & my rr's item i's text from paragraph 2 to text -1 end repeat return r end script tell o to run end _retrieve_data on _join(tt, d) (* list tt : source list string d : separator return string : tt joined with d *) local astid0, t try set {astid0, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {} & d} set t to "" & tt set AppleScript's text item delimiters to astid0 on error errs number errn set AppleScript's text item delimiters to astid0 error errs number errn end try return t end _join --END OF APPLESCRIPT

If any one is interested, below is the code that is pretty much working.如果有人感兴趣,下面是非常有效的代码。

--APPLESCRIPT
_main()
on _main()
    set mm to {}
    tell application "Mail"
        repeat with m in (get selection)
            set mm's end to m's content & linefeed
        end repeat
    end tell
    set r to my _retrieve_data(mm)
    do shell script "printf '%s' " & r's quoted form & " > ~/desktop/$(date +list_%F_%H%M%S.csv)"
    return r
end _main

on _retrieve_data(mm)
    (*
        list mm : list of message text
    *)
    script o
        property pp : mm
        property qq : {}
        property rr : {}
        property boundary : do shell script "uuidgen" without altering line endings -- UUID & LF
        property batch : 50 -- number of messages to be processed at once; combined text should not exceed ca. 200K

        -- divide messages into batches
        repeat with i from 1 to count my pp by batch
            set j to i + batch - 1
            if j > (count my pp) then set j to -1
            set my qq's end to my pp's items i thru j
        end repeat

        -- retrieve data per batch
        repeat with q in my qq
            set my rr's end to do shell script "perl -CSDA -w <<'EOF' - " & boundary's quoted form & "
use strict;
local $\\ = qq(\\n);
local $, = qq(,);

my $boundary = shift;
my @data = ();
my ($new, $complete, $i, $j) = (1, 0, -1, 0);
while (<DATA>) {
    next if ! ($new ||= $_ =~ /^$boundary$/) && $complete;
    if ( $new ) {
        ($new, $complete) = (0, 0);
        ++$i;
    }

    /^From:\\s*(.+?)\\s*$/o         && do { $data[$i]{from}     = $1; $j=0; next; };
    /^Gender:\\s*(.+?)\\s*$/o       && do { $data[$i]{gender}   = $1; $j=0; next; };
    /^Age:\\s*(.+?)\\s*$/o          && do { $data[$i]{age}      = $1; $j=0; next; };
    /^Mobile:\\s*(.+?)\\s*$/o       && do { $data[$i]{mobile}   = $1; $j=0; next; };
    /^Phone Day:\\s*(.+?)\\s*$/o    && do { $data[$i]{phoneday} = $1; $j=0; next; };
    /^Email:\\s*(.+?)\\s*$/o        && do { $data[$i]{email}    = $1; $j=0; next; };

    /^I would like to be kept up to date on future Eden events, competitions and special offers\\s*(.+?)\\s*$/o 
                                    && do { $data[$i]{eden}     = $1; next; };
    /^I would like to be kept up to date on future Kuoni events, competitions and special offers\\s*(.+?)\\s*$/o
                                    && do { $data[$i]{kuoni}    = $1; next; };

    /^Address:\\s*(.+?)\\s*$/o      && do { $data[$i]{address1} = $1; $j=1; next; };
    $j == 1 && /^\\s*(.+?)\\s*$/o   && do { $data[$i]{address2} = $1; $j++; next; };
    $j == 2 && /^\\s*(.+?)\\s*$/o   && do { $data[$i]{address3} = $1; $j++; next; };
    $j == 3 && /^\\s*(.+?)\\s*$/o   && do { $data[$i]{address4} = $1; $j++; next; };

    $complete = (0 + keys %{$data[$i]} == 12);
}

my @keys = ('from', 'gender', 'age', 'mobile', 'phoneday', 'address1', 'address2','address3','address4','email', 'eden', 'kuoni');
print map { s/\"/\"\"/og; qq(\"$_\") } @keys;

for (@data) {
    print map { $_ = '' unless defined $_; s/\"/\"\"/og; qq(\"$_\") } @{$_}{@keys};
}
__END__
" & _join(q, boundary) & "
EOF" without altering line endings
        end repeat

        -- combine data from each batch
        set r to my rr's item 1 -- include header
        repeat with i from 2 to count my rr -- exclude header for rest
            set r to r & my rr's item i's text from paragraph 2 to text -1
        end repeat
        return r
    end script
    tell o to run
end _retrieve_data

on _join(tt, d)
    (*
        list tt : source list
        string d : separator
        return string : tt joined with d
    *)
    local astid0, t
    try
        set {astid0, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {} & d}
        set t to "" & tt
        set AppleScript's text item delimiters to astid0
    on error errs number errn
        set AppleScript's text item delimiters to astid0
        error errs number errn
    end try
    return t
end _join
--END OF APPLESCRIPT

Final working code below for interest.感兴趣的最终工作代码如下。

 --APPLESCRIPT _main() on _main() set mm to {} tell application "Mail" repeat with m in (get selection) set mm's end to m's content & linefeed end repeat end tell set r to my _retrieve_data(mm) do shell script "printf '%s' " & r's quoted form & " > ~/desktop/$(date +list_%F_%H%M%S.csv)" return r end _main on _retrieve_data(mm) (* list mm : list of message text *) script o property pp : mm property qq : {} property rr : {} property boundary : do shell script "uuidgen" without altering line endings -- UUID & LF property batch : 50 -- number of messages to be processed at once; combined text should not exceed ca. 200K -- divide messages into batches repeat with i from 1 to count my pp by batch set j to i + batch - 1 if j > (count my pp) then set j to -1 set my qq's end to my pp's items i thru j end repeat -- retrieve data per batch repeat with q in my qq set my rr's end to do shell script "perl -CSDA -w <<'EOF' - " & boundary's quoted form & " use strict; local $\\\\ = qq(\\\\n); local $, = qq(,); my $boundary = shift; my @data = (); my ($new, $complete, $i, $j) = (1, 0, -1, 0); while (<DATA>) { next if ! ($new ||= $_ =~ /^$boundary$/) && $complete; if ( $new ) { ($new, $complete) = (0, 0); ++$i; } /^From:\\\\s*(.+?)\\\\s*$/o && do { $data[$i]{from} = $1; $j=0; next; }; /^Gender:\\\\s*(.+?)\\\\s*$/o && do { $data[$i]{gender} = $1; $j=0; next; }; /^Age:\\\\s*(.+?)\\\\s*$/o && do { $data[$i]{age} = $1; $j=0; next; }; /^Mobile:\\\\s*(.+?)\\\\s*$/o && do { $data[$i]{mobile} = $1; $j=0; next; }; /^Phone Day:\\\\s*(.+?)\\\\s*$/o && do { $data[$i]{phoneday} = $1; $j=0; next; }; /^Email:\\\\s*(.+?)\\\\s*$/o && do { $data[$i]{email} = $1; $j=0; next; }; /^Address:\\\\s*(.+?)\\\\s*$/o && do { $data[$i]{address1} = $1; $j=1; next; }; $j == 1 && /^\\\\s*(.+?)\\\\s*$/o && do { $data[$i]{address2} = $1; $j++; next; }; $j == 2 && /^\\\\s*(.+?)\\\\s*$/o && do { $data[$i]{address3} = $1; $j++; next; }; $j == 3 && /^\\\\s*(.+?)\\\\s*$/o && do { $data[$i]{address4} = $1; $j++; next; }; /^I would like to be kept up to date on future Eden events, competitions and special offers\\\\s*$/o && do { $data[$i]{eden} = '1'; $j=0; next; }; /^I would like to be kept up to date on future Kuoni events, competitions and special offers\\\\s*$/o && do { $data[$i]{kuoni} = '1'; $j=0; next; }; $complete = (0 + keys %{$data[$i]} == 12); } my @keys = ('from','gender','age','mobile','phoneday','email','address1','address2','address3','address4','eden','kuoni'); print map { s/\\"/\\"\\"/og; qq(\\"$_\\") } @keys; for (@data) { print map { $_ = '' unless defined $_; s/\\"/\\"\\"/og; qq(\\"$_\\") } @{$_}{@keys}; } __END__ " & _join(q, boundary) & " EOF" without altering line endings end repeat -- combine data from each batch set r to my rr's item 1 -- include header repeat with i from 2 to count my rr -- exclude header for rest set r to r & my rr's item i's text from paragraph 2 to text -1 end repeat return r end script tell o to run end _retrieve_data on _join(tt, d) (* list tt : source list string d : separator return string : tt joined with d *) local astid0, t try set {astid0, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {} & d} set t to "" & tt set AppleScript's text item delimiters to astid0 on error errs number errn set AppleScript's text item delimiters to astid0 error errs number errn end try return t end _join --END OF APPLESCRIPT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 applescript &amp; Mac Mail - 从邮件正文中提取内容到 Excel 电子表格 - 并非所有内容提取 - applescript & Mac Mail - extract content from mail body to an excel spreadsheet - not all content extracts 使用 AppleScript 阻止 Excel 消息“不受支持的内容” - Block the Excel message “content that is not suported” using AppleScript Applescript:无法将日期和邮件内容从Outlook复制到Excel - Applescript: Can't copy Date and Message Content from Outlook to Excel 结合使用AppleScript和Safari在邮件中发送内容 - Use applescript with safari to send content in mail 基于 Python 的 Outlook 自动化使用来自 Excel 文件的数据作为邮件正文并添加签名 - Python-based automation of Outlook using data from an Excel file for the mail body and adding a signature 如何使用Applescript从Excel工作表填充页面文件 - How to Populate Pages file from Excel Worksheet using Applescript 使用Excel VBA将pdf或Word附件添加到Outlook邮件 - Adding pdf or Word attachments to Outlook mail using Excel VBA 如何从电子邮件(Outlook)提取内容到Excel表? - How to extract content from an E-Mail (Outlook) to an excel table? 使用 Applescript 将 Excel 转换为 PDF(另存为 PDF) - Convert Excel to PDF using Applescript (save as PDF) 如何使用AppleScript遍历Excel工作表? - How to iterate through excel sheet using AppleScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM