I made some changes and added several instructions writing a log file on the desktop.
This version does its duty flawlessly.
Problem : if I remove the instructions writing the log it doesn't enter the handler speaking to Numbers and I don't understand why.
# Edited on 2017/06/03
property theSpreadSheet : missing value
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell me to set theUserPath to "" & (path to home folder)
set shortSpreadSheet to "Documents:AppRevenue.numbers" # EDITED, was "Documents:MyNumbers.numbers"
set my theSpreadSheet to theUserPath & shortSpreadSheet
if theUserPath ends with "koenig:" then
set targetWord to "Foncez"
else
set targetWord to "whatever"
end if
tell me to set path2log to "" & (path to desktop) & "log_gol.txt"
my writeto(path2log, (linefeed & (current date) & linefeed), «class utf8», false) #Init the log
my writeto(path2log, "count of theMessages = " & (count theMessages) & linefeed, «class utf8», true)
repeat with i from (count theMessages) to 1 by -1
set this_item to item i of theMessages
tell application "Mail"
set mb to name of mailbox of this_item
my writeto(path2log, "i = " & i & ", mb = " & mb & linefeed, «class utf8», true)
if mb = "INBOX" then
my writeto(path2log, "I'm in the test" & linefeed, «class utf8», true)
set theTxt to this_item's content
my writeto(path2log, "theText is grabbed" & linefeed, «class utf8», true)
my writeto(path2log, (targetWord) & linefeed, «class utf8», true)
--ignoring case
if (theTxt) contains (targetWord) then
my writeto(path2log, "I'm in the test" & linefeed, «class utf8», true)
-- do some stuff here
--
--
set a to random number
set b to random number
set c to random number
set d to random number
set e to random number
my writeto(path2log, "a, b, c, d, e = " & a & ", " & b & ", " & c & ", " & d & ", " & e & linefeed, «class utf8», true)
tell me to say "ready to call the handler"
my letsDoNumbers(a, b, c, d, e, path2log)
end if
--end ignoring
end if
set read status of this_item to true
-- deselect the message here?
end tell
end repeat
end perform mail action with messages
end using terms from
on letsDoNumbers(p, q, r, s, t, path2log)
tell me to say "I am in the handler"
tell application "Numbers"
activate
open alias (my theSpreadSheet)
tell me to delay 0.5
my writeto(path2log, "doc name = " & name of document 1 & linefeed, «class utf8», true)
tell document "AppRevenue.numbers"
set this_sheet to (first sheet whose name is "A Sheet")
my writeto(path2log, "this_sheet = " & (name of this_sheet) & linefeed, «class utf8», true)
tell this_sheet
set _aTable to (first table whose name is "Table")
my writeto(path2log, "_aTable = " & (name of _aTable) & linefeed, «class utf8», true)
tell _aTable
set theCol to 4
set theRow to 5
set theRng to cell theCol of row theRow
my writeto(path2log, "count rows = " & (count rows & linefeed), «class utf8», true)
add row above theRng
my writeto(path2log, "count rows = " & (count rows & linefeed), «class utf8», true)
set theDataToBeAdded to {p, q, r, s, t}
set theIterator to 1
repeat with i from 1 to 5
set this_item to item i of theDataToBeAdded
set theRng's value to this_item
set theCol to theCol + theIterator
set theRng to cell theCol of row theRow
end repeat
end tell
end tell
end tell
end tell
end letsDoNumbers
#=====
(*
*)
on writeto(targetFile, theData, dataType, apendData)
-- targetFile is the path to the file you want to write
-- theData is the data you want in the file.
-- dataType is the data type of theData and it can be text, list, record etc.
-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
try
set targetFile to targetFile as «class furl»
set openFile to open for access targetFile with write permission
if not apendData then set eof of openFile to 0
write theData to openFile starting at eof as dataType
close access openFile
return true
on error
try
close access targetFile
end try
return false
end try
end writeto
#=====