Reputation: 15083
I am trying to change the name of a file. Seems simple enough, if one is able to change the "displayed name" property. But I keep getting this error:
Can't set displayed name of alias "Path:to:file:" to "New_name"
Here's the folder action script I'm using (i.e. saved applescript, then used folder action setup service to assign it to my folder):
on adding folder items to this_folder after receiving these_items
try
repeat with this_item in these_items
tell application "Finder" to set displayed name of this_item to "New_Name"
end repeat
on error error_message number error_number
display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
end try
end adding folder items to
All the scripts I'm finding that does something similar (e.g. this question) first get the "name" property then strip the extension. I'd rather just go straight to the "displayed name" property.
Upvotes: 1
Views: 4599
Reputation: 6932
Lauir Ranta's answer is correct for the Finder.
But after posting my comment I remembered that the System events looks at things a bit deeper than the Finder.
So I swapped the commands to change the name to from Finder to System events So now it works.
Before when I had a file named "someFile.kkl" and the extension is just made up. Finder would return "" for the extension and rename the file without an extension. "newName"
But when System events does it it sees the extension and sets the name to "newName.kkl"
tell application "Finder" to set thisFile to (item 1 of (get selection) as alias)
tell application "System Events"
if name extension of thisFile is "" then
set name of thisFile to "newName"
else
set name of thisFile to ("newName" & "." & name extension of thisFile)
end if
end tell
set in a folder action.
on adding folder items to this_folder after receiving these_items
try
repeat with this_item in these_items
tell application "System Events"
if name extension of this_item is "" then
set name of this_item to "new_Name"
else
set name of this_item to ("new_Name" & "." & name extension of this_item)
end if
end tell
end repeat
on error error_message number error_number
display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
end try
end adding folder items to
Upvotes: 1
Reputation: 27613
The displayed name can contain the extension if the file has an extension that isn't recognized by Finder or if showing all extensions is enabled.
Appending the previous extension wouldn't be that complex:
tell application "Finder"
set f to some file of desktop
set name of f to "New_name" & "." & name extension of f
end tell
This would also work if the file has no extension or if the extension isn't recognized by Finder:
set text item delimiters to "."
tell application "Finder"
set f to some file of desktop
set ti to text items of (get name of f)
if number of ti is 1 then
set name of f to "New_name"
else
set name of f to "New_name" & "." & item -1 of ti
end if
end tell
If you created the folder action with Automator, you could use a do shell script action like this:
for f in "$@"; do
mv "$f" "New_name.${f##*.}"
done
Upvotes: 3