APPLESCRIPT : dialog box to renaming FILE or FOLDER

Hi, I am desperately trying to concatenate these two APPLESCRIPT codes in order to perform a simple renaming action with a standard dialog box. I am missing something (knowledge) to integrate the whole thing: an APPLESCRIPT error has always returned to me...

Details of the desired action:
1 – Select an ITEM from the Finder, whether it is a File or a Folder and wherever it is on the disk, including the Desktop.
2 – Invoke a dialog box to rename this ITEM.
3a – If the ITEM is a Folder, the dialog box displays its current name and allows the user to enter a new name.
3b – If the ITEM is a File, the dialog box displays its current name, BUT without its extension and this allows the user to enter a new name without changing the extension.
4 – Once the new name has been entered and validated, the dialog box disappears.
5a – If the ITEM was a Folder, it will now have the name entered in the dialog box.
5B – If the ITEM was a File, it will now have the name entered in the dialog box, BUT will have its original extension back.

I already have the same KM macro, but I need an APPLESCRIPT code here.

Here is the first code that arbitrarily assigns the name “newName” to the selected ITEM:

tell application "Finder"
	set itemSelection to (item 1 of (get selection) as alias)
end tell

tell application "System Events"
	if name extension of itemSelection is "" then
		set name of itemSelection to "newName"
	else
		set name of itemSelection to ("newName" & "." & name extension of itemSelection)
	end if
end tell

And here is the second code that gives the possibility to enter a new name, BUT displays the extension for Files:

tell application "Finder"
	set itemSelection to selection
	repeat with aTemp in itemSelection
		set newName to name of aTemp
		set newName to text returned of (display dialog "Saisir nom :" default answer newName)
		set name of aTemp to newName
	end repeat
end tell

Thank you for your attention and above all a very happy new year to all... :wink:

Don't use System Events -- you can do everything in Finder. Something like:

tell application "Finder"
	set theSelection to item 1 of (get selection)
	set theClass to class of theSelection
	if theClass is folder then
		set theName to name of theSelection
	else if theClass is document file or theClass is alias file then
		set AppleScript's text item delimiters to "."
		set theName to (text items 1 thru -2 of (get name of theSelection)) as text
		set theExt to text item -1 of (get name of theSelection)
	else
		display dialog "Make sure you select a file or folder"
		return 1
	end if
	
	set newName to text returned of (display dialog "Name to use" default answer theName)
	
	if theClass is folder then
		set name of theSelection to newName
	else
		set name of theSelection to (newName & "." & theExt)
	end if
	
end tell

...should do what you want.

1 Like

Hi Nige_S, once again... you are my hero!…
Your code works wonderfully!…

A thousand thanks to you. :wink:

Obviously, I lack a lot of knowledge to arrive at your solution. I find that there is no faster way to rename a File or a Folder. Pressing the “Enter” key in the Finder on an item does not always work on the first try.

Weird -- it should do.

What you could do instead is set your own keyboard shortcut for the Finder's "Rename" Manu item in System Settings->Keyboard Shortcuts->App Shortcuts:

Nige_S, your code seems to me to be THE most efficient and fast solution to rename a file or a folder, definitely (for me). :wink:

I took the opportunity to add some cosmetic elements...

1 Like