Scripting - Rename file

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
1 Like

I just run your script and it renamed my file to name_month/year.

So it did exactly what you want.

BTW, the names of your variables are a bit misleading. For example the name ‘thisMonth’ should probably be called something like ‘itemMonth’ or ‘documentMonth’.

My gut is telling me that this script was not created by a human being…

Anyway, one issue I can tell is that you are attempting to compare type of theRecord, which is not text, with some text. This will always return false, which is probably not what you want.

You would like to avoid having try blocks in an untested script, because they can hinder the debugging process when something isn’t quite right. Maybe if you say this to ChatGPT it would comply…

1 Like

Isn’t type of record what the scripting dictionary describes as

type (“bookmark”/‌"feed"/‌"formatted note"/‌"group"/‌"html"/‌"markdown"/‌"PDF document"/‌"picture"/‌"plist"/‌"quicktime"/‌"rtf"/‌"rtfd"/‌"script"/‌"sheet"/‌"smart group"/‌"txt"/‌"unknown"/‌"webarchive"/‌"xml", r/o) : The type of a record. Note: In compiled menu/toolbar scripts you might have to use a string representation of the type for comparisons.

That looks very much like text to me. But there are obviously other problems with this script. One being that a PDF document need not contain a plain text property – that is only meaningful for OCRd PDFs. Then setting theSubject to “Untitled” in every branch instead of doing that at the top once – an indicator that this might be an A"I" written script.

Also, the in condition uses "pdf", which is never true, while the if … else refers to "PDF document".

That’s JavaScript, it’s not a string in case of AppleScript.

Ah, of course. In AS, it’s kind of an enumeration. So @meowky is right, the comparison will never be true.

1 Like

thanks yes part of if was generated by AI the part with reading the content or subject of the selected pdf and extract it as a filename which is totally unaware of how to do it.

Nobody else have to try to achieve this? are there already any working scripts for that?

There are a bunch of scripts floating around in the forum to rename files. And you know already one of the problems with your script.

I suggest investing the time to learn a scripting language instead of taking a broken shortcut, since I don’t feel inclined to correct A"I" generated borken code just so these programs can “learn” from my efforts.

2 Likes

ok thanks I have figured it out

I didn’t know about the placeholder feature in smart rules. That fixed it very quickly and easy…

Sorry but I am still very new to Devonthink and AppleScript. I can do Bash scripting and some python but not AppleScript

In case you’re not aware: You can also us JavaScript to script DT. A bit close to Python.

One more thing to consider when renaming: name of the record may be different from the filename if it contains symbols, not supported by the filesystem. For subsequent filesystem operations, like shell commands, you should use resulting filename or path property of the record.

2 Likes