# Renaming, change date format from custom metadata

**URL:** https://discourse.devontechnologies.com/t/renaming-change-date-format-from-custom-metadata/65958
**Category:** DEVONthink
**Created:** [August 2, 2021, 6:21pm UTC](https://discourse.devontechnologies.com/t/renaming-change-date-format-from-custom-metadata/65958 "2021-08-02T18:21:55Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![SlickSlack](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@SlickSlack](https://discourse.devontechnologies.com/u/SlickSlack)
#### Post date: [August 2, 2021, 6:21pm UTC](https://discourse.devontechnologies.com/t/renaming-change-date-format-from-custom-metadata/65958/1 "2021-08-02T18:21:55Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)
#### Post date: [August 2, 2021, 6:31pm UTC](https://discourse.devontechnologies.com/t/renaming-change-date-format-from-custom-metadata/65958/2 "2021-08-02T18:31:14Z")

</div>

> [@SlickSlack](#):
>
> 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.

---

<div class="post-metadata">

### Author: ![SlickSlack](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@SlickSlack](https://discourse.devontechnologies.com/u/SlickSlack)
#### Post date: [August 2, 2021, 6:35pm UTC](https://discourse.devontechnologies.com/t/renaming-change-date-format-from-custom-metadata/65958/3 "2021-08-02T18:35:26Z")

</div>

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

---

<div class="post-metadata">

### Author: ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)
#### Post date: [August 2, 2021, 6:39pm UTC](https://discourse.devontechnologies.com/t/renaming-change-date-format-from-custom-metadata/65958/4 "2021-08-02T18:39:14Z")

</div>

```auto
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! 😛 😉

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.

---

<div class="post-metadata">

### Author: ![SlickSlack](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@SlickSlack](https://discourse.devontechnologies.com/u/SlickSlack)
#### Post date: [August 3, 2021, 4:00pm UTC](https://discourse.devontechnologies.com/t/renaming-change-date-format-from-custom-metadata/65958/5 "2021-08-03T16:00:25Z")

</div>

> [@BLUEFROG](#):
>
> Yeah, that AppleScript is awful! 😛 😉

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!

---

<div class="post-metadata">

### Author: ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)
#### Post date: [August 3, 2021, 4:24pm UTC](https://discourse.devontechnologies.com/t/renaming-change-date-format-from-custom-metadata/65958/6 "2021-08-03T16:24:24Z")

</div>

> [@SlickSlack](#):
>
> 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.

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. 😉 😛

---

<div class="post-metadata">

### Author: ![SlickSlack](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@SlickSlack](https://discourse.devontechnologies.com/u/SlickSlack)
#### Post date: [August 3, 2021, 4:25pm UTC](https://discourse.devontechnologies.com/t/renaming-change-date-format-from-custom-metadata/65958/7 "2021-08-03T16:25:52Z")

</div>

> [@BLUEFROG](#):
>
> My comment was a light-hearted jab at @chrillek, who loves his JavaScript and stifles his retching when encountering AppleScript. 😉 😛

The third possibility!

---

<div class="post-metadata">

### Author: ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)
#### Post date: [August 3, 2021, 4:30pm UTC](https://discourse.devontechnologies.com/t/renaming-change-date-format-from-custom-metadata/65958/8 "2021-08-03T16:30:13Z")

</div>

JavaScript?

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

---

<div class="post-metadata">

### Author: ![SlickSlack](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@SlickSlack](https://discourse.devontechnologies.com/u/SlickSlack)
#### Post date: [August 3, 2021, 4:34pm UTC](https://discourse.devontechnologies.com/t/renaming-change-date-format-from-custom-metadata/65958/9 "2021-08-03T16:34:55Z")

</div>

I meant the third possibility of the interpretation of your…

> [@BLUEFROG](#):
>
> Yeah, that AppleScript is awful! 😛 😉

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.

---

<div class="post-metadata">

### Author: ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)
#### Post date: [August 3, 2021, 5:02pm UTC](https://discourse.devontechnologies.com/t/renaming-change-date-format-from-custom-metadata/65958/10 "2021-08-03T17:02:48Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

### Author: ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)
#### Post date: [August 3, 2021, 5:07pm UTC](https://discourse.devontechnologies.com/t/renaming-change-date-format-from-custom-metadata/65958/11 "2021-08-03T17:07:46Z")

</div>

> [@SlickSlack](#):
>
> but I don’t have to time to learn both at once.

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.

---

<div class="post-metadata">

### Author: ![SlickSlack](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@SlickSlack](https://discourse.devontechnologies.com/u/SlickSlack)
#### Post date: [August 3, 2021, 5:16pm UTC](https://discourse.devontechnologies.com/t/renaming-change-date-format-from-custom-metadata/65958/12 "2021-08-03T17:16:15Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)
#### Post date: [August 3, 2021, 5:26pm UTC](https://discourse.devontechnologies.com/t/renaming-change-date-format-from-custom-metadata/65958/13 "2021-08-03T17:26:55Z")

</div>

In order to save the honor of JS

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

```

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

---

<div class="post-metadata">

### Author: ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)
#### Post date: [August 3, 2021, 5:30pm UTC](https://discourse.devontechnologies.com/t/renaming-change-date-format-from-custom-metadata/65958/14 "2021-08-03T17:30:34Z")

</div>

/bows and waves his hat in the air  
🙂

---

<div class="post-metadata">

### Author: ![SlickSlack](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@SlickSlack](https://discourse.devontechnologies.com/u/SlickSlack)
#### Post date: [August 3, 2021, 5:34pm UTC](https://discourse.devontechnologies.com/t/renaming-change-date-format-from-custom-metadata/65958/15 "2021-08-03T17:34:11Z")

</div>

> [@chrillek](#):
>
> ```auto
> const dt = new Date("June 4 2021");
> const str = dt.toISOString().replace(/T.*$/,"").replaceAll(/-/g,"");
> console.log(str)
> 
> ```

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

---

<div class="post-metadata">

### Author: ![SlickSlack](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@SlickSlack](https://discourse.devontechnologies.com/u/SlickSlack)
#### Post date: [August 3, 2021, 5:36pm UTC](https://discourse.devontechnologies.com/t/renaming-change-date-format-from-custom-metadata/65958/16 "2021-08-03T17:36:05Z")

</div>

> [@BLUEFROG](#):
>
> ```auto
> set r to "July 4, 2021"
> 
> set {mm, dd, yyyy} to words of (short date string of (date r))
> 
> log (yyyy & mm & dd)
> --> (*20210704*)
> 
> ```

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

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

```

---

<div class="post-metadata">

### Author: ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)
#### Post date: [August 3, 2021, 7:26pm UTC](https://discourse.devontechnologies.com/t/renaming-change-date-format-from-custom-metadata/65958/17 "2021-08-03T19:26:13Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![SlickSlack](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@SlickSlack](https://discourse.devontechnologies.com/u/SlickSlack)
#### Post date: [August 3, 2021, 8:31pm UTC](https://discourse.devontechnologies.com/t/renaming-change-date-format-from-custom-metadata/65958/18 "2021-08-03T20:31:43Z")

</div>

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.](https://apple.stackexchange.com/questions/320253/applescript-date-format-and-calculation)

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.

```auto
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

```

---

<div class="post-metadata">

### Author: ![SlickSlack](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@SlickSlack](https://discourse.devontechnologies.com/u/SlickSlack)
#### Post date: [August 3, 2021, 9:37pm UTC](https://discourse.devontechnologies.com/t/renaming-change-date-format-from-custom-metadata/65958/19 "2021-08-03T21:37:45Z")

</div>

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”

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

```

---

<div class="post-metadata">

### Author: ![Stephen\_C](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/stephen_c/32/8107_2.png) [@Stephen\_C](https://discourse.devontechnologies.com/u/Stephen_C)
#### Post date: [August 4, 2021, 5:15am UTC](https://discourse.devontechnologies.com/t/renaming-change-date-format-from-custom-metadata/65958/20 "2021-08-04T05:15:14Z")

</div>

Try:

```auto
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!

Stephen

[Next page](https://discourse.devontechnologies.com/t/renaming-change-date-format-from-custom-metadata/65958.md?page=2)
