Wednesday, September 3, 2014

#1 2008-09-01 11:09:15 am

captainmac
Member
Registered: 2008-09-01
Posts: 3

Renaming files using "replace text in item names"

Hi all.

I'm using the Apple supplied script "Replace Text in Item Names.scpt" to rename a number of files within a specified directory. I modified the script to include default search & replace values. It works great and all but I'd like to try and take it a step further.

Question. Is there a way to attach this script to a folder so that anytime a file is dropped in the folder it executes this script and renames the files? I'd like it to do it automatically with no dialog boxes. It should search for "_ASP_400" or _ASP_800" and replace with "new text 1" or "new text 2". I'm trying to automate a production flow and this would help me very much.

Thanks very much.

Michael

Applescript:


--set the source_folder to choose folder with prompt "Folder containing items to edit:"

-- get the path to the folder of the front window
-- if no windows are open, the desktop folder will be used
try
   tell application "Finder" to set the source_folder to (folder of the front window) as alias
on error -- no open folder windows
   set the source_folder to path to desktop folder as alias
end try
display dialog "Search and replace in:" buttons {"File Names"} default button 1
set the search_parameter to the button returned of the result

repeat
   display dialog "Enter text to find in the item names:" default answer "_ASP_400" 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 "-disc001-file001-400kbps-400pixels" 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
*)

set the item_list to list folder source_folder without invisibles
set source_folder to source_folder as string
repeat with i from 1 to number of items in the item_list
   set this_item to item i of the item_list
   set this_item to (source_folder & this_item) as alias
   set this_info to info for this_item
   set the current_name to the name of this_info
   set change_flag to false
   if the current_name contains the search_string then
       if the search_parameter is "Folder Names" and ¬
           folder of this_info is true then
           set the change_flag to true
       else if the search_parameter is "File Names" and ¬
           folder of this_info is false then
           set the change_flag to true
       else if the search_parameter is "Both" then
           set the change_flag to true
       end if
       if the change_flag is true then
           -- replace target string using delimiters
           set AppleScript's text item delimiters to the search_string
           set the text_item_list to every text item of the current_name
           set AppleScript's text item delimiters to the replacement_string
           set the new_item_name to the text_item_list as string
           set AppleScript's text item delimiters to ""
           my set_item_name(this_item, new_item_name)
       end if
   end if
end repeat

beep 2

on set_item_name(this_item, new_item_name)
   tell application "Finder"
       --activate
       set the parent_container_path to (the container of this_item) as text
       if not (exists item (the parent_container_path & new_item_name)) then
           try
               set the name of this_item to new_item_name
           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
               tell me to display dialog the error_message default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
               copy the result as list to {new_item_name, button_pressed}
               if the button_pressed is "Skip" then return 0
               my set_item_name(this_item, new_item_name)
           end try
       else --the name already exists
           --beep
           tell me to display dialog "This name is already taken, please rename." default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
           copy the result as list to {new_item_name, button_pressed}
           if the button_pressed is "Skip" then return 0
           my set_item_name(this_item, new_item_name)
       end if
   end tell
end set_item_name

Last edited by captainmac (2008-09-01 11:22:21 am)


Filed under: Finder

Offline

 

#2 2008-09-01 11:44:37 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10490
Website

Re: Renaming files using "replace text in item names"

Hi,

of course you can rename the files automatically with a folder action.
The usual infinite loop problem doesn't matter in this case, because the file names will be filtered

Applescript:


on adding folder items to this_folder after receiving these_items
   repeat with oneItem in these_items
       set {folder:Fo, name:Nm} to info for oneItem
       if not Fo then
           if Nm contains "_ASP_400" then
               tell application "Finder" to set name of contents of oneItem to my replace_chars(Nm, "_ASP_400", "new text 1")
           else if Nm contains "_ASP_800" then
               tell application "Finder" to set name of contents of oneItem to my replace_chars(Nm, "_ASP_800", "new text 2")
           end if
       end if
   end repeat
end adding folder items to

on replace_chars(this_text, search_string, replacement_string)
   set {TID, text item delimiters} to {text item delimiters, search_string}
   set the item_list to every text item of this_text
   set text item delimiters to the replacement_string
   set this_text to the item_list as string
   set text item delimiters to TID
   return this_text
end replace_chars

Note: The script doesn't have any error handling if a file name already exists

Last edited by StefanK (2008-09-01 11:46:54 am)


regards

Stefan

Filed under: Finder

Offline

 

#3 2008-09-01 12:03:03 pm

captainmac
Member
Registered: 2008-09-01
Posts: 3

Re: Renaming files using "replace text in item names"

StefanK,

Thanks for your quick response. I'm new to this and have another question.

I've set up the folder where the files will be copied to. I've enabled folder actions and attached this script. When I drop files into that folder nothing happens. I'm sure there is something simple that I've overlooked but I'm not that experienced with applescript.

What am i missing?

Thanks again.

Michael

StefanK wrote:

Hi,

of course you can rename the files automatically with a folder action.
The usual infinite loop problem doesn't matter in this case, because the file names will be filtered

Applescript:


on adding folder items to this_folder after receiving these_items
   repeat with oneItem in these_items
       set {folder:Fo, name:Nm} to info for oneItem
       if not Fo then
           if Nm contains "_ASP_400" then
               tell application "Finder" to set name of contents of oneItem to my replace_chars(Nm, "_ASP_400", "new text 1")
           else if Nm contains "_ASP_800" then
               tell application "Finder" to set name of contents of oneItem to my replace_chars(Nm, "_ASP_800", "new text 2")
           end if
       end if
   end repeat
end adding folder items to

on replace_chars(this_text, search_string, replacement_string)
   set {TID, text item delimiters} to {text item delimiters, search_string}
   set the item_list to every text item of this_text
   set text item delimiters to the replacement_string
   set this_text to the item_list as string
   set text item delimiters to TID
   return this_text
end replace_chars

Note: The script doesn't have any error handling if a file name already exists


Filed under: Finder

Offline

 

#4 2008-09-01 12:06:41 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10490
Website

Re: Renaming files using "replace text in item names"

The script must be in (~)/Library/Scripts/Folder Action Scripts


regards

Stefan

Offline

 

#5 2008-09-01 12:14:01 pm

captainmac
Member
Registered: 2008-09-01
Posts: 3

Re: Renaming files using "replace text in item names"

Ahhh. I knew it would be something simple. Works like a charm. Thank you very much.

Michael

StefanK wrote:

The script must be in (~)/Library/Scripts/Folder Action Scripts

Last edited by captainmac (2008-09-01 12:14:16 pm)

Offline

 

#6 2008-09-03 02:35:10 pm

m7tran
Member
Registered: 2008-09-03
Posts: 3

Re: Renaming files using "replace text in item names"

Hello All,

By searching for Applescripts Search and Replace, I got to this site. Is there a script out there to search and replace FOLDER name and it searchs  (and replaces) recursively?

I am about to rename thousands of folders/files that have the "/" on the name that cannot be read properly by Windows users. A script would help saving time a lot instead of doing the rename manually. Apple's buit-in "Replace Text in Item Names" does not do recursive.

Thank you in advance,
Michael

Offline

 

#7 2008-09-03 03:50:22 pm

m7tran
Member
Registered: 2008-09-03
Posts: 3

Re: Renaming files using "replace text in item names"

Never mind :-). I found a script like one below and it worked like a charm. Thank you anyway!


tell application "Finder" to set the source_folder to choose folder
my handleFolder(source_folder)
to handleFolder(source_folder) -- this handler burrows down the folder heirarchy
    set filelist to source_folder
end handleFolder
to handleFiles(myfilelist) -- this performs the file operations
    repeat with fileToProcess in source_folder
    end repeat
end handleFiles
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 "" 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 (smile." 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


set dd to {source_folder}
repeat until my dd = {}
    set source_folder to my dd's item 1
    set dd to my dd's rest
    set the item_list to list folder source_folder without invisibles
    set source_folder to source_folder as string
    repeat with i from 1 to number of items in the item_list
        set this_item to item i of the item_list
        set this_item to (source_folder & this_item) as alias
        set this_info to info for this_item
        if folder of this_info then set end of my dd to this_item
        set the current_name to the name of this_info
        set change_flag to false
        if the current_name contains the search_string then
            if the search_parameter is "Folder Names" and ¬
                folder of this_info is true then
                set the change_flag to true
            else if the search_parameter is "File Names" and ¬
                folder of this_info is false then
                set the change_flag to true
            else if the search_parameter is "Both" then
                set the change_flag to true
            end if
            if the change_flag is true then
                -- replace target string using delimiters
                set AppleScript's text item delimiters to the search_string
                set the text_item_list to every text item of the current_name
                set AppleScript's text item delimiters to the replacement_string
                set the new_item_name to the text_item_list as string
                set AppleScript's text item delimiters to ""
                my set_item_name(this_item, new_item_name)
            end if
        end if
    end repeat
end repeat
beep 2

on set_item_name(this_item, new_item_name)
    tell application "Finder"
        --activate
        set the parent_container_path to (the container of this_item) as text
        if not (exists item (the parent_container_path & new_item_name)) then
            try
                set the name of this_item to new_item_name
            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 (smile."
                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
                tell me to display dialog the error_message default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
                copy the result as list to {new_item_name, button_pressed}
                if the button_pressed is "Skip" then return 0
                my set_item_name(this_item, new_item_name)
            end try
        else --the name already exists
            --beep
            tell me to display dialog "This name is already taken, please rename." default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
            copy the result as list to {new_item_name, button_pressed}
            if the button_pressed is "Skip" then return 0
            my set_item_name(this_item, new_item_name)
        end if
    end tell
end set_item_name


***************

Michael Tran


Filed under: Finder

Offline

 

#8 2008-09-03 03:52:37 pm

m7tran
Member
Registered: 2008-09-03
Posts: 3

Re: Renaming files using "replace text in item names"

Please note that the (smile actually is ( then : then )    wink

Last edited by m7tran (2008-09-03 03:52:54 pm)

Offline

 

#9 2008-10-16 12:38:00 am

jwoo
Member
Registered: 2008-10-16
Posts: 1

Re: Renaming files using "replace text in item names"

m7tran- Thx much for your work on this, I have been trying to do something similar.  Copied the code you provided and it seems to only be recursive to one level deep, meaning it goes to folder/folder, but no deeper.  Did you have similar results or am I missing something? Thx JW

Offline

 

#10 2011-10-27 06:02:43 am

mfdxlreb
Member
Registered: 2011-10-27
Posts: 1

Re: Renaming files using "replace text in item names"

Does anyone have the original 'replace text in item names.scpt'

I can't find it anywhere and it would be really useful

many thanks

Offline

 

#11 2011-10-27 07:51:19 am

DJ Bazzie Wazzie
Member
From: the Netherlands
Registered: 2004-10-20
Posts: 1893

Re: Renaming files using "replace text in item names"

Applescript:

(*
Replace Text In Item Names

This script is designed to replace text in the names of specific files and/or folders in the front window of the desktop.
If no folder windows are open, the script will affect files and/or folders on the desktop.

Copyright © 2001–2007 Apple Inc.

You may incorporate this Apple sample code into your program(s) without
restriction. This Apple sample code has been provided "AS IS" and the
responsibility for its operation is yours. You are not permitted to
redistribute this Apple sample code as "Apple sample code" after having
made changes. If you're going to redistribute the code, we require
that you make it clear that the code was descended from Apple sample
code, but that you've made changes.
*)


--set the source_folder to choose folder with prompt "Folder containing items to edit:"

-- get the path to the folder of the front window
-- if no windows are open, the desktop folder will be used
try
   tell application "Finder" to set the source_folder to (folder of the front window) as alias
on error -- no open folder windows
   set the source_folder to path to desktop folder as alias
end try
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 "" 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

set the item_list to list folder source_folder without invisibles
set source_folder to source_folder as string
repeat with i from 1 to number of items in the item_list
   set this_item to item i of the item_list
   set this_item to (source_folder & this_item) as alias
   set this_info to info for this_item
   set the current_name to the name of this_info
   set change_flag to false
   if the current_name contains the search_string then
       if the search_parameter is "Folder Names" and ¬
           folder of this_info is true then
           set the change_flag to true
       else if the search_parameter is "File Names" and ¬
           folder of this_info is false then
           set the change_flag to true
       else if the search_parameter is "Both" then
           set the change_flag to true
       end if
       if the change_flag is true then
           -- replace target string using delimiters
           set AppleScript's text item delimiters to the search_string
           set the text_item_list to every text item of the current_name
           set AppleScript's text item delimiters to the replacement_string
           set the new_item_name to the text_item_list as string
           set AppleScript's text item delimiters to ""
           my set_item_name(this_item, new_item_name)
       end if
   end if
end repeat

beep 2

on set_item_name(this_item, new_item_name)
   tell application "Finder"
       --activate
       set the parent_container_path to (the container of this_item) as text
       if not (exists item (the parent_container_path & new_item_name)) then
           try
               set the name of this_item to new_item_name
           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
               tell me to display dialog the error_message default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
               copy the result as list to {new_item_name, button_pressed}
               if the button_pressed is "Skip" then return 0
               my set_item_name(this_item, new_item_name)
           end try
       else --the name already exists
           --beep
           tell me to display dialog "This name is already taken, please rename." default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
           copy the result as list to {new_item_name, button_pressed}
           if the button_pressed is "Skip" then return 0
           my set_item_name(this_item, new_item_name)
       end if
   end tell
end set_item_name


Kind regards

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.120 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)