Help with my applescript

I got this applescript from Copilot.

What I want to achieve is to automatically change the value of custom metadata “University” based any university names contained in the file name. Like if the file name has “New York University” then Add New York University to the metadata.

But, this script doesn’t work.

DevonThink 3 中创建一个智能规则并使用 Apple Script 来实现这个功能是可行的。以下是一种可能的方法来达到你的目标:

  1. 打开 DevonThink 3 并导航到你想要设置智能规则的数据库。
  2. 点击菜单栏中的 “工具”,然后选择 “智能规则”
  3. 点击 “添加智能规则” 按钮以创建一个新的规则。
  4. 在规则设置中,选择 “当” 条件为 “名称” 包含 “华北理工大学”“南京师范大学”“南通大学”
  5. “执行” 部分,选择 “Apple Script” 作为操作。
  6. 输入以下 Apple Script 代码来修改文件的自定义元数据 “学校”:
on performSmartRule(theRecords)
    tell application id "DNtp"
        repeat with theRecord in theRecords
            set recordName to name of theRecord
            if (recordName contains "华北理工大学") then
                set customMetadata of theRecord to {学校:"华北理工大学"}
            else if (recordName contains "南京师范大学") then
                set customMetadata of theRecord to {学校:"南京师范大学"}
            else if (recordName contains "南通大学") then
                set customMetadata of theRecord to {学校:"南通大学"}
            end if
        end repeat
    end tell
end performSmartRule

What does that mean, exactly? How do you employ the code?
And what has Copilot to say about that, if it wrote the code?
Hint: it’s hallucinating. Check out the resources available here and the scripting dictionary of DT.

I use the apply script in smart rules, and it runs the script, but none of the custom meta data get changed. I read many resources, and come up with an updated one. The script runs, but still, none of the custom metadata changed.

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in records
			set recordnam to name of theRecord
			if recordnam contains "华北理工大学" then
				get custom meta data for "地理"
				add custom meta data "华北理工大学" for "地理" to thisRecord
			else if recordnam contains "南京工业大学" then
				get custom meta data for "地理"
				add custom meta data "南京工业大学" for "地理" to thisRecord
			else if recordnam contains "南京师范大学" then
				get custom meta data for "地理"
				add custom meta data "南京师范大学" for "地理" to thisRecord
			end if
		end repeat
	end tell
end performSmartRule

All these instructions are actually unnecessary. In addition, sometimes thisRecord is used instead of theRecord.

Hi, cgrunenbery, I apologize for my mess.

I just start to use applescript.

I apply this rule to indexed files.

I don’t know what goes wrong, although there is no bug reporting, the script runs but the custom metadata did not change.

Best way is to have a look at both existing, similar scripts and DEVONthink’s script suite (e.g. by dropping the DEVONthink 3.app onto the Script Editor.app in the Dock/Finder). Copilot, ChatGPT & Co. rarely produce a result that is useful out of the box.

Did you fix the inconsistent usage of theRecord vs. thisRecord?

Yes, here’s the new script

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			set recordnam to name of theRecord
			if recordnam contains "华北理工大学" then
				add custom meta data "华北理工大学" for "地理" to theRecord
			else if recordnam contains "南京工业大学" then
				add custom meta data "南京工业大学" for "地理" to theRecord
			else if recordnam contains "南京师范大学" then
				add custom meta data "南京师范大学" for "地理" to theRecord
			else if recordnam contains "南通大学" then
				add custom meta data "南通大学" for "地理" to theRecord
			end if
		end repeat
	end tell
end performSmartRule

Does it have anything to do with all my files being indexed instead of imported?

No, this doesn’t matter. But the conditions of the rule might matter as a basic test of your last revision was successful over here.


Here is the screenshot of my setup

This rule is executed only on startup and every hour, maybe On Import/Creation/Renaming would make more sense to simply process new items instead of almost all items regularly? In addition, searching only for those items that contain the required strings in the name would also improve this.

OK, now I find the problem.

So the script add custom meta data is not mean to change the value of a meta data but instead add a new metadata with the same name. So, it will generate numerous metadata with the same name "地理“ in the database

The add custom meta data updates already existing meta data and running the rule multiple times doesn’t cause additional entries over here.

Also, it looks like this script works for a few files, but not all files, don’t know what’s going wrong. I change the condition, but still not always working. Hmm, strange

It looks like that’s not the case with my database, I’m thinking maybe it has something to do with Asian characters?

The script is using Chinese characters “地理” in the custom metadata identifier. That should be avoided. It’s not clear to me why you use “地理” (lit. geography) for a field purposed to record the name of a university.

That’s possible, e.g. that AppleScript is using a different encoding internally and the string is not found. Alternatively you could try a smart rule like this, scripting is not necessary at all:

Well, I kind follow the convention of LCSH. In LCSH, geographic is a subdivision type. Complex. I think I will change the identifier in my custom metadata to English and have another try.

I have sent you a private message.