Renaming, change date format from custom metadata

I am using a custom metadata date field for some financial documents. I want to be able to use the date in a rename smart rule but I want the name to be in yyyymmdd not July 6 2021 as it is in the field.
Is there a way to do this? I am guessing AS or javascript.

I am guessing that you’re right. Getting from the month name to its numerical is the main problem here, I suppose.

The custom metadata field is set as a date format and not plain text so that info is in there somewhere I imagine.

set r to "July 4, 2021"

set {mm, dd, yyyy} to words of (short date string of (date r))

log (yyyy & mm & dd)
--> (*20210704*)

Yeah, that AppleScript is awful! :stuck_out_tongue: :wink:

Note: The order of the variables will depend on your localization. Here in the US, it’s commonly MM-DD-YYYY, hence the order of mine.

Not sure if this is self deprecating because of some script writers code of simplicity, or if it’s actually a hackjob way to do this. Either way I am trying to jam it into a script to concatenate three custom metadata fields into a new name string (that script will definitely be the second of the two options I presented) Wish me luck!

Neither and it’s certainly not a hackjob method. It’s clean and useful.
My comment was a light-hearted jab at @chrillek, who loves his JavaScript and stifles his retching when encountering AppleScript. :wink: :stuck_out_tongue:


The third possibility!


No idea why when that little bit of code works as needed. Unless you’re going for a complete JS solution.

I meant the third possibility of the interpretation of your…

as @chrillek was not directly referenced.

I am giving AS a go. @chrillek will have to assume I am doing a terrible job of it and it would be a snap in javascript but I don’t have to time to learn both at once.

This time, I was stifling a gasp of admiration. Seriously, this is quite elegant. I have no idea how it works across locales (eg parsing a British date in a German setting), but that’s an esoteric question anyway.

In JavaScript, I’d try

const date = new Date("July 4, 2021");
const str = `${date.getYear()}${date.getMonth()+1}${date.getDate()}`;

But that looks quite hackish and does not guarantee two digit month and day.


Sure. But if you consider learning a new language, you might want to consider what you can do with it. AS is Mac-only, and it is fairly improbable that it will ever come to iOS. On the other hand, there are quite a few apps now offering JS scripting on iOS and macOS.
And JS is the language of the web, so to speak, and it’s evolving. Something you can’t say about AS.

I think you’re right. In fact I am pretty sure you’re right.
What I suspect is happening in my brain right now is I am getting used to the idea of thinking programatically and the somewhat natural language simplicity of AS is helping me get there. If I can get my head around that I think I can get to javascript with a bit more effort.

I have some experience of java like scripting from AfterEffects but it’s always been a “someone has figured this out already and I don’t have time to debug a script that I can brute force if it comes down to the wire” so I’ve never taken it onboard in me head.

I am mildly excited that I have figured out (via research, and referring to the DT AS library in Script editor) how to applescript a rename process using custom metadata and it’s almost job ready. If I can do that and understand how it could be done in javascript I might try and do it both ways as a learning exercise.

In order to save the honor of JS

const dt = new Date("June 4 2021");
const str = dt.toISOString().replace(/T.*$/,"").replaceAll(/-/g,"");

Not really elegant, but at least not as clumsy as my first go at it.

1 Like

/bows and waves his hat in the air

When I ran that in Script Editor I got an undefined as a result. is there a date format preference that it relies on?

When I run this in Script Editor as is, I get

error "Can’t get item 2 of {\"20210704\"}." number -1728 from item 2 of {"20210704"}

Given that the script does not return a value, that’s to be expected. If you switch on the message part of script editor (click on the three horizontal lines in the bottom), you should see the output. Alternatively, add return str as the last line.

1 Like

After a lot of research and floundering around with date formats and settings on my computer I have arrived at this script.
It pulls two metadata fields (vendor, daterec), formats the date into yyyymmdd and concatenates them into a string separated by an underscore.
I couldn’t solve the above error message from @BLUEFROG’s script so I went hunting on stackexchange and found CJK’s addendum suggestion.

I await your scripting critiques and forehead slapping at my crude scratchings.

BTW my research also lead me to downloading Script Debugger after reading the millionth suggestion to do just that. It was definitely a help.

tell application id "DNtp"
	set theRecords to (selected records)
	repeat with thisRecord in theRecords
		set vendorname to get custom meta data for "vendor" from thisRecord
		set datestring to get custom meta data for "daterec" from thisRecord
		set [_day, _month, _year] to [day, month, year] of datestring
		set _month to _month * 1
		set _month to texts -1 thru -2 of ("0" & _month)
		set _day to texts -1 thru -2 of ("0" & _day)
		set datename to {_year, _month, _day} as string
		set transformedName to vendorname & "_" & datename
		set name of thisRecord to transformedName
	end repeat
end tell

One last question (famous last words):
I am trying to add an if statement to check if there’s an account number, then include it in the name.
(accountno is custom metadata, single line text.)
I can’t figure out how to test for an empty value.
The below results in
“VendorCo_missing value_20180212”

if accountno = {} then
			set transformedName to vendorname & "_" & datename
			set transformedName to vendorname & "_" & accountno & "_" & datename
		end if


if accountno is missing value

Warning: I have limited experience with AppleScript. As usual, make the change and try it on something harmless and disposable first!