Hey there,
I am trying to create an AppleScript for Devonthink that does 2 things. Maybe someone can help.
- Get the subject line from the selected doc
- Get the creation date from the doc
and then rename the filename to Subject_Month/YYYY
the thing with the creation date works but how to extract the subject line or any information from the Doc I don’t get it to work.
What I have tried so far
tell application id "DNtp"
try
-- Check if any files are selected
if not (exists selection) then error "No files selected."
-- Loop through each selected record
repeat with thisRecord in (selection as list)
-- Get the creation date of the file
set creationDate to (creation date of thisRecord)
-- Extract the month and year
set thisMonth to texts -2 thru -1 of ("0" & (month of creationDate as number))
set thisYear to year of creationDate
-- Initialize the subject
set theSubject to ""
-- Extract the subject based on the type of document
if type of thisRecord is in {"txt", "markdown", "rtf", "html", "pdf", "docx"} then
try
-- For text-based documents, extract the first line as the subject
set theSubject to paragraph 1 of (plain text of thisRecord)
on error
set theSubject to "Untitled"
end try
else if kind of thisRecord is "PDF document" then
try
-- For PDFs, try to extract text from the first page
set theSubject to paragraph 1 of (plain text of thisRecord)
on error
set theSubject to "Untitled"
end try
else
-- For unsupported types, use the current name minus the extension as the subject
set theSubject to name of thisRecord
end if
-- Clean up the subject to remove any unwanted characters
set theSubject to do shell script "echo " & quoted form of theSubject & " | tr -d ':*/?\\<>|\"'"
-- Construct the new filename: "Subject_MM/YYYY"
set newFilename to theSubject & "_" & thisMonth & "/" & thisYear
-- Rename the record
set name of thisRecord to newFilename
end repeat
on error error_message number error_number
display alert "DEVONthink" message error_message as warning
end try
end tell