Apple Script Running Very Slow - Please help

Hi All

I am a newbie to apple scripts. The requirements for my script is that
I have to rename new folders created inside a hot folder. The folders should be named with the current date (mm-dd-yyyy) which means that only on folder can be created for a day. Below is the script I have written. It runs very slow. If I create 2 folders one after the other it doesn’t work. But If I wait for a while between creating folders it works as expected. Is it a problem with my script. If not is there anyway I can make it work as expected. Any help is really appreciated.

on adding folder items to theFolder after receiving theItems
try
tell application “Finder” to repeat with anItem in theItems
–display dialog anItem as string
set this_name to the name of anItem
–display dialog this_name as string
if this_name = “untitled folder” then

			set month_ to the month of (current date) as integer
			set day_ to the day of (current date) as string
			set year_ to the year of (current date) as string
			set newName to month_ & "-" & day_ & "-" & year_ as string
			
			--set this_name to text returned of (display dialog "What do you want this folder named?" default answer "untitled folder")
			--set name of anItem to this_name
			
			--display dialog newName as string
			set name of anItem to newName
			if newName = name of anItem then
				display dialog "xxxxx"
			else
				display dialog "Folder already created for today"
				delete anItem
			end if
			
			
			--move anItem to newName
		end if
	end repeat
on error the error_message number the error_number
	display dialog "Folder action caused an error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
end try

end adding folder items to

Thanks

Hi, Kanag_k.

One of the problems with renaming items that have triggered that kind of folder action is that the change of name makes the system think another item’s been added, which retriggers the action. This causes various problems, including the time taken to finish. There are also a couple of things in your script that don’t seem to be happening the right order, which might be causing it to error.

If I’ve correctly understood what you want, your game plan could be:

  1. When the action’s triggered, decide what today’s newName is.
  2. If there’s only one added item and it has today’s name, it’s because the script’s been called again after the renaming the item. Do nothing.
  3. Otherwise, if the folder already contains an item with today’s name, display a message to that effect and delete the added items.
  4. Otherwise, loop through the added items, renaming any called “untitled folder” and deleting the others.
on adding folder items to theFolder after receiving theItems
	try
		set today to (current date)
		set month_ to the month of today as integer
		set day_ to the day of today
		set year_ to the year of today
		-- This bit creates a string with the necessary leading zeros.
		tell (year_ * 10000 + month_ * 100 + day_) as string
			set newName to text 5 thru 6 & "-" & text 7 thru 8 & "-" & text 1 thru 4 -- mm-dd-yyyy
			-- set newName to text 1 thru 4 & "-" & text 5 thru 6 & "-" & text 7 thru 8 -- yyyy-mm-dd
		end tell
		
		tell application "Finder"
			activate
			if ((count theItems) is 1) and (name of item 1 of theItems is newName) then
				-- Do nothing. It's just the script retriggering after renaming the item.
			else if (item newName of theFolder exists) then
				display dialog "Folder already created for today" buttons {"OK"} default button 1
				delete theItems
			else
				repeat with thisItem in theItems
					if (name of thisItem is "untitled folder") then
						set name of thisItem to newName
						display dialog "xxxxx"
					else
						delete thisItem
					end if
				end repeat
			end if
		end tell
	on error the error_message number the error_number
		display dialog "Folder action caused an error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
	end try
end adding folder items to

Thanks a lot. Let me try to see if I can do anything about it.