I thought this would be Easy!!
I want to change the name of a file and replace it with the date & time it was created.
So far this is what I have:
set thedate to current date
set TheMonth to month of thedate
set TheDay to day of thedate
set TheTime to time of thedate
set dateString to (TheMonth & " " & TheDay & ", " & TheTime) as string
tell application “Finder”
select file “Attachment (image/bmp document)” of folder “Attachments”
set dateString to dateString & “.jpg”
set name of selection to dateString as string
print selection
end tell
I end up with a file called “March 20, 50504.jpg” I know that 50504 indicates the number of seconds after midnight but I want a time stamp (example 5:15 pm).
Also at the moment I can’t get the creation date only the current date, and I would preferr the creation date.
Can anyone help??
Ben
PS I’m new to scripting so go easy!!
benreilly,
You are pretty close but I would do the following. I didn’t have much time to hammer this together - so maybe someone can improve on it.
--first define a path to the folder that houses the file you want to rename, this is just text for now
set myFolder to "Mac HD:Some Folder:Attachments:"--change this to the path of the folder
set fileName to "file name here" --this is the name of the file now
set myFile to myFolder & fileName as string--put the two together to get your file path
--next get the date by splitting up the current date
set thedate to the current date --get the current date
set thedate to thedate as string --coerce to string so we can work with it as text
set oldTID to AppleScript's text item delimiters --get the current text item delimiters so we can set it back
set newTID to " " --we want to split the date up at the spaces to get the items
set AppleScript's text item delimiters to newTID --set the TID's to spaces
set dateItems to the text items of thedate --get the text items of the current date
set AppleScript's text item delimiters to oldTID --reset the text item delimiters
--you cannot use ":" in the name of your file as the mac reserves that character for system uses, so replace it with a period, or whatever
set thedate to item 5 of dateItems
--use the text item delimters again to split it, this time by ":"
set oldTID to AppleScript's text item delimiters --get the current text item delimiters so we can set it back
set newTID to ":" --we want to split the date up at the spaces to get the items
set AppleScript's text item delimiters to newTID --set the TID's to spaces
set timeItems to the text items of thedate
set newDate to item 1 of timeItems & "." & item 2 of timeItems & "." & item 3 of timeItems as string
set AppleScript's text item delimiters to oldTID --reset the text item delimiters
--set up the new name
set newFileName to item 2 of dateItems & " " & item 3 of dateItems & " " & newDate & ".jpg" as string
--point the finder to the newly named file so we can print it
set newPathForPrint to myFolder & newFileName
tell application "Finder"
set the name of file myFile to newFileName --have the finder rename the file, must be within the tell Finder block
print file newPathForPrint
end tell
Hope this helps you,
Thanks for the help!!
This is what I using. I’ve made the script into a Folder Action Script as the files can be added by “Hand” or “Email”.
on adding folder items to this_folder after receiving these_items
tell application "Finder"
repeat with this_item in these_items
select item this_item
set theDate to current date
set TheMonth to month of theDate
set theDay to day of theDate
set theTime to time string of theDate
set AppleScript's text item delimiters to ":"
set TheTimeList to text items of theTime
set AppleScript's text item delimiters to "-"
set theTime to TheTimeList as text
set AppleScript's text item delimiters to ""
set dateString to (TheMonth & " " & theDay & ", " & theTime) & ".jpg" as string
set name of selection to dateString as string
print selection
end repeat
end tell
end adding folder items to
Regards
Ben