Get item creation date

Is there a script to get created date for an item? Preferably in YYYY-MM-DD format.

Is a script actually required? For certain tasks smart rules or batch processing and placeholders might be an option too.

Sure. But in my case, I’m trying to use it as a field while linking the items externally.

I’m trying to use it as a field while linking the items externally.

Can you clarify this?

Of course. I need to refer to the email and the date on which it was sent. The emails are archived in DT. I can generate a markdown link to refer/link to them in Obsidian. I also need to add the date, for which I am looking for a script to use.

So what is the data you’re passing to Obsidian supposed to look llike?

YYYY-MM-DD is my date format in Obsidian

But you said you’re passing a Markdown link. So is the link just a date, e.g., [2021-12-07](x-devonthink-item://…) ??

They are separate. The markdown contains the note title (email subject in my case) and the item link. The date will be used to link to the daily note which is [[YYYY-MM-DD]]

As an example, [Email subject] (x-devonthink://…) informed on [[2021-12-07]] that …

Here’s a little teaching edition snippet…

tell application id "DNtp"
	set sel to selected records -- Get the selection
	if (sel ≠ {}) or (sel = 1) then -- Only continue if there is a selection and it's only one item
		set {m, d, y} to (words of (short date string of (get creation date of (item 1 of sel))))
		-- Modify the variables per your locale.
		-- Mine is month-day-year
		
		-- Rearrange the variables however you want to create the desired string, e.g.,…
		set newDateString to (y & m & d) as string
		--> 20121207
	else
		display alert "Please select a single file to process."
	end if
end tell
2 Likes

Thank you, Jim. Very useful for my learning too :slight_smile:

1 Like

So sel = 1 is something like „count of items in sel = 1“ (in my words), meaning that AppleScript automatically considers the length of a list if you compare it to an integer?

Also, I think the „or“ should be an „and“: if the list is not empty, the „or“ always results in true, so the sel = 1 wil would be only relevant if the list were not empty…?

I think your logic is correct (of course: it would be! :grinning:) but neither the original formulation nor yours work correctly for me. I’m still messing around to see if there is some simple way of doing this!

Stephen

OK - I think I have something that works. sel is a list class. I think the following works:

tell application id "DNtp"
	set sel to selected records -- Get the selection
	
	if (sel ≠ {}) and ((count of sel) = 1) then -- Only continue if there is a selection and it's only one item
		set {m, d, y} to (words of (short date string of (get creation date of (item 1 of sel))))
		-- Modify the variables per your locale.
		-- Mine is month-day-year
		
		-- Rearrange the variables however you want to create the desired string, e.g.,…
		set newDateString to (y & m & d) as string
		--> 20121207
	else
		display alert "Please select a single file to process."
	end if
end tell

Edit: Of course, you can dispense with (sel ≠ {}) and in that case - leaving, if ((count of sel) = 1) then.

Stephen

That looks more like it :wink: I was wondering about the sel = 1 in the original – that looked too weird even for AppleScript to me.