Rename current open file/document?

I’m wanting a script to be able to rename open files (usually Photoshop CS5 files or TextEdit documents).
I saw this one and tried changing the application name to TextEdit and also Photoshop CS5, but didn’t work.

Any help would be greatly appreciated.

for textedit you can achieve this with minor changes

tell application "TextEdit"
	set old_name to name of document of window 1
	set dialog_result to display dialog ¬
		"Rename active document:" default answer (old_name) ¬
		buttons {"Cancel", "Rename"} default button 2 ¬
		with icon note
	if button returned of dialog_result = "Rename" then
		set new_name to text returned of dialog_result
		set d to properties of document of window 1
		if path of d is equal to missing value then
			set name of document of window 1 to new_name
			tell me to set the_file to POSIX file (d's path as text) as alias
			tell application "Finder"
				set name of the_file to new_name
			end tell
		end if
	end if
end tell

Thanks so much, that’s great!

I tried it with Preview and Photoshop (changing the application names to “Preview” and “Adobe Photoshop CS5”) but it wouldn’t work with those.

For Preview it says “error “Preview got an error: Can’t get name of document of window 1.” number -1728 from name of document of window 1” and for Photoshop it says “error “Adobe Photoshop CS5 got an error: Can’t get window 1.” number -1728 from window 1”.

Any ideas on those?

Well Jono, preview is not scriptable and jou can’t just change the names of the application and expect the same results. You’re talking with a different ‘API’ so the results, commands and property (and values) can be different.

Ah, OK. Thanks anyway.

I’ve only just started learning AppleScript, so maybe somewhere down the line I’ll be able to figure this out myself :slight_smile:

For many application (document based applications) they don’t use document of window 1 but change it into just document 1. As far as I know does Quark and Adobe use documents and not windows. Also path of document could be file of document in some versions.

Thanks a lot for the info.