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 来实现这个功能是可行的。以下是一种可能的方法来达到你的目标:
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
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?
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
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.
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
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
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.