Banging my head against the wall trying to figure out why this won’t work. I’m trying to append a prefix to all visible files in a folder, after first making sure the prefix does not already exist. This script returns the error: Can’t set name of “item1” to “bu_item1”
set backup_files to list folder alias (path to folder) without invisibles
set item_count to count of backup_files
tell application "Finder"
tell backup_files
repeat with i from 1 to item_count
set old_filename to item i of backup_files
if old_filename does not start with "bu_" then
set new_filename to "bu_" & old_filename
set name of old_filename to new_filename
end if
end repeat
end tell
end tell
This site has been a lifesaver. Thanks for the help.
The variable old_filename is just a name and not a reference. What you need to do is concatenate the old name to the path to to the folder and coerce to alias. But first, what is the path to the folder? This line:
list folder alias (path to folder)
doesn’t make sense. There are several ways to get a path to the folder. For now try this:
set f to choose folder
list folder f
Then to concatenate the old name:
set item_reference to (“” & f & old_filename) as alias
Then you can change the name of item reference to the new name.
Thanks kel,
(path to folder) was just shorthand for my actual folder location. Actual script was
set backup_files to list folder alias "Powerbook:Users:dou:Documents:script tutorial:daily intake:" without invisibles
set item_count to count of backup_files
tell application "Finder"
tell backup_files
repeat with i from 1 to item_count
set old_filename to item i of backup_files
if old_filename does not start with "bu_" then
set new_filename to "bu_" & old_filename
set name of old_filename to new_filename
end if
end repeat
end tell
end tell
So I have a reference to a list which is backup_files. Are you saying I need to concatenate using backup_files?
When you use the ‘list folder’ command, you get a list of names (strings). These are not references which is needed to name items. So you have to create a reference. Here’s a mod to your script:
-- a path to the orginal folder is needed later
-- so set a variable to this path
set bu_folder to choose folder --alias "Macintosh HD:Users:kel:Desktop:Main Folder:"
-- "Powerbook:Users:dou:Documents:script tutorial:daily intake:"
set backup_files to (list folder bu_folder without invisibles)
-- remove dot files if any
repeat with this_name in backup_files
if this_name begins with "." then
set backup_files to rest of backup_files
else
exit repeat
end if
end repeat
set item_count to count of backup_files
tell application "Finder"
repeat with i from 1 to item_count
set old_filename to item i of backup_files
if (contents of old_filename) does not start with "bu_" then
set new_filename to "bu_" & old_filename
-- create a reference to the file
set file_ref to ("" & bu_folder & old_filename) as alias
-- use the reference to the file to name it
set name of file_ref to new_filename
end if
end repeat
end tell
Instead of using ‘list folder’, you could just use the Finder. This eliminates the need to rid the list of dot files and creating references. Something like this:
set f to choose folder
tell application "Finder"
set file_list to (every file of f whose name does not start with "bu_")
repeat with this_file in file_list
set n to name of this_file
set name of this_file to ("bu_" & n)
end repeat
end tell
Using the Finder is simpler, but sometimes slower.
Edited: note that ‘list folder’ is good when you want all the items in the folder and don’t want to use the Finder.