I am trying to use a script to take a selected number in a record item and transfer the amount to the meta data item “Price.” I know that the Assign Document Date & Amount.scpt does this pretty well, but I wanted to be able to add this quickly for the documents that this script passed up.
Having learned that I need to “coerce” the text into a number, from reading the extremely interesting post here:
I copied the coercion script and saved it as “CoercetoNumber.scpt”:
on tid(theVariable, theDelimiter)
set d to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDelimiter
if class of theVariable = text then
set transformedVariable to text items of theVariable
else if class of theVariable = list then
set transformedVariable to theVariable as text
end if
set AppleScript's text item delimiters to d -- always set them back
return transformedVariable
end tid
And I wrote the following script to add the coerced number to the Price metadata of the selected document:
tell application id "DNtp"
if (selected records) = {} then return
repeat with theRecord in (selected records) -- Process the selected record(s)
set theText to selected text
set myScript to script "CoercetoNumber"
CoercetoNumber's tid(theText)
get theText
add custom meta data theText for "Price" to theRecord
get custom meta data for "Price" from theRecord
end repeat
end tell
It does not work. It did not work before I added the coercion, and now that I have added the call to CoerceToNumber, it says:
error “DEVONthink 3 got an error: Can’t get script "CoercetoNumber".” number -1728 from script “CoercetoNumber”
I really thought that this would be simple. I select some text, and copy it to a blank field. I can do it manually, but automation is a lot of my goal with DVTPro.
You don’t need to go through all that rigamarole to coerce a string to a number.
As an example…
set r to "12"
try -- You can use a try block in case the text contains non-numeric characters
r as real -- This coerces the string to a decimal number
on error msg
display alert msg
end try
tell application id "DNtp"
if (selected records) = {} then return
repeat with theRecord in (selected records) -- Process the selected record(s)
set theText to selected text
set r to theText
try -- You can use a try block in case the text contains non-numeric characters
r as real -- This coerces the string to a decimal number
on error msg
display alert msg
end try
add custom meta data r for "Price" to theRecord
get custom meta data for "Price" from theRecord
end repeat
end tell
It inserted the number 1,937,072,755.00 into the Price field, and gave the error message:
Can’t make «class subs» into type real.
Just to make sure that the selected text was not gibberish, I copied and pasted it:
Now I wanted to see if there was a problem with just the way I was defining the text. I changed it to:
tell application id "DNtp"
if (selected records) = {} then return
repeat with theRecord in (selected records) -- Process the selected record(s)
set theText to selected text
display dialog theText
end repeat
end tell
This just coerces the value but doesn’t assign the result to the same or another variable. Therefore this should work:
tell application id "DNtp"
-- Processing the record having the text selection is sufficient
set theRecord to content record
set theText to (selected text as string)
set thePrice to (theText as real)
add custom meta data thePrice for "Price" to theRecord
end tell
You’re trying to coerce the text into a number, hence the error. And «subs» is a class for the selected text in DEVONthink.
And again…
What are you processing - PDFs?
Have you tried using the document amount parameter?
tell application id "DNtp"
if not (exists content record) then return
set theRecord to content record
set thePrice to document amount of theRecord
if (character 1 of thePrice) is in {"$", "¥", "£", "€"} then set thePrice to (characters 2 thru -1 of thePrice as string) -- You can't coerce a currency symbol, so it's removed if found.
add custom meta data thePrice for "Price" to theRecord -- However, you can pass a string to the custom metadata without coercing it to a real
end tell
It does work! The selected text is copied to Price!
I do not understand it, though.
First, I have never heard of “document amount.” I do not know how or where or when it is created, nor how it operates with selecting text.
Second, why did my super-simple script not work? I.e.,
tell application id "DNtp"
if (selected records) = {} then return
repeat with theRecord in (selected records) -- Process the selected record(s)
set theText to selected text
display dialog theText
end repeat
end tell
There is no coercion. All it says is, “Display selected text.” Why the “subs” error? What would get this to work (just for my own edification)?
selected text is not a string (e.g. might be rich text and even editable depending on the document) but display dialog expects a string. Therefore you have to use (theText as string)
There’s another issue, selected text is a property of windows and tabs, not one of the application. See AppleScript suite of DEVONthink (e.g. after dropping the app onto the Finder/Dock icon of the Script Editor.app)
tell application id "DNtp"
set theRecord to content record
try
set theSelectedText to selected text of think window 1 & "" as string
on error
error "Please select some text"
end try
add custom meta data theSelectedText for "Price" to theRecord
end tell
I got it from this thread on “Get the currently selected text”: