0
我已經通過並使用了一些很棒的代碼形式來使用Applescript將郵件內容從郵件添加到Excel。我快到了,但還有一個問題。使用Applescript將郵件內容添加到Excel中
我有一個電子郵件
From: xxxxxx
Gender: xxxxxx
Age: >18 - 24
Phone Day: xxxxxx
Mobile:
Address: xxxxx
xxxxx
xxxx
xxxx
通知每一行都有一個介紹(例如,「來自:」)的下面的佈局,該內容正被正確地拉動。我的問題是添加「地址:」行的第2,3和4行,因爲它沒有介紹。
有什麼想法?
這是我迄今使用過的。
任何幫助感激地收到。
--APPLESCRIPT
_main()
on _main()
\t set mm to {}
\t tell application "Mail"
\t \t repeat with m in (get selection)
\t \t \t set mm's end to m's content & linefeed
\t \t end repeat
\t end tell
\t set r to my _retrieve_data(mm)
\t do shell script "printf '%s' " & r's quoted form & " > ~/desktop/$(date +scores_%F_%H%M%S.csv)"
\t return r
end _main
on _retrieve_data(mm)
\t (*
list mm : list of message text
*)
\t script o
\t \t property pp : mm
\t \t property qq : {}
\t \t property rr : {}
\t \t property boundary : do shell script "uuidgen" without altering line endings -- UUID & LF
\t \t property batch : 50 -- number of messages to be processed at once; combined text should not exceed ca. 200K
\t \t
\t \t -- divide messages into batches
\t \t repeat with i from 1 to count my pp by batch
\t \t \t set j to i + batch - 1
\t \t \t if j > (count my pp) then set j to -1
\t \t \t set my qq's end to my pp's items i thru j
\t \t end repeat
\t \t
\t \t -- retrieve data per batch
\t \t repeat with q in my qq
\t \t \t 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;
}
\t /^From:\\s*(.+?)\\s*$/o && do { $data[$i]{from} = $1; next; };
\t /^Gender:\\s*(.+?)\\s*$/o && do { $data[$i]{gender} = $1; next; };
\t /^Age:\\s*(.+?)\\s*$/o && do { $data[$i]{age} = $1; next; };
\t /^Mobile:\\s*(.+?)\\s*$/o && do { $data[$i]{mobile} = $1; next; };
\t /^Phone Day:\\s*(.+?)\\s*$/o && do { $data[$i]{phoneday} = $1; next; };
\t /^Address:\\s*(.+?)\\s*$/o && do { $data[$i]{address1} = $1; next; character id(200);};
\t /^Address:\\s*(.+?)\\s*$/o && do { $data[$i]{address2} = $1; next; };
\t /^Address:\\s*(.+?)\\s*$/o && do { $data[$i]{address3} = $1; next; };
\t /^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
\t \t end repeat
\t \t
\t \t -- combine data from each batch
\t \t set r to my rr's item 1 -- include header
\t \t repeat with i from 2 to count my rr -- exclude header for rest
\t \t \t set r to r & my rr's item i's text from paragraph 2 to text -1
\t \t end repeat
\t \t return r
\t end script
\t tell o to run
end _retrieve_data
on _join(tt, d)
\t (*
list tt : source list
string d : separator
return string : tt joined with d
*)
\t local astid0, t
\t try
\t \t set {astid0, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {} & d}
\t \t set t to "" & tt
\t \t set AppleScript's text item delimiters to astid0
\t on error errs number errn
\t \t set AppleScript's text item delimiters to astid0
\t \t error errs number errn
\t end try
\t return t
end _join
--END OF APPLESCRIPT
如果任何人有興趣,下面是非常使用的代碼。
--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