You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript for matching two files creation date

Hello,


I reopen here a question that I posted on another forum (macrumors):


I have converted almost 1,000 video files from MPG1 to MOV in order importing them to Final Cut Pro (FCP can't import directly MPG1!).


So I have TWO folders:

Folder 1 with the original VideoXXXX.mpg1 files

Folder 2 the converted VideoXXXX.mov files.


The file names on both folders are the same. And the number of files in the two folders are the same.


My problem is that the creation date of files in the Folder 2 is the "conversion" time (July 2012) instead the original creation time (from 2005 to 2010).


When imported to FCP, I need the creation time. The name doesn't include the time, so I don't know the file date looking only at the file name.


I need an Apple Script for changing the creation date to the files in Folder 2 files matching the creation date on the Folder 1 files.


- For file n on Folder 1, take name and creation date

- Look for the same file name on Folder 2

- Set creation date of file on Folder 2 to Creation date of original file


I have seen that it can be made with the "touch" command, but I'm new to applescripting and any help would be very appreciated!!


Thanks

iMac, OS X Mountain Lion

Posted on Aug 15, 2012 4:57 AM

Reply
Question marked as Top-ranking reply

Posted on Aug 15, 2012 11:39 AM

My two cents…


In the very special case where “the file names on both folders are the same and the number of files in the two folders are the same”, the following script should do exactly what you are asking for:


set folder1 to POSIX file "/POSIX/path/to/MPG1/folder"

set folder2 to POSIX file "/POSIX/path/to/MOV/folder"


tell application "Finder"

set folder1 to folderfolder1

set folder2 to folderfolder2

set N to (count folder1)

if not (count folder2) = N then return

set creationDates to creation date of files of folder1

set modificationDates to modification date of files of folder1

repeat with k from 1 to N

my changeDates(filek of folder2, itemk of creationDates, itemk of modificationDates)

end repeat

end tell


on changeDates(theFile, theCreationDate, theModificationDate)

tell application "Finder"

set modification date of theFile to theCreationDate

set modification date of theFile to theModificationDate

end tell

end changeDates

21 replies
Question marked as Top-ranking reply

Aug 15, 2012 11:39 AM in response to Fierabras

My two cents…


In the very special case where “the file names on both folders are the same and the number of files in the two folders are the same”, the following script should do exactly what you are asking for:


set folder1 to POSIX file "/POSIX/path/to/MPG1/folder"

set folder2 to POSIX file "/POSIX/path/to/MOV/folder"


tell application "Finder"

set folder1 to folderfolder1

set folder2 to folderfolder2

set N to (count folder1)

if not (count folder2) = N then return

set creationDates to creation date of files of folder1

set modificationDates to modification date of files of folder1

repeat with k from 1 to N

my changeDates(filek of folder2, itemk of creationDates, itemk of modificationDates)

end repeat

end tell


on changeDates(theFile, theCreationDate, theModificationDate)

tell application "Finder"

set modification date of theFile to theCreationDate

set modification date of theFile to theModificationDate

end tell

end changeDates

Aug 15, 2012 11:43 AM in response to Camelot

Camelot wrote:


set originalDateString to short date string of originalFileCreationDate & space & time string of originalFileCreationDate

set revisedDateStr to (do shell script " date -j -f \"%D %I:%M:%S %p\" " & quoted form of originalDateString & " \"+%Y%m%d%H%M.%S\"")


No, no, no.


The ironic thing about this, Camelot, is that I cribbed that routine from one of your own posts on this thread. that'll teach me to be lazy. 😝😉

Aug 15, 2012 7:23 AM in response to Fierabras

You can change the creation date of a file backwards by using the unix touch command to change the modification date (the system won't allow a file to have a modification date that's earlier than its creation date). the basic procedure you'd use would be to take a pair of related files, extract the creation date from the first and and touch the second with that date.


You didn't specify how the two lists of files are related, so it's hard to determine what the best way of pairing related files is. But assuming they are just stored in the file system in an arbitrary order you'd need to loop through the originals folder and search for the associated revised file for each file. That would look like this (where originalFilesFolder and revisedFilesFolder are POSIX paths to the folders where the original and revised files live):


tell application "System Events"

set originalFilePaths to POSIX path of (every file of folder originalFilesFolder whose name extension is "MPG1")

end tell


repeat with theOriginalFile in originalFilePaths

tell application "System Events"


-- get creation date of original file

set originalFileCreationDate to creation date of filetheOriginalFile


-- strip off mpg1 extension

set strippedFileName to text 1 through -5 of (name of theOriginalFile)


-- get associated mov file

set theRevisedFile to item 1 of (POSIX path of (every file of folder revisedFilesFolder whose name is (strippedFileName & "mov")))

end tell



-- reformat the creation date into a time slug for the unix touch command

set originalDateString to short date string of originalFileCreationDate & space & time string of originalFileCreationDate

set revisedDateStr to (do shell script " date -j -f \"%D %I:%M:%S %p\" " & quoted form of originalDateString & " \"+%Y%m%d%H%M.%S\"")



-- use touch to reset the modification date (which forces the creation date to change as well)

do shell script "touch -t " & revisedDateStr & space & quoted form of theRevisedFile

end repeat

Aug 15, 2012 8:40 AM in response to twtwtw

twtwtw wrote:


You can change the creation date of a file backwards by using the unix touch command to change the modification date (the system won't allow a file to have a modification date that's earlier than its creation date).


You can also change the creation date of a file backwards as follows:


set theFile to POSIX file "/POSIX/path/to/a/file"

tell application "Finder"

set temporary to modification date of file theFile

set modification date of file theFile to date "January 24, 1984"

set modification date of file theFile to temporary

end tell

Aug 15, 2012 8:46 AM in response to twtwtw

Thank you for your response.


I have an error here:


-- strip off

set strippedFileName to text 1 through -5 of (name of theOriginalFile)


I have checked the script with two folders I have just created on the Desktop (Folder1 and Folder2), and putting inside two files por trying.


I added two lines at the beginnig of the script:


set originalFilesFolder to "Macintosh HD:users:fhuet:Desktop:Folder1"

set revisedFilesFolder to "Macintosh HD:users:fhuet:Desktop:Folder2"


And I get that error: (Sorry, is in Spanish...XD):


tell application "System Events"

get POSIX path of every file of folder "Macintosh HD:users:fhuet:Desktop:Folder1" whose name extension = "MPG1"

--> {"/Users/fhuet/Desktop/Folder1/IMG_0299.MPG1", "/Users/fhuet/Desktop/Folder1/IMG_0300.MPG1"}

get creation date of file "/Users/fhuet/Desktop/Folder1/IMG_0299.MPG1"

--> date "viernes 13 de julio de 2012 20:34:11"

Resultado:

error "No puede obtenerse name of \"/Users/fhuet/Desktop/Folder1/IMG_0299.MPG1\"." number -1728 from name of "/Users/fhuet/Desktop/Folder1/IMG_0299.MPG1"


"No puede obtenerse" means "Can not get...". It seems it tries to sustrate "number -1728".

Or may be the

(name of theOriginalFile) uses the name of the file plus the whole path, instead of only just the name.


Anyway, it's a great help!!


I'll try to use the (offset of ".") instruction for isolating the name.


One more thing to clarify: The two folders (not the example floders, but the really ones with the video files) are in the same external HD volume, in the same folder main folder. Like in tha example above.


Thanks again





-- strip off m

Aug 15, 2012 9:32 AM in response to Fierabras

Hi,


Fierabras wrote:


Thank you for your response.


I have an error here:

ferror "No puede obtenerse name of \"/Users/fhuet/Desktop/Folder1/IMG_0299.MPG1\"." number -1728 from name of "/Users/fhuet/Desktop/Folder1/IMG_0299.MPG1"

Because theOriginalFile variable is a posix path, use ---> (name of file theOriginalFile)



Here is a script that use another method, try it:

--------------------------------------

set mpg1Folder to quoted form of POSIX path of (choose folder with prompt "Select folder wich contains .mpg1 files")

set movFolder to quoted form of POSIX path of (choose folder with prompt "Select folder wich contains .mov files")


do shell script "cd " & mpg1Folder & " && for f in *.MPG1;do /usr/bin/perl -e 'utime($ARGV[0], $ARGV[0], \"$ARGV[1]\")' $(/usr/bin/stat -f %B \"$f\") " & movFolder & "/\"${f%.MPG1}.mov\"; done"

Aug 15, 2012 9:57 AM in response to Fierabras

As Jacques says, a POSIX path is just a text string, so you have to tell system events that it's a file by adding the file keyword. i.e.:


(nameof filetheOriginalFile)


if you want to use the offset method, make sure you take the offset of the reversed string; otherwise if there's another dot in the name (e.g. 'file.name.something.MPG1') you'll get the wrong result. use


reverse of (characters of (name of file theOriginalFile)) as text


since it's always going to be extension MPG1, though, it's probably less trouble just to clip off the last 4 characters.


Keep in mind that Jacques and Pierre (Yeeee... I may be too American for this thread) are just giving you alternate approaches to the same thing: Jacques is giving a unix approach and Pierre is going for more pure applescript. Use whatever makes most sense to you.

Aug 15, 2012 11:13 AM in response to twtwtw

set originalDateString to short date string of originalFileCreationDate & space & time string of originalFileCreationDate

set revisedDateStr to (do shell script " date -j -f \"%D %I:%M:%S %p\" " & quoted form of originalDateString & " \"+%Y%m%d%H%M.%S\"")


No, no, no.


'short date string' returns a localized version of the date - that is, something that's typical for your current country/language settings. You can not predict or rely on what that format might be. In some cases it might be MM:DD:YY, but other countries might use DD:MM:YY, while others use DD/MM/YYYY.

Likewise with the time string - That string might be in 12 or 24-hour format, based on local settings.


Although it's more work, it would be better to extract the specific date/time elements and format in the order you want. It avoids ambiguity.


tell originalFileCreationDate

set myY to year

set myM to text -2 through end of ("0" & (its month as integer))

set myD to text -2 through end of ("0" & day)

set myHr to text -2 through end of ("0" & (its time) div 3600)

set myMin to text -2 through end of ("0" & (its time) mod 3600 div 60)

set mySec to text -2 through end of ("0" & (its time) mod 60 as text)

end tell

set revisedDateStr to myY & myM & myD & myHr & myMin & "." & mySec as text

Aug 15, 2012 12:01 PM in response to Pierre L.

Wooow...

How many helping responses! Thanks to you all.

I am trying the first sugested solution.


I have tried adding "file" to theOriginalFile twtwtw script.


(nameof filetheOriginalFile)


But the error persists (I changed -5 to -4, as the extension is only MPG)


tell application "System Events"


get POSIX path of every file of folder "HD Mac:users:fhuet:Desktop:Vid1" whose name extension = "MPG"


--> {"/Users/fhuet/Desktop/Vid1/MOV04859.MPG", "/Users/fhuet/Desktop/Vid1/MOV04860.MPG", "/Users/fhuet/Desktop/Vid1/MOV04882.MPG", "/Users/fhuet/Desktop/Vid1/MOV04905.MPG", "/Users/fhuet/Desktop/Vid1/MOV04906.MPG"}


get creation date of file "/Users/fhuet/Desktop/Vid1/MOV04859.MPG"


--> date "viernes, 31 de diciembre de 2004 23:39:00"


get text 1 thru -4 of name of file "/Users/fhuet/Desktop/Vid1/MOV04859.MPG"


--> error number -1720 from text 1 thru -4 of name of file "/Users/fhuet/Desktop/Vid1/MOV04859.MPG"

Resultado:

error "System Events ha detectado un error: Espectro no válido." number -1720 from text 1 thru -4 of name of file "/Users/fhuet/Desktop/Vid1/MOV04859.MPG"


If I do

setstrippedFileNametonameof file theOriginalFile


I got:


tell application "System Events"


get POSIX path of every file of folder "HD Mac:users:fhuet:Desktop:Vid1" whose name extension = "MPG"


--> {"/Users/fhuet/Desktop/Vid1/MOV04859.MPG", "/Users/fhuet/Desktop/Vid1/MOV04860.MPG", "/Users/fhuet/Desktop/Vid1/MOV04882.MPG", "/Users/fhuet/Desktop/Vid1/MOV04905.MPG", "/Users/fhuet/Desktop/Vid1/MOV04906.MPG"}


get creation date of file "/Users/fhuet/Desktop/Vid1/MOV04859.MPG"


--> date "viernes, 31 de diciembre de 2004 23:39:00"


get name of file "/Users/fhuet/Desktop/Vid1/MOV04859.MPG"


--> "MOV04859.MPG"


So it's OK. After that, obviously, it gives another error.


So, the problem is with the instruction

set strippedFileName to text 1 thru -4 of (name of filetheOriginalFile). There must be a sintaxis error on that. It says "ESPECTRO NO VALIDO" ERROR


I keep trying and will try the other options.


But I'd like to test all of them, as it's the best way to well understand and study the lenguage.



Thanks again.



Aug 15, 2012 12:12 PM in response to Pierre L.

Pierre L. wrote:


My two cents…


In the very special case where “the file names on both folders are the same and the number of files in the two folders are the same”, the following script should do exactly what you are asking for:


set folder1 to POSIX file "/POSIX/path/to/MPG1/folder"

set folder2 to POSIX file "/POSIX/path/to/MOV/folder"



It was exactly my case: I tried your script and worked perfectly.


I had a little problem with the POSIX path, as I included the "Macintosh HD:/Users/fhuet/Desktop/Vid1" and it didn't work.

But when I wrote only "/Users/fhuet/Desktop/Vid1", It was all OK.


Thank you!!!!


Anyway, I'll try the other solutions.

Aug 15, 2012 12:28 PM in response to twtwtw

Dear twtwtw,


I have done as you said, and the Script goes on, but it stops on another error:


tell application "System Events"


get POSIX path of every file of folder "HD Mac:users:fhuet:desktop:Vid1" whose name extension = "MPG"


--> {"/Users/fhuet/Desktop/Vid1/MOV04859.MPG", "/Users/fhuet/Desktop/Vid1/MOV04860.MPG", "/Users/fhuet/Desktop/Vid1/MOV04882.MPG", "/Users/fhuet/Desktop/Vid1/MOV04905.MPG", "/Users/fhuet/Desktop/Vid1/MOV04906.MPG"}


get creation date of file "/Users/fhuet/Desktop/Vid1/MOV04859.MPG"


--> date "viernes, 31 de diciembre de 2004 23:39:00"


get name of file "/Users/fhuet/Desktop/Vid1/MOV04859.MPG"


--> "MOV04859.MPG"


get item 1 of POSIX path of every file of folder "HD Mac:users:fhuet:desktop:Vid2" whose name = "MOV04859.mov"


--> error number -1700 from item 1 of POSIX path of every file of folder "HD Mac:users:fhuet:desktop:Vid2" whose name = "MOV04859.mov" to reference

Resultado:

error "System Events ha detectado un error: No es posible convertir item 1 of POSIX path of every file of folder \"HD Mac:users:fhuet:desktop:Vid2\" whose name = \"MOV04859.mov\" en tipo reference." number -1700 to reference


It seems there is a problem with the get item 1.

Any idea?

Thx

Aug 15, 2012 12:34 PM in response to Pierre L.

Merci bien pour ton aide.


Dans le chapitre X du premier volume de Don Quichotte de la Manche de Miguel de Cervantes, après un de ses batailles, Don Quichotte mentionne à Sancho Panza qu'il connaît la recette pour le baume de Fierabras. C'est unepotion magiquepour guérirtous les mauxdu corps humain.


Sorry for my bad french, but I have not used it for more than 20 years...

Aug 15, 2012 12:43 PM in response to Jacques Rioux

Jacques.... Magnifique!! Énorme!!


Your Script is really amazing.

It's very very quick and it's absolutly OK.


My problem (or better "my limitation") is that I think i'll never would be capable of learning the Unix or shell lenguage!!


Thanks





Jacques Rioux wrote:


Here is a script that use another method, try it:

--------------------------------------

set mpg1Folder to quoted form of POSIX path of (choose folder with prompt "Select folder wich contains .mpg1 files")

set movFolder to quoted form of POSIX path of (choose folder with prompt "Select folder wich contains .mov files")


do shell script "cd " & mpg1Folder & " && for f in *.MPG1;do /usr/bin/perl -e 'utime($ARGV[0], $ARGV[0], \"$ARGV[1]\")' $(/usr/bin/stat -f %B \"$f\") " & movFolder & "/\"${f%.MPG1}.mov\"; done"

Applescript for matching two files creation date

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.