In my words: You extract an amount from a PDF document using a smart rule action. Then you assign this string to a custom meta data field named “Notes(REC)”. Then you run the script that
- saves the current custom meta data of the record to
customMD
(why)?
- converts the
mdCompany
field of customMD
to a number (why?)
- takes this “value” to set the custom meta data field
amount
- finally clears the custom meta data field
notesrec
First, in order to debug this, you should trim down the script to the absolute necessary and run it in Script Editor (or Script Debugger, which has a free edition) when a sample record is selected. So something like
tell application id "DNtp"
repeat with theRecord in selected records
set customMD to custom meta data of theRecord
add custom meta data mdcompany of customMD as number for "amount" to theRecord
add custom meta data "" for "notesrec" to theRecord
end repeat
end tell
Then you can see which Apple Events are sent and what the answers look like. Which helps to understand what’s going wrong. And is one step in systematically finding and solving problems.
Which here gives me:
Interestingly … because I set (manually) the “Company” field of this record to the amount. However, you don’t do such a thing: You set the field Notes(REC) (note the parenthesis) in your smart rule. But in your script, you retrieve the value of mdcompany (i.e. the pre-defined meta data field “Company”), convert that to a number (which might give 0 or some other useless value) and then add this value to your custom meta data field “amount”. Which then is probably 0 or empty or some other useless value.
Finally, you set the custom meta data field “notesrec” to the empty string – although in your rule, you used “Notes(REC)”, which has quite a different spelling.
To summarize: In your rule, store the amount in a custom meta data field (e.g. notesrec
). Use the very same field (e.g. notesrec
) in your script like so
set myAmount to get custom metadata for "notesrec" from theRecord
(yes, there’s even a method for that, no need to get the custom meta data record first and then index into it). And then use this to set your amount
field like so
add custom meta data myAmount as number for "amount" to theRecord
and finally clear the notesrec
field like you did before
Disclaimer: I do not think that this is a very good method to achieve what you want. It works, but it mostly works around limitations of the smart rule system. Instead, I’d write a script that does everything: Extract the amount and set the custom meta data (in fact, I did that for my purposes). That also avoids introducing a completely useless custom meta data field. However, the document amount
field seems to be utterly useless for this purpose (at least in the one case where I test it). So your script would have to find the amount it is interested in by itself.