Change Mod/Creation Date to folder path problem

Hi Hank,

the definition is necessary, whenever a variable is passed as an address pointer (&variable) and the called method does not definitely write into this variable

Thanks Stefan. I’ve seen that before too so it wasn’t a big surprise. The surprise was that it worked flawlessly up until now, and this particular version even worked when the file name only had a few characters… so I can’t explain why the problem happens only with long file names. In other words the subsequent line where I use if (error) didn’t cause an error with short file names but with long file names it did. I can’t explain that.

Hello regulus

Just a detail :

In the source posted in your web page you forgot to update the version num which is always 2 :wink:

Yvan KOENIG (VALLAURIS, France) lundi 31 janvier 2011 22:54:14

I also found out how to update the EXIF date if you have EXIFtool. This script will also create EXIF headers if one doesnt exist. It copies the Mod Date from the system and put it in the EXIF header.

-- this will change the creation and modification dates of dropped files and/or the files in dropped folders,
-- assuming file paths ending "./yyyy/mm/dd/file name".
-- http://www.hamsoftengineering.com/codeSharing/ChangeFileDates/ChangeFileDates.html
-- EXIF date change with EXIF tool
-- http://www.sno.phy.queensu.ca/~phil/exiftool/

on open dropped_items
	set changeFileDatesPath to POSIX path of ((path to home folder as text) & "UnixBins:ChangeFileDates")
	
	repeat with this_item in dropped_items
		set this_POSIX to POSIX path of this_item
		if (this_POSIX ends with "/") then
			-- If this item's a folder, get the POSIX paths of all the visible files in it.
			set these_POSICES to (do shell script "find -f " & (quoted form of this_POSIX) & " \\( -type f \\) \\! \\( -path \"*/.*\" \\)")
			-- Lose the double slashes which "find" inserts into the paths and get the paths as individual entities.
			set od to AppleScript's text item delimiters
			set AppleScript's text item delimiters to {"//"}
			set these_POSICES to these_POSICES's text items
			set AppleScript's text item delimiters to {"/"}
			set these_POSICES to paragraphs of (these_POSICES as text)
			set AppleScript's text item delimiters to od
			-- Deal with each file individually.
			repeat with this_POSIX in these_POSICES
				handle_file(this_POSIX, changeFileDatesPath)
			end repeat
		else -- This dropped item's a file.
			handle_file(this_POSIX, changeFileDatesPath)
		end if
	end repeat
	display dialog "Complete"
end open

on handle_file(File_Path, changeFileDatesPath)
	-- Arrange the relevant parts of the file path into a mm/dd/yyyy short date string.
	-- It's assumed that the folder names will already be in yyyy, mm, and dd formats.
	set od to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {"/"}
	set {folder_year, folder_month, folder_day} to text items -4 thru -2 of File_Path
	set creationdate to {folder_month, folder_day, folder_year} as text
	set AppleScript's text item delimiters to od
	
	-- Get the file's creation date and format it in 24-hour time as hh:mm:ss
	tell application "System Events" to set createdtime to time of (get creation date of file File_Path) -- System Events accepts POSIX paths in file specifiers.
	tell (1000000 + createdtime div hours * 10000 + createdtime mod hours div minutes * 100 + createdtime mod minutes) as text
		set createdtime to text 2 thru 3 & ":" & text 4 thru 5 & ":" & text 6 thru 7
	end tell
	set creationdate to quoted form of (creationdate & " " & createdtime)
	
	-- Set the file's creation and modification dates to the above date and time.
	do shell script (changeFileDatesPath & " -cDate " & creationdate & " -mDate " & creationdate & " -file " & quoted form of File_Path)
	do shell script "exiftool -overwrite_original_in_place '-exif:datetimeoriginal<filemodifydate'" & " " & quoted form of File_Path
	--	If you want to preserve the Modification date you must run ChangeFileDates again
	--	do shell script (changeFileDatesPath & " -cDate " & creationdate & " -mDate " & creationdate & " -file " & quoted form of File_Path)
end handle_file

Thanks!

I’m still feeling my way with “find” at the moment. The above might look a bit less cryptic like this:

set these_POSICES to (do shell script "find -f " & (quoted form of this_POSIX) & " \\( -type f -and -not -path '*/.*' \\)")

It means: in the hierarchy of which this_POSIX is the root folder, find all items which are normal files and whose paths don’t contain any elements beginning with a dot. I’ve changed it in my script above.

Further edit 2011/02/05: I’ve now found a simpler way to get rid of the double slashes in the “find” result and have edited the script again accordingly.