Devonthink/Tagging.sript
2025-05-23 15:46:17 +07:00

176 lines
5.6 KiB
Plaintext

tell application id "DNtp"
try
set selectedRecords to selection
if (count of selectedRecords) = 0 then
display dialog "Please select one or more files first" buttons {"OK"} default button 1
return
end if
set taggedCount to 0
set errorFiles to {}
repeat with theRecord in selectedRecords
try
-- Try multiple methods to get content
set recordContent to ""
try
set recordContent to content of theRecord
on error
try
set recordContent to plain text of theRecord
on error
try
set recordContent to rich text of theRecord
end try
end try
end try
if recordContent is "" then
error "Could not read content"
end if
-- Check for Hoàng Thị Quỳnh Hoa
if (recordContent contains "Hoàng Thị Quỳnh Hoa") then
if not (tags of theRecord contains "PB.Hoa") then
set tags of theRecord to tags of theRecord & "PB.Hoa"
set taggedCount to taggedCount + 1
end if
end if
-- Check for Đặng Thu Thủy
if (recordContent contains "Đặng Thu Thủy") then
if not (tags of theRecord contains "PB.Thuỷ") then
set tags of theRecord to tags of theRecord & "PB.Thuỷ"
set taggedCount to taggedCount + 1
end if
end if
-- Check for Định Quang Dân
if (recordContent contains "Đinh Quang Dân") then
if not (tags of theRecord contains "PB.Dân") then
set tags of theRecord to tags of theRecord & "PB.Dân"
set taggedCount to taggedCount + 1
end if
end if
-- Check for Lê Hải Hà
if (recordContent contains "Lê Hải Hà") then
if not (tags of theRecord contains "TB.Hà") then
set tags of theRecord to tags of theRecord & "TB.Hà"
set taggedCount to taggedCount + 1
end if
end if
-- Check for Phạm Minh Thư
if (recordContent contains "Phạm Minh Thư") then
if not (tags of theRecord contains "PB.Thư") then
set tags of theRecord to tags of theRecord & "PB.Thư"
set taggedCount to taggedCount + 1
end if
end if
-- Check for Chu Ngọc Quý
if (recordContent contains "Chu Ngọc Quý") then
if not (tags of theRecord contains "PB.Quý") then
set tags of theRecord to tags of theRecord & "PB.Quý"
set taggedCount to taggedCount + 1
end if
end if
-- Check for THƯ CÔNG TÁC (strictly case sensitive)
considering case
if (recordContent contains "THƯ CÔNG TÁC") then
if not (tags of theRecord contains "TCT") then
set tags of theRecord to tags of theRecord & "TCT"
set taggedCount to taggedCount + 1
end if
end if
end considering
-- Check for TỜ TRÌNH (strictly case sensitive)
considering case
if (recordContent contains "TỜ TRÌNH") then
if not (tags of theRecord contains "TTr") then
set tags of theRecord to tags of theRecord & "TTr"
set taggedCount to taggedCount + 1
end if
end if
end considering
-- Check for GIẤY MỜI (strictly case sensitive)
considering case
if (recordContent contains "GIẤY MỜI") then
if not (tags of theRecord contains "GM") then
set tags of theRecord to tags of theRecord & "GM"
set taggedCount to taggedCount + 1
end if
end if
end considering
-- Check for PHIẾU YÊU CẦU (strictly case sensitive)
considering case
if (recordContent contains "PHIẾU YÊU CẦU") then
if not (tags of theRecord contains "PYC") then
set tags of theRecord to tags of theRecord & "PYC"
set taggedCount to taggedCount + 1
end if
end if
end considering
-- Check for NGHỊ QUYẾT (strictly case sensitive)
considering case
if (recordContent contains "NGHỊ QUYẾT") then
if not (tags of theRecord contains "NQ") then
set tags of theRecord to tags of theRecord & "NQ"
set taggedCount to taggedCount + 1
end if
end if
end considering
-- Check for THÔNG BÁO (strictly case sensitive)
considering case
if (recordContent contains "THÔNG BÁO") then
if not (tags of theRecord contains "TB") then
set tags of theRecord to tags of theRecord & "TB"
set taggedCount to taggedCount + 1
end if
end if
end considering
-- Check for BÁO CÁO (strictly case sensitive)
considering case
if (recordContent contains "BÁO CÁO") then
if not (tags of theRecord contains "BC") then
set tags of theRecord to tags of theRecord & "BC"
set taggedCount to taggedCount + 1
end if
end if
end considering
-- Check for PHIẾU GIAO VIỆC (strictly case sensitive)
considering case
if (recordContent contains "PHIẾU GIAO VIỆC") then
if not (tags of theRecord contains "PGV") then
set tags of theRecord to tags of theRecord & "PGV"
set taggedCount to taggedCount + 1
end if
end if
end considering
on error errText
-- Add the problematic file to our list with error details
set errorFiles to errorFiles & ((name of theRecord) & " (Error: " & errText & ")" & return)
end try
end repeat
-- Show results
if (count of errorFiles) > 0 then
display dialog "Added " & taggedCount & " tags." & return & return & "The following files could not be read:" & return & errorFiles buttons {"OK"} default button 1
else
display notification "Successfully added " & taggedCount & " tags to all selected files."
end if
on error errMsg
display dialog "Error: " & errMsg buttons {"OK"} default button 1
end try
end tell