Script to take selected number amount and add to "Price" metadata

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
1 Like

Well I tried it:

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:

85.00

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

It gave me a message that said simply, “subs”:

It’s unclear what you actually have selected.

  • What are you processing - PDFs?
  • Have you tried using the document amount parameter?

PS: selected text is a property of a think window or the text of a rich text file.

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

When I run that script from the script menu, nothing happens. When I run it from Script Editor, I get the “subs” error:

What is this “class subs,” anyway? Doesn’t it refer to a subroutine? Why is it appearing?

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

Does the slightly revised version work?

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)

This:

tell application id "DNtp"
	set theText to selected text as string
	display dialog theText as string
end tell

Returns this:

I moved “as string” to different lines, then both lines; and I got the same result. The text selected in the PDF document was “7,115.00.”

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)

This script worked:

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”:

And you’ll note, it wasn’t working for this person.