I’ve spent most of the morning clumsily trying to get a file-renaming folder action to work, and now I’m thinking I’d like to have the script ask the user where to move the file.
But first things first… what I have now doesn’t seem to fly:
(The IF is to prevent the renaming routine from repeating over and over)
on adding folder items to this_folder after receiving added_items
set suffix to (“_BL.pdf”)
tell application “Finder”
repeat with anItem in added_items
set oldName to name of anItem
if oldName contains “BL” then
else
set newName to (oldName & suffix) as Unicode text
set name of anItem to newName
end if
end repeat
end tell
end adding folder items to
name of anItem returns the name with extension.
You need to get the name without extension.
When extensions are always hidden you can ask for displayed name, otherwise you have to split it off using text item delimiters
(and allowing for more than one dot in the name):
----------------------------------------------------------------
-- Get file name without extension
-- allows multiple dots
-- allows no extension
-- parameter: a file name
-- returns: file name without extension
---------------------------------------------------------------
on getNameWithoutExtension(itemName)
set text item delimiters to "."
if (count text items of itemName) > 1 then
set displayedName to (text items 1 through -2 of itemName) as text
else
set displayedName to itemName
end if
set text item delimiters to ""
return displayedName
end getNameWithoutExtension
Your if clause can be streamlined:
if oldName does not contain "BL" then
set newName to oldName & suffix
set name of anItem to newName
end if