adding date to a filename

I would like to create a droplet and when I drop a file in it,
it has to add date, time and ok

for example:

filename.txt

has to become:

20131203-192358-filename-ok.txt

As i’m not on my mac right now, i haven’t compiled it, so may contain error’s…


on open added_items
try
	repeat with i in added_items
		set AppleScript's text item delimiters to ":"
		set tldn to count text items of i
		set filename to text item tldn of i
		set filepath to text items 1 thru ((text item tldn) - 1) of i
		set filename to (current date) & " - " & filename
		set newfilepath to filepath & ":" & newfilename
		do shell script "mv " & i & " " & newfilepath
	end repeat
on error x
display dialog "Error" default answer x
end open

in this case the mv command should rename instead of move, since it actually moves the files to a file in the same folder, but with another name.

oops! unfortunately that didn’t work

it gives me:

Syntax Error
Expected “error”, “try”, etc. but found command name.


on open added_items
	try
		repeat with i in added_items
			set AppleScript's text item delimiters to ":"
			set tldn to count text items of i
			set filename to text item tldn of i
			set filepath to text items 1 thru ((text item tldn) - 1) of i
			set filename to (current date) & " - " & filename
			set newfilepath to filepath & ":" & newfilename
			do shell script "mv " & i & " " & newfilepath
		end repeat
	on error x
		display dialog "Error" default answer x
		
	end try
end open

The end try was missed out

this is the message i get:

every text item of item 1 of { alias “Macintosh HD:Users:apple:Desktop:file test.png” } doesn’t understand the “count” message

Hi,

that happens because in the repeat loop ‘repeat with i in added_items’, item i is a reference to an item in the list and not the actual value. to get the value use ‘contents of’.

Edited: i.e. ‘contents of i’ to get the value

gl,
kel

This one is supposed to be corrected.

on open added_items
	try
		set Otids to AppleScript's text item delimiters # ADDED
		set AppleScript's text item delimiters to ":" # MOVED
		repeat with i in added_items
			set i to i as text # ADDED
			set tldn to count text items of i
			set filename to text item tldn of i
			set filepath to text items 1 thru (tldn - 1) of i as text # EDITED
			#set filename to (current date) & " - " & filename # it inserted the date in long format
			tell (current date) to (((its year) * 10000 + (its month) * 100 + (its day)) as text) & "-" & text 2 thru -1 of ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as text) # ADDED
			set filename to result & " - " & filename # ADDED
			set newfilepath to filepath & ":" & filename # EDITED
			do shell script "mv " & quoted form of POSIX path of i & " " & quoted form of POSIX path of newfilepath # EDITED
		end repeat
	on error x
		set AppleScript's text item delimiters to Otids # ADDED
		display dialog "Error" default answer x
	end try
	set AppleScript's text item delimiters to Otids # ADDED
end open

Yvan KOENIG (VALLAURIS, France) mardi 3 décembre 2013 22:18:04

now I get:

Can’t get text item 5 of alias “Macintosh HD:Users:apple:testfile.mp4”.

Normal I was editing it while you tested it.
Grab it above.

Yvan KOENIG (VALLAURIS, France) mardi 3 décembre 2013 22:29:10

now I get:

the variable newfilename is not defined

It’s corrected.

Yvan KOENIG (VALLAURIS, France) mardi 3 décembre 2013 23:03:14

Sorry!

as i said, i wrote it out of my head. So wasn’t sure wether it would work.

it works perfectly!!! thanks a lot!!!