Question

I am trying to make a script to automate the renaming of all items on the desktop. Here is what I have so far:

tell application "Finder"
    activate
    set TheFolder to (path to desktop)
    set theFile to (every item in TheFolder) as text
    set name of item theFile to "This should be the new name"                     
end tell

I attempted it when I had only one item on the desktop, and it worked. However, after that, when I added just one more folder to the desktop (with nothing inside), this error appeared when I tried it:

Finder got an error: Can’t set item "Macintosh HD:Users:erictsai:Desktop:untitled folder :Macintosh HD:Users:erictsai:Desktop:untitled folder 1:" to "This should be the new name".

Anyone know how to fix this?

Was it helpful?

Solution

You're getting a list of items and then turning the list into a string, which is causing problems. You need to loop through that list and apply your name change to each item. Also, getting every item will include disks, which may not be what you want. You can also simplify a bit. Here's how you could do it if you don't want to change disk names:

tell application "Finder"
    repeat with finderObj in (items in desktop where class of it is not disk)
        -- Make whatever change you want to the name here
        set the name of finderObj to the name of finderObj & " test"
    end repeat
end tell

OTHER TIPS

every item in TheFolder returns a list, then you should go through the list with a loop:

tell application "Finder"
    activate
    set theFolder to (path to desktop)
    set theFiles to (every item in theFolder)
    repeat with aFile in theFiles
        set name of aFile to "This should be the new name"
    end repeat
end tell

The script will try to rename every item with the same name, so you'll get an error when renaming the second item.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top