how to set name of folder to string in middle of original file name

I have a script that works great as shown below, but if I change line 13 from..


Open this Scriplet in your Editor:
set name of first_item to (text 1 thru 8 of theFile)

to


Open this Scriplet in your Editor:
set name of first_item to (text 2 thru 8 of theFile)

the script breaks.


Can someone please tell me why and/or how I can solve this?


Open this Scriplet in your Editor:
property dialog_timeout : 120 -- set the amount of time before dialogs auto-answer.

on adding folder items to this_folder after receiving dropped_items -- this_folder is an alias
  delay 5
  tell application "Finder"
    set this_folder_list to every folder of this_folder
    set this_file_list to every file of this_folder
    
    repeat with i in this_folder_list
      set first_item to item 1 of dropped_items -- name of entire path
      set theFile to (name of first_item)
      if kind of first_item is "folder" then
        set name of first_item to (text 1 thru 8 of theFile)
      end if
    end repeat
    
    set oldFiles to (every file of this_folder whose name starts with theFile)
    delete oldFiles
    
    delay 5
    
    repeat with i in this_folder_list
      set this_file_list to every file of i
      repeat with x in this_file_list
        set theFile to (name of x)
        set theFolderName to name of container of x
        set name of x to theFolderName & "_" & theFile
      end repeat
      
      set this_file_list_with_new_name to every file of i
      
      move this_file_list_with_new_name to this_folder
      
      delete i
      
      delay 5
      
      set unneededFiles to (every file of this_folder whose name extension is not in {"csv", "xml"})
      delete unneededFiles
      
    end repeat
  end tell
end adding folder items to


MacBook Pro 15″, OS X 10.11

Posted on Jul 4, 2021 3:39 PM

Reply
Question marked as Top-ranking reply

Posted on Jul 4, 2021 7:33 PM

When you say 'the script breaks', can you be more specific?


Normally if there's an error in the script, AppleScript will post an error message. That error message will give you some idea of what's wrong. For example, I could see the script failing if the filename in question had less than 8 characters in its name (can't get text 2 through 8 of "noname")


Other than that, your script may be doomed to fail anyway. You're changing the filenames in situ, meaning that the OS will see a new file added to the folder and will kick off the script again... which will (attempt to) rename the file... which will kick off the script again... etc.

Similar questions

1 reply
Question marked as Top-ranking reply

Jul 4, 2021 7:33 PM in response to nicpe

When you say 'the script breaks', can you be more specific?


Normally if there's an error in the script, AppleScript will post an error message. That error message will give you some idea of what's wrong. For example, I could see the script failing if the filename in question had less than 8 characters in its name (can't get text 2 through 8 of "noname")


Other than that, your script may be doomed to fail anyway. You're changing the filenames in situ, meaning that the OS will see a new file added to the folder and will kick off the script again... which will (attempt to) rename the file... which will kick off the script again... etc.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

how to set name of folder to string in middle of original file name

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.