Rename file by Creation date

what is wront with this script

The snippet you posted is fine. Any problems you’re having are probably coming from somewhere else in your script so you’ll need to post more code. One thing to keep in mind is that “creation date” includes the time which is colon seperated. You can’t name a file in Mac OS X with a “:” in the file name because this is used to seperate folders. So if you had a folder named “My:Stuff” it could create problems with the OS thinking that it’s actually a file/folder called “Stuff” in a folder called “My”. I hope that makes sense.

So, this could be causing the problem or, as I said earlier, it could be another part of the script. Post the error your getting and which line is hilighted in Script Editor when the error occurs and someone should be able to help you out.


tell application "Finder"
	
	set pathString to "Mac HD:My Folder:My File"
	
	set fileRef to alias pathString -- alias
	-- or
	set fileRef to item pathString -- Finder object
	
	set dateObject to creation date of fileRef --> date object
	
	set newName to dateObject as string
	
	-- On my system, date strings have colon characters ":",
	-- which aren't allowed in filenames. On some systems,
	-- date strings would also have the forward slash "/",
	-- which is also disallowed (under OSX).
	--
	set newName to my ReplaceText(newName, ":", "-")
	set newName to my ReplaceText(newName, "/", ",")
	
	set name of fileRef to newName
	
end tell

on ReplaceText(s, f, r)
	set astids to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to f
		set s to s's text items
		set AppleScript's text item delimiters to r
		set s to s as string
	on error m from f to t partial result p
		set AppleScript's text item delimiters to astids
		error m from f to t partial result p
	end try
	set AppleScript's text item delimiters to astids
	return s
end ReplaceText

I don’t think you read his snippit very carefully. :slight_smile:

He set a variable to the name of a file, OK. Then he immediately sets the same variable to another value, without having used the first value in any way. The second value he tried to set it to is “creation date”… of what? Even if he had said “creation date of theFile”, he still neglected to coerce the returned date object to a string.

I’m not picking on him, by the way, I’m just pointing out that telling him his snippit is fine is misleading.

What i want to happen is that when you put files in a folder that it would rename them to there creation date w/ time. Also maybe a addtional prefix and with its extension

so if you drop a file that is named “Fax 9259874561.pdf”

It would rename it to “05-04-04 (2-22-36).pdf” or “New Fax 05-04-04 (2-22-36).pdf” Somthing like “date-time.pdf” or “New Fax date-time.pdf”

thanks all for the help

I understand changing the “:” and “/” to something OSX can uses how would i add that do this script

I cannot believe I missed that. At least I was the only one sleeping at the wheel. :lol:

still not working though.

So, to make up for it:

This script will do it. There’s a couple of things to keep in mind when working with folder actions. First:

on adding folder items to this_folder after receiving this_file

the variable that you’re setting to “this_file” will actually always be a list. No matter how many items you drop on a folder actioin, “this_file” will always be a list, it may just be a one item list. So you need to loop through every item of the list in your script.

Second: When you rename a file using a folder action, the folder thinks that another new file has been added to it and it tries run the action again. To get around this, you need to create a “Renamed” folder inside the folder you’re attaching the script to and check for it in your script, then move the file before you rename it.

Here it is:


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

You’ll get file names of the form “New Fax date (time).pdf”

Hope this helps.

BTW: I left out AM/PM so you could potentially have two files with the same name created. If you need this to be in the file name, just let me know and I can fix it if you need help doing it. Lastly, if you use a non-American date format, this will definitely “break” the script and you might get a file name with two months and no day or something like that.

Thanks. Looks like this is working great so far.

Hello jakacmar,

I am running OS/X 2.8, and your script works only partially. The folder “Rename” is created, and the added files are moved to it, but NOT RENAMED. Is it me, or the computer, or something else. Thanks in advance for your help/advice.

Sincerely,

Variable as the shade

Well then,

Would anyone else be able to advise/help me with this script, please? (see post immediately above)

Thanks in advance,

Variable as the shade

Hmm… Well, I ran his script and it worked fine for me, so OS/X 2.8 is doing something funny that I can’t test. Here are some thoughts:


...
	repeat with thisItem in theseItems

		-- The above creates a special kind of reference syntax:
		-- 'item 1 of { ..., ..., etc... }'. Try an explicit dereference:
		--
		set thisItem to contents of thisItem
...
			set movedItem to (move thisItem to renamedFolder) as alias

			-- I actually seem to remember there being some sort of
			-- problem with the Finder's move command not returning
			-- accurate information. Try replacing the above with
			-- these two lines:
			--
			move thisItem to renamedFolder
			set movedItem to file (thisItem's name) of renamedFolder

Let me know if any of this helped. :slight_smile:

It’s the dateFormat handler that’s the problem. Month to integer coercion didn’t become possible until 10.3. This works:

on adding folder items to thisFolder after receiving theseItems
  tell application "Finder"
    if not (exists folder "Renamed" of thisFolder) then
      set renamedFolder to (make new folder at thisFolder with properties {name:"Renamed"})
    else
      set renamedFolder to folder "Renamed" of thisFolder
    end if
    repeat with thisItem in theseItems
      if kind of thisItem is not "Folder" then
        set myDate to creation date of thisItem
        set movedItem to (move thisItem to renamedFolder) as alias
        set formattedDate to (my dateFormat(myDate))
        set newName to "New Fax " & formattedDate & ".pdf"
        set name of movedItem to newName
      end if
    end repeat
  end tell
end adding folder items to

on dateFormat(theDate)
  set {year:y, day:d, time:t} to theDate
  copy theDate to b
  set b's month to January
  
  tell (y * 10000 + (b - 2500000 - theDate) div -2500000 * 100 + d) as string
    set dateString to text 5 thru 6 & "-" & text 7 thru 8 & "-" & text 3 thru 4
  end tell
  tell (1000000 + t div hours * 10000 + t mod hours div minutes * 100 + t mod minutes) as string
    set dateString to dateString & " (" & text 2 thru 3 & "-" & text 4 thru 5 & "-" & text 6 thru 7 & ")"
  end tell
  return dateString
end dateFormat

I didn’t even think to look there… I am so ashamed. :wink:

Nigel,

Many thanks for your code. However, I hope you will assist me once again with this same code as it only appears to move and rename the first file (and sometimes the second, as well) placed in the folder and then it simply moves all subsequent files without renaming them to the specified folder. I remain in your debt for your recent aid.

Sincerely,

Variable as the shade

Hi, Variable as the shade.

jakacmar’s script, with my variation on the dateFormat handler, works perfectly on my OS 10.2.8 machine. The only time that subsequent files don’t get renamed is if they have exactly the same creation date as one of the files already renamed - since obviously the names are based on the creation dates and you can’t have two items with the same name in the same folder.

Here’s a slightly extended version of the script with a crude duplicate check built in. Subsequent files with the same date have “#” and a number included in their names. If you intend to remove some files from the “Renamed” folder, you may need something more thorough.

on adding folder items to thisFolder after receiving theseItems
  tell application "Finder"
    if not (exists folder "Renamed" of thisFolder) then
      set renamedFolder to (make new folder at thisFolder with properties {name:"Renamed"})
    else
      set renamedFolder to folder "Renamed" of thisFolder
    end if
    repeat with thisItem in theseItems
      if kind of thisItem is not "Folder" then
        set myDate to creation date of thisItem
        set movedItem to (move thisItem to renamedFolder) as alias
        set formattedDate to (my dateFormat(myDate))
        set newName to "New Fax " & formattedDate & ".pdf"
        -- ****** Check for duplicate name
        set i to count (files of renamedFolder whose name contains formattedDate)
        if i > 0 then
          set newName to "New Fax " & formattedDate & " #" & (i + 1) & ".pdf"
        end if
        -- ******
        set name of movedItem to newName
      end if
    end repeat
  end tell
end adding folder items to

on dateFormat(theDate)
  set {year:y, day:d, time:t} to theDate
  copy theDate to b
  set b's month to January
  
  tell (y * 10000 + (b - 2500000 - theDate) div -2500000 * 100 + d) as string
    set dateString to text 5 thru 6 & "-" & text 7 thru 8 & "-" & text 3 thru 4
  end tell
  tell (1000000 + t div hours * 10000 + t mod hours div minutes * 100 + t mod minutes) as string
    set dateString to dateString & " (" & text 2 thru 3 & "-" & text 4 thru 5 & "-" & text 6 thru 7 & ")"
  end tell
  return dateString
end dateFormat

Hello Nigel,

Many thanks for your new script. It not only works perfectly, but is 'fast as greased lightning as well. Everything being green lights and blue skies from my corner of the scripting planet, I simply can’t thank you enough. Please let me know if there’s anything I can do for you.

Sincerely,

Variable as the shade