Determining font of section of text in RTF document

Hi,

I am wondering if it is possible to determine the font of a section of text in an RTF document using applescript.

I have been using a script that splits up a DEVONthink RTF document by paragraph and then reads the font directly from the DEVONthink window, i.e.


set theText to text of window 1

repeat with j from 1 to (count paragraphs in theText)
	set ARfont to {font} of character 1 of paragraph j of text of window 1 as string
        .....
end repeat

but now I don’t want to divide up the document according to paragraph, but rather by delimiters, i.e.:

set theText to text of window 1

set {myTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {"*"}}
	set theSection to text items of theText
	set AppleScript's text item delimiters to myTID
	
	repeat with mySection in theSection
		set ARfont to {font} of character 1 of mySection of text of window 1 as string
                ......
        end repeat

the set ARfont won’t work now because the mySection doesn’t relate to window 1 in the way that paragraph j does. So I am wondering if there’s another way to read the font of mySection?

What’s your ultimate goal – what do you need the name of the font for? The answer to that might open other avenues.

I have an if/else if statement which applies different AppleScript to each section based on the font of the 1st character in the section.

Try something like this:

set theText to text of window 1

set {myTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {"*"}}
set theSection to text items of theText
set AppleScript's text item delimiters to myTID

set ARfont to {font} of character 1 of window 1 as string -- first section

set soFar to 1
set theMax to length of text of window 1
set delimLength to length of "*" -- whatever the delimiter is

repeat with mySection in theSection
	set soFar to soFar + delimLength + (length of mySection)
	if soFar < theMax then
		set ARfont to {font} of character soFar of text of window 1 as string
		...
	else
		exit repeat
	end if
end repeat

Thanks.

I am getting an error on this line:

set theMax to length of text of window 1

error “DEVONthink Pro got an error: Can’t get length of every text of window 1.” number -1728 from length of every text of window 1

Try getting the text first.

That seemed to help. It managed a couple of items, and then I got this:

DEVONthink Pro got an error: Can’t get character 869 of every text of window 1. Invalid index.

on this line: set ARfont to {font} of character soFar of text of window 1 as string

I’m afraid I don’t have DEVONthink Pro, so I’m not sure what’s causing that. Perhaps you need to get theMax differently.

I don’t own DevonThink but I tested Shane’s code upon TextEdit and Pages.

For TextEdit I used :

tell application "TextEdit" # ADDED
	
	set theText to text of document 1 # replaced window by document
	set theDelim to "" # ADDED
	set {myTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {theDelim}}
	set theSection to text items of theText
	set AppleScript's text item delimiters to myTID
	
	set ARfont to {font} of character 1 of document 1 as string -- first section # replaced window by document
	
	set soFar to 1
	set theMax to length of (get text of document 1)
	log "Point 01" # ADDED
	set delimLength to length of theDelim
	log "Point 02" # ADDED
	
	repeat with mySection in theSection
	log "Point 03,  " & mySection # ADDED
		set soFar to soFar + delimLength + (length of mySection)
		if soFar < theMax then
		log "Point 04" # ADDED
			set ARfont to {font} of character soFar of text of document 1 as string # replaced window by document
			log ARfont # ADDED
			--...
		else
			log "Point 04" # ADDED
			exit repeat
		end if
	end repeat
	
end tell # ADDED

And it behaved flawlessly.

For Pages I used a version in which I inserted some log instructions allowing you ( we too) to learn where the script is failing.

tell application "Pages" # ADDED
	set theText to body text of document 1 # replaced text of window by body text of document
end tell # ADDED

set theDelim to ""
set {myTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {theDelim}}
set theSection to text items of theText
set AppleScript's text item delimiters to myTID

tell application "Pages" # ADDED
	set ARfont to (font of character 1 of body text of document 1) as string -- first section # replaced text of window by body text of document
	
	set soFar to 1
	set theMax to length of (get body text of document 1) # replaced text of window by body text of document and added get
	log "Point 01" # ADDED
	set delimLength to length of theDelim -- whatever the delimiter is
	log "Point 02" # ADDED
	
	repeat with mySection in theSection
		log "Point 03, " & mySection # ADDED
		set soFar to soFar + delimLength + (length of mySection)
		if soFar < theMax then
			log "Point 04" # ADDED
			set ARfont to {font} of character soFar of body text of document 1 as string # replaced text of window by body text of document
			log ARfont # ADDED
			--...
		else
			log "Point 06" # ADDED
			exit repeat
		end if
	end repeat
	
end tell # ADDED

It behaved flawlessly too.

May you insert the log instructions in your code and tell us which is the log issued before the error message ?

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) mardi 14 février 2017 18:03:46

I have tried merging your script and my script Yvan, but can’t save it as I get an error message on trying to save it.

I am posting the whole script below in case you can see what the issue is:

tell application "TextEdit" # ADDED
	
	set theText to text of document 1 # replaced window by document
	set theDelim to "*" # ADDED
	set {myTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {theDelim}}
	set theSection to text items of theText
	set AppleScript's text item delimiters to myTID
	
	set ARfont to {font} of character 1 of document 1 as string -- first section # replaced window by document
	
	set soFar to 1
	set theMax to length of (get text of document 1)
	set delimLength to length of theDelim -- whatever the delimiter is
	
	repeat with mySection in theSection
		set soFar to soFar + delimLength + (length of mySection)
		log ARfont # ADDED
		set paraText to text of mySection
		if soFar < theMax then
				set ARfont to {font} of character soFar of text of document 1 as string # replaced window by document
				if ARfont is equal to "Optima-Bold" then
					set thisPart to paraText
					tell application "DEVONthink Pro"
					set x to create location "Notes/" & authorName & "/" & titleName & "/" & thisPart in database "research"
					end tell
				else if ARfont is equal to "Optima-BoldItalic" then
					set sectionNo to 1
					set thisChapter to paraText
					tell application "DEVONthink Pro"
					set x to create location "Notes/" & authorName & "/" & titleName & "/" & thisPart & "/" & thisChapter in database "research"
					end tell
				else if ARfont is equal to "Optima-Italic" and character 1 of paraText is not "*" then
					set subSectionNo to 1
					if thisPart is "" and thisChapter is "" then
						set thisSection to sectionNo & ". " & paraText
						tell application "DEVONthink Pro"
						set x to create location "Notes/" & authorName & "/" & titleName & "/" & thisSection in database "research"
						end tell
						set sectionNo to (sectionNo + 1)
					else
						set thisSection to sectionNo & ". " & paraText
						tell application "DEVONthink Pro"
						set x to create location "Notes/" & authorName & "/" & titleName & "/" & thisPart & "/" & thisChapter & "/" & thisSection in database "research"
						end tell
						set sectionNo to (sectionNo + 1)
					end if
				else if ARfont is equal to "Optima-Italic" and character 1 of paraText is "*" then
					if thisPart is "" and thisChapter is "" then
						set thisSubSection to subSectionNo & ". " & texts 2 thru -1 of paraText
						tell application "DEVONthink Pro"
						set x to create location "Notes/" & authorName & "/" & titleName & "/" & thisSection & "/" & thisSubSection in database "research"
						end tell
						set subSectionNo to (subSectionNo + 1)
					else
						set thisSubSection to subSectionNo & ". " & texts 2 thru -1 of paraText
						tell application "DEVONthink Pro"
						set x to create location "Notes/" & authorName & "/" & titleName & "/" & thisPart & "/" & thisChapter & "/" & thisSection & "/" & thisSubSection in database "research"
						end tell
						set subSectionNo to (subSectionNo + 1)
					end if
				else
					if (paraText contains "~") and (paraText contains "§") then
						set thisName to texts ((offset of "~" in paraText) + 1) thru ((offset of "§" in paraText) - 2) of paraText
					else if paraText contains "~" then
						set thisName to texts ((offset of "~" in paraText) + 1) thru -1 of paraText
					else
						set thisName to texts 1 thru ((offset of ":" in paraText) - 1) of paraText
					end if
					if paraText starts with "0" then
						set thisText to "<p class=\"quote\">" & texts ((offset of ":" in paraText) + 2) thru ((offset of "~" in paraText) - 2) of paraText & "</p><p class='last'>" & citationID & "@" & texts ((offset of "/" in paraText) + 1) thru ((offset of ":" in paraText) - 1) of paraText & "}" & "</p>"
					else
						set thisText to "<p class=\"quote\">" & texts ((offset of ":" in paraText) + 2) thru ((offset of "~" in paraText) - 2) of paraText & "</p><p class='last'>" & citationID & "@" & texts 1 thru ((offset of "/" in paraText) - 1) of paraText & "}" & "</p>"
					end if
					if paraText contains "§" then
						set thisTag to texts ((offset of "§" in paraText) + 1) thru -1 of paraText
					end if
					set thisPage to texts 1 thru ((offset of ":" in paraText) - 1) of paraText
					set searchText to texts ((offset of ":" in paraText) + 2) thru ((offset of "~" in paraText) - 2) of paraText
					tell application "DEVONthink Pro"
					set searchURL to ("x-devonthink-item://" & uuid of content record & "?search=" & searchText as string)
					end tell
					set strHTML to "
<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">
<html>
<head>
  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
  <meta http-equiv=\"Content-Style-Type\" content=\"text/css\">
  <title></title>
  <style type=\"text/css\">
" & pstrDefaultCSS & "
  </style>
</head>
<body>" & thisText & "</body>"
					tell application "DEVONthink Pro"
					set oHTML to create record with {type:html, source:strHTML, URL:searchURL, comment:thisPage, tags:thisTag} in x
					set oNewRec to convert record oHTML to rich
					delete record oHTML
					set name of oNewRec to thisName
					end tell
				end if
		else
			exit repeat
		end if
	end repeat
	
end tell # ADDED

You are mixing TextEdit and DevonThink.
You can’t do that.
Works only with DevonThink.
Please, open your document, run the script below and tell us where it is failing.

tell application "DEVONthink Pro"
set theText to text of window 1
set theDelim to ""
set {myTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {theDelim}}
set theSection to text items of theText
set AppleScript's text item delimiters to myTID

set ARfont to {font} of character 1 of window 1 as string -- first section

set soFar to 1
set theMax to length of text of window 1
	log "Point 01" # ADDED
set delimLength to length of theDelim 
	log "Point 02" # ADDED

repeat with mySection in theSection
	log "Point 03,  " & mySection  # ADDED
   set soFar to soFar + delimLength + (length of mySection)
   if soFar < theMax then
	log "Point 04" # ADDED
       set ARfont to {font} of character soFar of text of window 1 as string
	log ARfont # ADDED
       ...
   else
	log "Point 06" # ADDED
       exit repeat
   end if
end repeat
end tell

You are supposed to get an events log resembling to :

tell application "DEVONthink Pro"
	get body text of document 1
	get font of character 1 of body text of document 1
	get body text of document 1
	(*Point 01*)
	(*Point 02*)
	(*Point 03, .*)
	(*Point 04*)
	get font of character 1306 of body text of document 1
	(*TrebuchetMS-Bold*)
	(*Point 03, .*)
	(*Point 04*)
	get font of character 2114 of body text of document 1
	(*ArialMT*)
	(*Point 03, .*)
	(*Point 04*)
	get font of character 2700 of body text of document 1
	(*Times-Bold*)
	(*Point 03, .*)
	(*Point 06*)
end tell

But, maybe I understand wrongly the problem.
Must I understand that you try to extract blocks of a text open in TextEdit to insert them in a DEVONThink database ?
If it’s that try to run the script posted in a preceding message dedicated to TextEdit.
I added some log instructions.
Run it as is and tell us what it returns.

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) mardi 14 février 2017 19:59:46

I get an error on line 8 of the script:

error “Can’t make font of character 1 of window 1 of application "DEVONthink Pro" into type string.” number -1700 from font of character 1 of window 1 to string

OK

It seems that you didn’t see that I added some comments at the end of my message.

Here they are again :

But, maybe I understand wrongly the problem.
Must I understand that you try to extract blocks of a text open in TextEdit to insert them in a DEVONThink database ?
If it’s that try to run the script posted in a preceding message dedicated to TextEdit.
I added some log instructions.
Run it as is and tell us what it returns.

I just remembered that I downloaded “DEVONthink Pro” to help somebody recently.
I am unable to use it but at least, I may compile scripts supposed to drive it.
Looking in your attempt to apply my proposal I discovered several typos.
In your script search the string texts and replace every occurence (they are 12) by text (without the plural s).
At least, after that, the script will compile correctly which it doesn’t at this time.

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) mardi 14 février 2017 21:00:54

Your script use twice a variable named citationID which is never defined

It uses five times a variable named authorName which is never defined.

It uses five times a variable named titleName which is never defined.

After correcting these oddities I get the error :

Erreur dans DEVONthink Pro : Il est impossible d’obtenir database “research”.

I assume that «research» is the name of a DEVONthink document which you are using.

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) mardi 14 février 2017 21:23:43

Thanks Yvan. The script works fine when I just cycle through the paragraphs of the document, so I was hoping that it could work with dividing the document by text delimiters as well. Unfortunately I don’t have time to work on this more right now, so I think I will have to stick to the usual method of paragraphs for now.

The script that works extracts the paragraphs from a RTF file in DEVONthink, but I would actually prefer to build the separate DEVONthink files from a TextEdit document.

With Best Wishes,

Nick

Did you apply the corrections listed in message #14 and at the very end of message #13.

Don’t hurry, I switch off after sending this message.

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) mardi 14 février 2017 22:12:47