Renaming a file using Applescript

Perhaps not as obvious as it seems.

I have files shared from DEVONthink into Tinderbox; DEVONthink does the fine thing of indexing them in a directory, so I can use any editor to change them, and they propagate back into Tinderbox courtesy of having a DT URL.

I have another script which I occasionally use, which sends the text from Tinderbox into the corresponding DEVONthink note, which magically writes it to disk. It all works very pleasantly.

Sometimes, I have to change the name of a note (‘file’) in Tinderbox. It would be great to have this replicated on disk, but the obvious answer of just deleting it and creating a new one is not my preferred choice as it breaks version histories and the complex mapping between both systems.

I see in the Applescript dictionary that the filename property of a record is ‘get’ only. However, I know that the filenames are all in a specific directory, and won’t conflict as much as items imported to DEVONthink.

With this in mind - is there any sane way to rename filenames? If I click them in the DEVONthink interface, I can rename them in lists, and metadata is preserved; is there any way I can get to this functionality programmatically? (with all the caveats about edge cases, such as renaming a file using the name an existing file has, or other idiocy human users such as myself might introduce)

However, I know that the filenames are all in a specific directory, and won’t conflict as much as items imported to DEVONthink.

Actually, the reverse is true. Items in DEVONthink are much less likely to have name collisions.

With this in mind - is there any sane way to rename filenames?

Umm… name ? :wink:
A very simple example, including no error trapping, etc.…

tell application id "DNtp" to set the name of (selected record 1) to "PDF about a watch"
  • When used on an indexed document, the name would have a number appended to the name in case of a filename collision.

  • You can’t change filetypes this way, so renaming a .png file DavesGreatestPic.jpg, it would yield DavesGreatestPic.jpg.png.

With a little massaging into my terrible code, this worked perfectly and was much more straightforward than I expected. Thanks, Jim!

/me sheepishly realises he is now officially dimmer than AI

You’re welcome :slight_smile:

?
What are you referring to here, re: AI? My reply certainly had nothing to do with AI.

Thanks Heaven !!! - my day to day relies on this.

1 Like

Not at all; more that such a straightforward bit of code would have been thrown out easily by all manner of models, whereas I’m frozen worrying about all the complexities. At least I’m still sharper than Quora.

Actually, some AI-generated code is pretty poor when it comes to AppleScript.

Anyone interested in a simple, clean renaming snippet that would work on indexed documents as well ?

As usual a good model and a good prompt can make a huge difference. E.g. I used Claude 4.1 Opus and the complete initial post in DEVONthink’s chat assistant and the first suggestion was this:

The second suggestion was nonsense though :wink: