Droplet to Rename Folders and Files

I am an AS newbie and I need some help. Our accounting software creates a folder structure on our server when a job is created with a 6 digit job number. Enclosed in this folder is a set of sub-folders which are generated with a generic name: 0000_Art, 0000_Final, 0000_Fonts etc. It also places a generic rtf file named 0000_Readme. When work begins on a job we currently have to replace the "0000" with the last four digits of the job number: "140662" = "0662_Art" etc.

I found a script to do this with some manual intervention but wanted to take this one step further by creating a droplet and making it drag and drop. This droplet works, but the problem comes in that it still wants you to choose the target folder from a dialog and I would like to just auto process the dropped folder and it's contents. Also, is there a way to tell AS to use characters 3-6 of the dropped folder's name to replace any instances of "0000" in sub-folder or document names enclosed? Is there any uneccessary code which can be stripped out?

Here is the script as it stands now:

on open of finderObjects
set the source_folder to (choose folder with prompt "Folder containing items to edit:") as Unicode text

display dialog "Search and replace in:" buttons {"File Names", "Folder Names", "Both"} default button 3
set the search_parameter to the button returned of the result

repeat
display dialog "Enter text to find in the item names:" default answer "0000" buttons {"Cancel", "OK"} default button 2
set the search_string to the text returned of the result
if the search_string is not "" then exit repeat
end repeat

repeat
display dialog "Enter replacement text:" default answer "" buttons {"Cancel", "OK"} default button 2
set the replacement_string to the text returned of the result
if the replacement_string contains ":" then
beep
display dialog "A file or folder name cannot contain a colon (:)." buttons {"Cancel", "OK"} default button 2
else if the replacement_string contains "/" then
beep
display dialog "A file or folder name cannot contain a forward slash (/)." buttons {"Cancel", "OK"} default button 2
else
exit repeat
end if
end repeat

display dialog "Replace “" & the search_string & "” with “" & the replacement_string & "” in every item name?" buttons {"Cancel", "OK"} default button 2

tell application "Finder"
-- Get a Finder reference to the relvant items.
if (search_parameter is "Folder Names") then
set item_reference to a reference to (folders of entire contents of folder source_folder whose name contains search_string)
else if (search_parameter is "File Names") then
set item_reference to a reference to (files of entire contents of folder source_folder whose name contains search_string)
else
set item_reference to a reference to (items of entire contents of folder source_folder whose name contains search_string)
end if
-- Get a list of aliases to the items.
-- (Individual Finder references might fail when renaming items within renamed folders.)
try
set item_list to item_reference as alias list
on error
set item_list to item_reference as alias as list
end try
if item_list is not {} then
-- If there are any relevant items, get their names.
set current_names to name of item_reference
-- Doctor each name...
repeat with i from 1 to (count current_names)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to search_string
set text_items to text items of (item i of current_names)
set AppleScript's text item delimiters to replacement_string
set new itemname to text_items as Unicode text
set AppleScript's text item delimiters to astid
-- ... and rename the associated item.
my set itemname(item i of item_list, new itemname)
end repeat
end if
end tell
end open
beep 2
on set item_name(thisitem, new itemname)
tell application "Finder"
--activate
set the parent_container to (the container of this_item)
if not (exists item new itemname of the parent_container) then
try
set the name of this_item to new itemname
on error the error_message number the error_number
if the error_number is -59 then
set the error_message to "This name contains improper characters, such as a colon (:)."
else --the suggested name is too long
--set the error_message to error_message -- "The name is more than 31 characters long."
end if
--beep
set new itemname to my get new_name(errormessage, new itemname)
if (new itemname is 0) then return 0
my set item_name(thisitem, new itemname)
end try
else --the name already exists
--beep
set new itemname to my get newname("This name is already taken, please rename.", new itemname)
if (new itemname is 0) then return 0
my set item_name(thisitem, new itemname)
end if
end tell
end set itemname

on get newname(msg, default_answer)
tell application (path to frontmost application as Unicode text)
set {text returned:new itemname, button returned:button_pressed} to (display dialog msg default answer default_answer buttons {"Cancel", "Skip", "OK"} default button 3)
if (button_pressed is "OK") then
return new itemname
else if (button_pressed is "Skip") then
return 0
else
error number -128 -- only necessary on non-English systems.
end if
end tell
end get newname


Various Mixed Macs Mac OS X (10.4.3)

Various Mixed Macs Mac OS X (10.4.3)

Posted on Jan 4, 2006 9:18 PM

Reply
10 replies

Jan 5, 2006 12:25 AM in response to lunarlandr

Yep, too many display dialog.

This will replace name of every items in the dropped folder whose name begins with "0000_" with characters 3 thru 6 of the name of the dropped folder (and keep the last part intact). Give it a test try first, be sure it works as you want.

on open _selection
repeat with _item in _selection -- you can drag-n-drop multiple folders
tell application "Finder"
if kind of _item is "folder" then -- make sure the item is a folder
set folder_name to name of _item -- get folder name, ie: 140662
set new_name to characters 3 thru 6 of folder_name as text -- get character 3-6 as you ask. ie: 0662
set the_items to (items of entire contents of _item whose name starts with "0000_") -- get items start with "0000_", including Readme
repeat with _item in the_items -- change their names
set the_name to (name of _item)
set name of _item to (new_name & (characters 5 thru -1 of the_name) as text)
end repeat
end if
end tell
end repeat
end open

Jan 5, 2006 12:54 AM in response to lunarlandr

Hi lunarlandr and welcome to Apple Discussions!

Without going through the script in detail, there's a lot that can be stripped out, and a few goodies that can be added.

Droplets work best when iterating through lists (even one-item lists) of Finder items. As a brief example:

on open (these_objects)
repeat with each_object in these_objects
--do something with each object in turn
end repeat
end open

You need to remove the line beginning "choose folder" and let each_object become your source folder.

The code below is rough and may need some amendment, but it should answer most of your questions. Test it and see.

--begin script
on run
display dialog "I'm a droplet! Drop things on me!" buttons {"OK"} default button 1 with icon note
--although you could incorporate a choose folder here
end run

on open (these_objects) -- a list of aliases
repeat with each_object in these_objects
set item_info to info for each_item
set folder_contents to (list folder item_info)
if kind of item_info is "Folder" and folder_contents contains "0000_Readme" then -- only process job folders
set job_number to text 3 thru 6 of name of item_info
process (each object,jobnumber)
else --not a job folder
display dialog "I only work with proper job folders." buttons {"OK"} with icon note giving up after 3 -- move on to next item in list
end if
end open

on process (top folder,jobnumber)
tell application "Finder"
set folder_contents to every item of top_folder
repeat with each_item in folder_contents
set generic_name to name of each_item
if generic_name begins with "0000_" then
set name of each_item to job_number & text 5 thru -1 of generic_name --everything from the underscore to the end
end if
end repeat
end tell
end process
--end script

The droplet will process multiple folders with different job numbers, without any user interaction. The name of the internal files and folders is derived from the name of the dropped folder.

Hope it helps,

(Although I don't see why the accounting software shouldn't do all of this for you...)

H

Jan 5, 2006 10:48 AM in response to lunarlandr

Thanks for the speedy replies. I tried both options from the first 2 replies with no success. The first option which appeared very simple and elegant produced no result and also no errors. The second option provided gave an error which stated that "Each_item" is not defined. I located the instance and changed it to "each_object" (derived from the syntax above) and recompiled. Now I recieved a lengthy error message regarding file creation and modification dates etc, all of which I have no desire to modify.

The script I posted works, but with too much user interaction. Can this just be modified in some way? Any takers?

As to why our accounting software can't handle this, it is administered by accountants not IT proffesionals. They can't fix it, but they can tell you how much it would cost to hire someone to fix it.

Jan 5, 2006 1:53 PM in response to lunarlandr

Sincere apologies, lunarlandr. There were a couple of really stupid mistakes in my original script which I should have checked and corrected before I posted.

Try this amended version:

--begin script
on run
display dialog "I'm a droplet! Drop things on me!" buttons {"OK"} default button 1 with icon note
--although you could incorporate a choose folder here
end run

on open (these_objects) -- a list of aliases
repeat with each_object in these_objects
set item_info to info for each_object
set folder_contents to (list folder each_object)
if kind of item_info is "Folder" and folder_contents contains "0000_Readme" then -- only process job folders
set job_number to text 3 thru 6 of name of item_info
process(each_object, job_number)
else --not a job folder
display dialog "I only work with proper job folders." buttons {"OK"} with icon note giving up after 3 -- move on to next item in list
end if
end repeat
end open

on process(top_folder, job_number)
tell application "Finder"
set folder_contents to every item of top_folder
repeat with each_item in folder_contents
set generic_name to name of each_item
if generic_name begins with "0000_" then
set name of each_item to job_number & text 5 thru -1 of generic_name --everything from the underscore to the end
end if
end repeat
end tell
end process
--end script

If that doesn't work, take me out and shoot me...

Jan 5, 2006 9:07 PM in response to Cyclosaurus

This is what the folder structure looks like. All are folders except the 2 items with the extensions after them.


140669
-----Control
-------------Job.apa
-----DigitalPrint
-----Fonts
-----HotFolders
-------------Processed
-----System
-------------BypassParts
-------------ForDeletion
-------------ImpositionPlans
-------------LocalImages
-------------PreflightLocalizedReports
-------------PreflightReports
-------------SidelinedFonts
-------------SidelinedImages
-------------SubPages
-------------SubPagesWorking
-------------SubPageThumbnails
-------------SubPageThumbnailsWorking
-----UserDefinedFolders
-------------0000_Art
-------------0000_Final
-------------0000_Fonts
-------------0000_Original
-------------0000_PreFlight
-------------0000_PrepsTemplates
-------------0000_Postscript
-------------0000_Scans
-------------0000_Templates
-------------Hires
-------------Read Me
--------------------0000_Readme.rtf
-----WebDownloads
-----WebUploads

Hope this helps.


LL





Various Mixed Macs Mac OS X (10.4.3)

Various Mixed Macs Mac OS X (10.4.3)

Jan 5, 2006 11:16 PM in response to lunarlandr

Your folder structure is quite different than I thought, however there is only a small change is needed.
This is one of the bug in AS when drilling down more than one level, as discuss in this thread: http://discussions.apple.com/thread.jspa?threadID=278173&tstart=0

Give this a try, the script replaces "0000" with character 3-6 of the name of the dropped folder:

on open _selection
repeat with _item in _selection -- you can drag-n-drop multiple folders
tell application "Finder"
if kind of _item is "folder" then -- make sure the item is a folder
set folder_name to name of _item -- get folder name, ie: 140662
set new_name to characters 3 thru 6 of folder_name as text -- get character 3-6 as you ask. ie: 0662
set the_items to (items of entire contents of _item whose name starts with "0000") as alias list -- get items start with "0000", including Readme
repeat with _item in the_items -- change their names
set the_name to (name of _item)
set name of _item to (new_name & (characters 5 thru -1 of the_name) as text)
end repeat
end if
end tell
end repeat
end open

Jan 6, 2006 5:13 PM in response to Cyclosaurus

H,

No luck with the revisions to your solution. Who do i bill for the bullet?

Cyc,

As it turns out there was a problem with the Mac I was testing on. I tried both your and H's scripts at home and your's worked, his did not. I also learned a few lessons about syntax and got my original script working as well. Now I have a manual script to change variable data and a full-auto script perfectly suited to it's intended purpose. I ran Applejack on the problem Mac and now it works there also.

Much Thanks to Both of You

LL

Various Mixed Macs Mac OS X (10.4.3)

Jan 6, 2006 6:48 PM in response to lunarlandr

LL,

Glad to hear that it worked.
Regarding HD's script, it doesn't work because of this:

if kind of item_info is "Folder" and folder_contents contains "0000_Readme" then -- only process job folders


Since "0000_Readme" is actually "0000_Readme.rtf" and it is 2 level down from the top.

That was the reason that I asked for the exact sample of your folder structure and sub items names, although, it is not really needed since I use entire contents of the top folder. 🙂

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.

Droplet to Rename Folders and Files

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