Bulk rename files

I’d like to bulk rename files using the following convention. I’d like to be able to append as many tags as there are attached to a file, ideally in alphabetical order.

Is there a way to do this without AppleScript? (Unfortunately AS is beyond my abilities)

[File Creation Year][File Creation Month][File Creation Date][TAG1][TAG2]_[TAG3].extension

would return

2021_11_07 - HOME - NEW YORK - LEASE.pdf

or

2017_01_17 - HEALTH - LABS.pdf

etc etc

Is this something I might be able to do with the batch rename regex function? If so, could someone kindly explain how?

Thank you in advance!

The dates could be done with placeholders. However, the tags can’t be processed and presented that way without some scripting. And no, RegEx wouldn’t work in this case.

  1. Unzip the attached file.
  2. In the Finder, press Command-Shift-G and paste: ~/Library/Application Scripts/com.devon-technologies.think3.
  3. Drag and drop the AppleScript from Step 1 into the Smart Rules folder in this window.
  4. You can access the script via an Execute Script > External in a smart rule or Tools > Batch Process.

Rename with Creation Date and Tags.scpt.zip (7.7 KB)

And the underlying code…

on run
	tell application id "DNtp"
		if selection = {} then return
		my performSmartRule(selected records)
	end tell
end run

on performSmartRule(theRecords)
	set od to AppleScript's text item delimiters -- Cache the default text item delimiter
	
	tell application id "DNtp"
		repeat with theRecord in theRecords
			set recordTags to (tags of theRecord) -- Cache the tags
			set cd to creation date of theRecord -- Get the creation date
			-- Assign variables to the date components
			set yyyy to year of cd
			set mm to (month of cd) as integer
			set dd to day of cd
			
			set dateString to {yyyy, (my zeropad(mm as integer)), my zeropad(dd)} -- Order the date components in a list
			set AppleScript's text item delimiters to "_" -- Assign the date delimiter
			set dateString to dateString as string -- Process the list as a string
			set AppleScript's text item delimiters to "-" -- Switch to a delimiter for the tags
			set newName to {dateString, recordTags} as string -- Concatenate the date and tags with the delimiter
			set AppleScript's text item delimiters to od -- Reset the text item delimiter
			
			set name of theRecord to newName -- Apply the new name
		end repeat
	end tell
end performSmartRule

on zeropad(num) -- Add prefixing zero (from @cgrunenberg)
	return (characters -2 thru -1 of ("0" & num as string)) as string
end zeropad

Jim – just so I understand, you’re saying that alphabetization of tags would be difficult even with AppleScript, correct?

Either way, thank you so so much. I just installed this and it works like a charm.

You’re welcome. Glad it’s working as you hoped.

you’re saying that alphabetization of tags would be difficult even with AppleScript, correct?

Ahh, I didn’t even notice that.

There isn’t a built-in AppleScript sort command, but it’s possible to accomplish.

1 Like

OK, good to know!

Last question: how do I edit this so that all delimiters are simply underscores? I tried editing the Applescript itself but it doesn’t seem to affect the end result at all.

Just remove this line…

set AppleScript's text item delimiters to "-" -- Switch to a delimiter for the tags

I tried that but it doesn’t work, unfortunately! The batch rename seems to perform exactly the same as the previous script. (And yes, I’ve saved the script as a new name and I’m positive it’s running the right one.)

Could it be cached, perhaps?

Did you quit and relaunch DEVONthink?

Yes, I’ve tried that to no avail.

Are you using this in a smart rule, batch process, or standalone?

I’ve tried batch process > execute script as well as a standalone.

I’m seeing no issue after removing that line…

image

And relaunching DEVONthink would reload the script.

Select a file or two, run the script, and post a screencap of what happened in the script editor.

Sorry, how can I see what’s happening in the script editor while I’m running the script?

Video screenshare attached. I feel like I’m going crazy!

Reopen the amended script.
Select Script > Compile. Does it compile?

It does!

And saves after compiling, verifying the line is gone?

If so, try this one…
Rename with Creation Date and Tags_underscores.scpt.zip (7.7 KB)

Ah! That one worked! I have no idea why. I even changed the delimiter to a dollar sign in the old script and it was still working as before:

Glad to hear it. Not sure what your specific issue was right now.