phonography

Renaming Files using Applescript

I seem to keep rewriting my file utilities each time I move from PC to Mac, C to Perl and back and now Applescript. Zoom devices are still lacking the ability to save files using time and date stamps. Arguably this is not an issue using the BWF format, however standard directory applications don’t read this headers. My usual approach is to use last modified dates which should use the originatig filesystem settings (often a variation on FAT), so fairly safe.

The language took a hour or 2 to get used to and came up with the following.

set file_ to choose file
set file_alias to (file_ as alias)
tell application "Finder"
	get properties of file_alias

	set d to (get modification date of file_alias)
	set t to (time string of d)
	set h to (hours of d as string)
	set y to (year of d as string)
	set m to (month of d as integer as string)
	set dy to (day of d as integer as string)
	set ss to (seconds of d as integer as string)
	set mm to (minutes of d as integer as string)

	-- fix padding of day
	repeat while length of dy < 2
		set dy to ("0" & dy)
	end repeat

	-- fix padding of month
	repeat while length of m < 2
		set m to ("0" & m)
	end repeat

	repeat while length of h < 2
		set h to ("0" & h)
	end repeat

	repeat while length of mm < 2
		set mm to ("0" & mm)
	end repeat

	repeat while length of ss < 2
		set ss to ("0" & ss)
	end repeat

	set f to (get folder of file_alias as string)

	set long_string to y & "-" & m & "-" & dy & "T" & h & "h" & mm & "m" & ss & "s" & ".wav"

	display dialog (long_string)

	set name of file file_alias to long_string

end tell

Current limitation is that it can only be run once, on a per file basis, but this gets me to renaming files like STE-012.wav to 2009-04-10T14h07m24s.wav, so a good start point.

Update

The following allows a batch process to be done using a multiple file selection. From here it would be good to use a temporary drop folder with automation attached. Maybe another day.

-- open a dialog with multiple selections enabled
set file_ to choose file with multiple selections allowed

-- set a boolean to skip verify action
set global_OK to false

repeat with this_file in file_

	set file_alias to (this_file as alias)
	tell application "Finder"
		get properties of file_alias

		set file_type to (get kind of file_alias)

		if (file_type is "WAVE Audio File") then
			--display dialog (file_type)

			set d to (get modification date of file_alias)
			set t to (time string of d)
			set h to (hours of d as string)
			set y to (year of d as string)
			set m to (month of d as integer as string)
			set dy to (day of d as integer as string)
			set ss to (seconds of d as integer as string)
			set mm to (minutes of d as integer as string)

			-- fix padding of day
			repeat while length of dy < 2
				set dy to ("0" & dy)
			end repeat

			-- fix padding of month
			repeat while length of m < 2
				set m to ("0" & m)
			end repeat

			repeat while length of h < 2
				set h to ("0" & h)
			end repeat

			repeat while length of mm < 2
				set mm to ("0" & mm)
			end repeat

			repeat while length of ss < 2
				set ss to ("0" & ss)
			end repeat

			set f to (get folder of file_alias as string)

			set long_string to y & "-" & m & "-" & dy & "T" & h & "h" & mm & "m" & ss & "s" & ".wav"

			if (global_OK is false) then

				set question to display dialog "Renaming " & file_alias & " to " & long_string buttons {"OK", "OK for All", "Cancel batch"} default button 1
				set answer to button returned of question

				if (answer is "Cancel batch") then
					display dialog "Cancelled" buttons {"Understood"}
					return
				end if

				if (answer is "OK for All") then
					set global_OK to true
				end if

			end if
			set name of file file_alias to long_string
		else
			display dialog ("Not a wav file, skipping")
		end if
	end tell

end repeat