Smart rule step to change RTF font

I’ve set up a smart rule which works on any imported plain text note that has a certain naming convention. It tags it, converts it to an RTF, then deletes the imported plain text version.

It work fine, but I was wondering if there might be a way of shoehorning in another step (an embedded AppleScript, I’m guessing), which changes the font of all the text in the new RTF file.

Formatting wouldn’t matter, since the original file was plain text anyway. It’s just the font change.

Also, before I do that, I need to delete the first 3 characters of the filename as well :grimacing:
I know the latter might be as simple as:
set FileName to ((characters 4 thru -1 of theText) as string)
but I have no idea how to reference the record/file name FileName in the rule.

Since the big boys are not around (Pete is probably having his well deserved sleep) for the time being you have to make do with me.

And that’s my suggestion:

on performSmartRule(theRecords)
tell application id "DNtp"
  repeat with theRecord in theRecords
    set properties of text of theRecord to {font:"Please Not Arial", size:12}
    set theName to name of theRecord
    set theName to (characters 4 thru -1 of theName) as string
    set the name of the theRecord to theName
  end repeat
end tell
end performSmartRule

And by the way I would recommend you not to use an embedded script but an external one so you can use it in other rules to. Just open the Scripts Folder and save it to the Smart Rules sub-folder.

In the end I got the renaming to work by the use of actions in the smart rule.

First:

Scan Name - String - “ij-*” (I wanted to chop off the string “ij-” from the beginning)

then:

Change Name to * (the placeholder ‘Document String’)

But the AppleScript to change the font didn’t work. I took out the renaming bits and embedded:

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			set properties of text of theRecord to {font:"Iowan", size:18}
		end repeat
	end tell
end performSmartRule

Maybe that was wrong. Well, obviously it was, since the log showed “Can’t set every text of content id 103816 of database id 1 to {font:"Iowan", size:18}.

The little script works for me so I have tried to recreate your log entry.

And I was successful—I got the same log entry when the script was applied to not a rich text but a plain text. So my guess is that the order of steps in your smart rule is not correct and it tries to change the font and font size of the original plain text file. Which does not work of course.

To find out if I am right you could create another Smart Rule that contains only the font changing script and see if it works isolatedly.

According to the documentation, there is no text property (unless I’m overlooking something). There’s a method get text of which returns text of an HTML page. Maybe you should work on the rich text property of theRecord?

Quoting from the Text Suite:

text n [inh. item; see also Extended Text Suite] : Rich (styled) text

elements
contains attachments, attribute runs, characters, paragraphs, words.

properties
color (color) : The color of the first character.
font (text) : The name of the font of the first character.
size (integer) : The size in points of the first character.

I was actually referring to DT, where I did not find a text property in the record object. But you’re right, of course: The code works. I’m just wondering why one can work on text of theRecord if according to the documentation theRecord has no property text. But so be it.

I was wondering too when I tried to make the script work for the first time. I wondered even more that the same script does not work when I used rich text instead of text in that script.

Interesting - and thank you for looking at this.

I tried a smart rule with just the font-changing step on an RTF file and it worked no problem.

I tried applying it to a plain text file and, like you, got the same log error.

By all this logic, and looking at the order of the steps in my smart rule, it should work, since the ‘convert to rich text’ step comes before the script-step to change the font.

But the order of these smart rules is often counter-intuitive (at least to me) and I often have to use trial-and-error on the ordering. Having said that, I can’t find an order that will allow the font-change step to work.

The order of the actions I have is:

  1. Move to [a subgroup in Inbox]
  2. Scan Name - String - ij-*
  3. Change Name to * (the placeholder ‘Document String’)
  4. Convert to Rich Text
  5. Execute Script - Embedded (the script as above)
  6. Delete

Everything works except the font-change step. It’s as if, as you suggest, it’s trying that operation on plain text.

I see what is wrong: I have to use Convert & Continue (to Rich Text) instead of just Convert, so the rule will apply the subsequent actions to the copy, not to the original.

But then I have to remove the Delete step - otherwise my final file will be the one that’s deleted.

But then I have the plain text version and the RTF version!

The only way (I know) to get around that is to script the whole process, rather than doing part in the rule and part in the script.

Well, maybe you’re right. But I made a sort of workaround by re-ordering the rule steps to:

  1. Scan Name - String - ij-*
  2. Change Name to * (the placeholder ‘Document String’)
  3. Move To Trash
  4. Convert & Continue to Rich Text
  5. Execute Script - Embedded (the font-changing script as above)
  6. Move to [a subgroup in Inbox]

That way I have just the finished file in the target group, though the original is still in the trash.

It’s all a bit of a palaver. Maybe your “one script” approach would be better.

1 Like

At this time, this is the method we’d suggest as there is only one input at a time in smart rules.