Script to create a custom data string as identifier

While doing other stuff, I wrote this script to get a custom data string as an identifier.

Demo

-- y-yr m-mon d-day h-hr n-min s-sec
tell application id "DNtp"
	set {a} to item 1 of {selection}
	tell application "System Events"
		set b to my getDateString(current date, "ymdhns", "-")
		--returns. "2020-03-07-08-41-22"
		set c to my getDateString(current date, "ymdh", ".")
		--returns "2020.03.07.08"
	end tell
	set d to my getDateString(modification date of a, "ymdhn", "-")
	--returns "2020-02-18-11-10"
	set e to my getDateString(modification date of a, "dmy", "-")
	--returns "18-02-2020"
end tell

The script needs to call three other handlers - I am used to calling my standard handlers to avoid re-writing the script each time. I am sure that it will be much easier to use shell script to do the same job. Perhaps somone will take the challenge…

The script (and the three handlers)

--ngan 2020.03.07

on getDateString(theDate, theFormat, theSeperator)
	tell application id "DNtp"
		local y, m, d, h, n, s, t
		local lol, ds
		set lol to {{"y", ""}, {"m", ""}, {"d", ""}, {"h", ""}, {"n", ""}, {"s", ""}}
		
		set (lol's item 1)'s item 2 to get year of theDate
		set (lol's item 2)'s item 2 to my PadNum((get month of theDate as integer) as string, 2)
		set (lol's item 3)'s item 2 to my PadNum((get day of theDate) as string, 2)
		set t to every word of (get time string of theDate)
		set (lol's item 4)'s item 2 to T's item 1
		set (lol's item 5)'s item 2 to T's item 2
		set (lol's item 6)'s item 2 to T's item 3
	end tell
	
	set ds to {}
	set theFormat to (every character of theFormat)
	repeat with each in theFormat
		set ds to ds & my lolLookup(each as string, 1, 2, lol)
	end repeat
	
	return my listToStr(ds, theSeperator)
	
end getDateString

on lolLookup(lookupVal, lookUpPos, getValPos, theList)
	--only for lol with 2 items or more
	local i, j, k
	set j to lookUpPos
	set k to getValPos
	repeat with i from 1 to length of theList
		if (item j of item i of theList) is equal to lookupVal then return {item k of item i of theList}
	end repeat
	return {}
end lolLookup

on PadNum(lngNum, lngDigits)
	-- Credit houthakker
	set strNum to lngNum as string
	set lngGap to (lngDigits - (length of strNum))
	repeat while lngGap > 0
		set strNum to "0" & strNum
		set lngGap to lngGap - 1
	end repeat
	strNum
end PadNum

on listToStr(theList, theDelimiter)
	set {tid, text item delimiters} to {text item delimiters, theDelimiter}
	set theStr to theList as text
	set text item delimiters to tid
	return theStr
end listToStr

Thanks for these – I do appreciate the effort you make to share, and there’s some interesting stuff in there which no doubt I’ll steal…

You mention using a shell script for the actual date function — it doesn’t do everything your script does, but perhaps it could be used as part of it?

set titleDateString to do shell script "date +%y%jt%H%M"

This uses the shell function `date’ and results in a string like 20067t1002. Broken down it’s

  • %y – 2 digit year
  • %j – 3 digit day of the year
  • t – to separate the time because DTTG sees 9 digit numbers as telephone numbers if you don’t break it up
  • %H and %M – 2 digit hours and minutes, of course.

This is for a unique timestamp, so I don’t need the century, and using the day of the year saves a digit over month + day. The overall timestamp is therefore 9 digits instead of 12, which makes it slightly less distracting.

The date function has lots of other parameters, for example:

  • %Y for 4-digit year
  • %a %A for Day of the week in words (Sat or Saturday)
  • %b %B for Month in words (Mar or March)
  • %F as YYYY-MM-DD (2020-03-07)
  • %T as HH:MM:SS (10:02:59)

and so on. There are loads of them… This page has some details: https://www.thegeekstuff.com/2013/05/date-command-examples/

HTH.

Thanks.

I tried to take a look at terminal and check out “man date”. The one issue I encounter is that it seems the shell script only manage system/current date string. But if I am to manipulate the DT’s internal date (modification, creation, etc), I am not sure how to do it.

This doesn’t work

	set {a} to item 1 of {selection}
	set b to (creation date of a) as string
	
	set c to do shell script b & " +%y%jt%H%M"

-- nor this

	set c to do shell script "\"" & b & " +%y%jt%H%M"

Don’t know sorry… For me the date/time of invoking the script and the ‘creation date of the record’ are the same thing, near enough, so I don’t need the extra complexity.

It looks like a combination of the -j and -f flags may do what you want (convert one format to another, but I haven’t played with it to find the exact invocation.

Thanks. I’ll use what I have and refining them in the future …

Yes, of course! I only mentioned the shell script because you wondered about it. I’ll have a look at the date function at some point and let you know if I find a way…