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

Question:

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

Reply
Question marked as Solved
Answer:
Answer:

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

Posted on

Question marked as Helpful

Aug 15, 2012 7:23 AM in response to Fierabras 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

There’s more to the conversation

Read all replies

Page content loaded

Question marked as Helpful

Aug 15, 2012 7:23 AM in response to Fierabras 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 7:23 AM

Reply Helpful (1)

Aug 15, 2012 8:40 AM in response to twtwtw 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:40 AM

Reply Helpful

Aug 15, 2012 8:46 AM in response to twtwtw 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 8:46 AM

Reply Helpful

Aug 15, 2012 9:32 AM in response to Fierabras 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:32 AM

Reply Helpful

Aug 15, 2012 9:57 AM in response to Fierabras 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 9:57 AM

Reply Helpful

Aug 15, 2012 11:13 AM in response to twtwtw 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 11:13 AM

Reply Helpful
Question marked as Solved

Aug 15, 2012 11:39 AM in response to Fierabras 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:39 AM

Reply Helpful

Aug 15, 2012 11:43 AM in response to Camelot 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 11:43 AM

Reply Helpful

Aug 15, 2012 12:01 PM in response to Pierre L. 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:01 PM

Reply Helpful
Question marked as Helpful

Aug 15, 2012 12:11 PM in response to Fierabras In response to Fierabras

Ah, System Events is sometimes picky about resolving references. this works:


set strippedFileName to text 1 through -4 of (get name of file theOriginalFile)


the added get forces it to get the actual string filename rather than a reference to it.

Aug 15, 2012 12:11 PM

Reply Helpful (1)

Aug 15, 2012 12:12 PM in response to Pierre L. 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:12 PM

Reply Helpful

Aug 15, 2012 12:28 PM in response to twtwtw 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:28 PM

Reply Helpful
User profile for user: Fierabras

Question: Applescript for matching two files creation date