Getting date & time of video files

Hello

I wrote this script to gather the creation date and time of video files I am recording.

Unfortunately the name of my recorded video files are not unique. In order to make those name unique I appending to the video name file the creation date & time.

The purpose of the following appelscript is to gather the date and time (only keeping numbers) Appending the date, time to the video name fil together is done by another process.

filename 00001.mts expected result 00001.mts20190728033632
filename 00002.mts expected result 00002.mts20190728034022
filename 00003.mts expected result 00003.mts2019xxxxxxxxxx and getting 00003.mtsontentChangeDate=

The reason I am writing this topic, is the script down below is not reliable. Sometime I do get a date and a time and some time I get something like “ontentChangeDate=”

Would someone have an idea to make this to be more reliable.

Note: This script is being executed within filemaker as an applescript function.

THANKS!

set myvar to "mdls -name kMDItemFSContentChangeDate /Volumes/NX5U/PRIVATE/AVCHD/BDMV/STREAM/00001.MTS"
set ChangeDate to do shell script myvar
display dialog ("Change date: *" & ChangeDate & "*")
set fileinfor to characters -25 through -7 of ChangeDate as text
set oldTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
set fileinfor to words of fileinfor as string
set AppleScript's text item delimiters to oldTIDs
display dialog (fileinfor)

I did more research on the web and found the following. What do you think?

set shellCommand to "stat -f %B -t %s " & "/Volumes/NX5U/PRIVATE/AVCHD/BDMV/STREAM/00001.MTS"
set ChangeDate to do shell script shellCommand
display dialog (ChangeDate)

I would not use shell script but ASObjC.

----------------------------------------------------------------
use AppleScript version "2.5" -- El Capitan (10.11) or later
use framework "Foundation"
use scripting additions
----------------------------------------------------------------

property |NSURL| : a reference to current application's NSURL
property NSURLContentModificationDateKey : a reference to current application's NSURLContentModificationDateKey
property NSURLCreationDateKey : a reference to current application's NSURLCreationDateKey

#=====#=====#=====#=====#=====#=====

on creationAndModificationDate:POSIXPath
	set theURL to |NSURL|'s fileURLWithPath:POSIXPath
	set {theResult, creDate} to theURL's getResourceValue:(reference) forKey:NSURLCreationDateKey |error|:(missing value)
	log creDate as date (*date samedi 27 avril 2019 à  19:19:17*)
	set {theResult, modDate} to theURL's getResourceValue:(reference) forKey:NSURLContentModificationDateKey |error|:(missing value)
	log modDate as date (*date samedi 3 août 2019 à  15:04:44*)
	set theFormatter to current application's NSDateFormatter's new()
	theFormatter's setDateFormat:"yyyyMMddHHmmss"
	return ((theFormatter's stringFromDate:creDate) as string) & (theFormatter's stringFromDate:modDate) --as string
end creationAndModificationDate:

#=====#=====#=====#=====#=====#=====

set POSIXPath to POSIX path of (path to desktop as text) & "zarbi.numbers"
my creationAndModificationDate:POSIXPath
--> "2019042719191720190803150444"

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 18 octobre 2019 12:17:47

Thanks Yvan,

Being honest, I do not understand the script, very advance for me. I really appreciate your help.

Please note this script once working is to be loaded into a “Perform applescript” FileMaker Pro function.

I’ve executed the script as such :

set myVar to "/Volumes/NX5U/PRIVATE/AVCHD/BDMV/STREAM/00001.MTS"
set POSIXPath to POSIX path of myVar & "zarbi.numbers"
my creationAndModificationDate:POSIXPath
--> "2019042719191720190803150444"

and I got the following error message error “Can’t make missing value into type date.” number -1700 from missing value to date where the script stopped at the line

log create as date

Would be more logical to use:

set POSIXPath to "/Volumes/NX5U/PRIVATE/AVCHD/BDMV/STREAM/00001.MTS"
my creationAndModificationDate:POSIXPath
--> "2019042719191720190803150444"

I build my own POSIXPath because, as you may guess, there is no file whose posix path is “/Volumes/NX5U/PRIVATE/AVCHD/BDMV/STREAM/00001.MTS” on my machine.

Below is a version which doesn’t rely upon a hard coded file path and add some comments about what is done.

----------------------------------------------------------------
use AppleScript version "2.5" -- El Capitan (10.11) or later
use framework "Foundation"
use scripting additions
----------------------------------------------------------------

property |NSURL| : a reference to current application's NSURL
property NSURLContentModificationDateKey : a reference to current application's NSURLContentModificationDateKey
property NSURLCreationDateKey : a reference to current application's NSURLCreationDateKey

#=====#=====#=====#=====#=====#=====

on creationAndModificationDate:POSIXPath
	# Build an URL from the posix path
	set theURL to |NSURL|'s fileURLWithPath:POSIXPath
	# Ask the system for the resource creation date of the file
	set {theResult, creDate} to theURL's getResourceValue:(reference) forKey:NSURLCreationDateKey |error|:(missing value)
	log creDate as date (*date samedi 27 juillet 2019 à  10:34:57*)
	# Ask the system for the resource modification date of the file
	set {theResult, modDate} to theURL's getResourceValue:(reference) forKey:NSURLContentModificationDateKey |error|:(missing value)
	log modDate as date (*date mercredi 16 octobre 2019 à  16:33:27*)
	# Creates a formatter designed to return the dates using the wanted format
	set theFormatter to current application's NSDateFormatter's new()
	theFormatter's setDateFormat:"yyyyMMddHHmmss"
	# Concatenate the two formatted dates
	return ((theFormatter's stringFromDate:creDate) as string) & (theFormatter's stringFromDate:modDate) --as string
end creationAndModificationDate:

#=====#=====#=====#=====#=====#=====

-- set POSIXPath to "/Volumes/NX5U/PRIVATE/AVCHD/BDMV/STREAM/00001.MTS"
set POSIXPath to POSIX path of (choose file)
my creationAndModificationDate:POSIXPath
--> "2019072710345720191016163327"

If you really want to rely upon Spotlight values using shell you may use :

-- set POSIXPath to POSIX path of (choose file)
set POSIXPath to "/Volumes/NX5U/PRIVATE/AVCHD/BDMV/STREAM/00001.MTS"

# Required when the path may contain space characters which is the case on my machine.
set qPath to quoted form of POSIXPath

return my getDate(qPath, "kMDItemFSCreationDate") & my getDate(qPath, "kMDItemFSContentChangeDate")

#=====

on getDate(qPath, aKey)
	set myvar to "mdls -name " & aKey & " " & qPath
	set aDate to do shell script myvar
	log aDate (*kMDItemFSCreationDate = 2019-07-27 08:34:57 +0000*)
	(*kMDItemFSContentChangeDate = 2019-10-16 14:33:27 +0000*)
	set aDate to item 2 of my decoupe(aDate, {"= ", " +"})
	log aDate (*2019-07-27 08:34:57*)
	(*2019-10-16 14:33:27*)
	set aDate to my remplace(aDate, {"-", " ", ":"}, "")
	log aDate (*20190727083457*)
	(*20191016143327*)
	return aDate
end getDate

#=====

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

on remplace(t, d1, d2)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1}
	set l to text items of t
	set AppleScript's text item delimiters to d2
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end remplace

#=====

You have no guarantee that such values are available because Spotlight may haven’t indexed your file.
It’s why I choose to extract resources which are always available.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 18 octobre 2019 15:15:28

Hello Yvan,

I do want to use your recommended approach.

The following is the applescript function I am using to gather file birth date and time. This function is executed in a loop and myVar keeps changing.

The first line is to gather the path of the file for which the date and time is required.
The last line is to return the founded (returned) value.

Let’s say I have three files to verify I could have something like :
Loop1 “filetoverify” is equal to “/Volumes/NX5U/PRIVATE/AVCHD/BDMV/STREAM/00003.MTS”
Loop2 “filetoverify” is equal to"/Volumes/NX5U/PRIVATE/AVCHD/BDMV/STREAM/00004.MTS"
Loop3 “filetoverify” is equal to"/Volumes/NX5U/PRIVATE/AVCHD/BDMV/STREAM/00005.MTS"

I’ve tried to use choose file to select the file and it did work. However, this require a human intervention. Everything is to run in the background.

How do I replace the Choose file statement by a field containing the file path? I’ve tried

I need to have the returned value in a field called filedate.

THANKS AGAIN!

Before writing more I need an explanation.
Are you wanting to append the creationDateTime, the modification Date Time or both.
In your first message you wrote about both so I thought that you wanted to get both.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 18 octobre 2019 19:57:34

Sorry for the confusion!.

I need to make the name unique by attaching date and time. My preference is to have Creation (birth) date and time “yyyyMMddHHmmss”.

I’ve read somewhere Creation (birth) date and time is not always available for files. Is this thru?

Thanks again!

Maybe you want to get the video encoding time. This is probably not KMD metadata, but EXIF metadata of video file.

Hello KniazidisR

I posted a script using the system dates and one using the spotlight ones.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 18 octobre 2019 20:44:12

I don’t use FileMaker so I can’t test a script ruling it.

Your POSIX path seems to describe files stored on an external disk attached to your machine.
As far as I know, in such case the system creation date is available assuming of course that the file is already created.
On the other hand, I can’t guarantee that the metadata “kMDItemFSCreationDate” would be available because it’s created by Spotlight which has its own life.

Below is a script in which I tried to write code using FileMaker.

----------------------------------------------------------------
use AppleScript version "2.5" -- El Capitan (10.11) or later # REQUIRED
use framework "Foundation" # REQUIRED
use scripting additions # REQUIRED
----------------------------------------------------------------


# Here is what I suppose to be the calling code:

tell application "FileMaker"
tell current record
set myVar to cell "filetoverify"
--> a Posix path like : "/Volumes/NX5U/PRIVATE/AVCHD/BDMV/STREAM/00003.MTS"
set theDate to my grabTheDate(myVar)
set cell "Infile::CreatedTime" to theDate
end tell # to current record
end tell # to FileMaker

#=====#=====#=====#=====#=====#=====

on grabTheDate(POSIXPath)
	# Enable the instruction which fits your needs.
	set myDate to my getCreationDate:POSIXPath --> "20190727103457"
	--set myDate to my getDate(POSIXPath, "kMDItemFSCreationDate")  --> "20190727083457"
	--set myDate to do shell script "stat -f %B -t %s " & quoted form of POSIXPath  --> "1564216497"
	return myDate
end grabTheDate

#=====#=====#=====#=====#=====#=====

# Handler using ASObjC
on getCreationDate:POSIXPath
	# Build an URL from the posix path
	set theURL to current application's NSURL's fileURLWithPath:POSIXPath
	# Ask the system for the resource creation date of the file
	set {theResult, creDate} to theURL's getResourceValue:(reference) forKey:(current application's NSURLCreationDateKey) |error|:(missing value)
	# Creates a formatter designed to return the dates using the wanted format
	set theFormatter to current application's NSDateFormatter's new()
	theFormatter's setDateFormat:"yyyyMMddHHmmss"
	# Apply the defined format to the date
	return (theFormatter's stringFromDate:creDate) as string
end getCreationDate:

#=====#=====#=====#=====#=====#=====

# Handler using mdls. Requires the handlers decoupe and remplace
on getDate(POSIXPath, aKey)
	# aKey may be "kMDItemFSCreationDate" or "kMDItemFSContentChangeDate"
	set myvar to "mdls -name " & aKey & " " & quoted form of POSIXPath
	set aDate to do shell script myvar
	--log aDate (*kMDItemFSCreationDate = 2019-07-27 08:34:57 +0000*)
	set aDate to item 2 of my decoupe(aDate, {"= ", " +"})
	--log aDate (*2019-07-27 08:34:57*)
	set aDate to my remplace(aDate, {"-", " ", ":"}, "")
	--log aDate (*20190727083457*)
	return aDate
end getDate

#=====

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

on remplace(t, d1, d2)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1}
	set l to text items of t
	set AppleScript's text item delimiters to d2
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end remplace

#=====#=====#=====#=====#=====#=====

You must use it without changes.
As is, the handler grabTheDate(POSIXPath) use ASObjC.
You may enable an other one among the three given ones.

I assume that to treat an other file you must change the current record.

Maybe somebody accustomed to FileMaker would be able to help you about what is linked to this application.
On my side I can’t help more.

Of course, if I pass a POSIX path pointing upon a file unavailable on my machine, I get “missing value”.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 18 octobre 2019 21:47:58

Thanks Yvan.

Wow, this is GREATLY appreciated!