Use AppleScript to remove a specific word/phrase from Multiple Folder names

Within one of my folders I have hundreds of Folders that have specific words or phrases in the names I would like to remove using apple script.


For instance if the folder names all contain the word "Default" what script could I write to remove "Default" from the names of all folders with it in the name.


So, if folder is named "bobDefault" how could I get it to remove Default and keep bob and then continue on the rest of the folder names within the main folder. I appreciate any help you may provide.

MacBook Pro with Retina display, OS X Yosemite (10.10)

Posted on Oct 25, 2014 5:17 AM

Reply
8 replies

Oct 25, 2014 6:54 AM in response to riotmike

The following script has been tested under Mavericks:


set theFolder to choose folder


tell application "Finder" to set theNames to name of folders of theFolder


set theNewNames to {}

repeat with thisName in theNames

set thisName to replace("default", "", thisName) -- replace "default" with ""

set thisName to replace(" ", " ", thisName) -- replace 2 spaces with 1 space

copy thisName to the end of theNewNames

end repeat


tell application "Finder" to repeat with k from 1 to (count theNames)

try

set name of folder (item k of theNames) of theFolder to (item k of theNewNames)

end try

end repeat


on replace(A, B, theText)

set {TID, AppleScript'stext item delimiters} to {AppleScript'stext item delimiters, {A}}

set {theTextItems, AppleScript'stext item delimiters} to {text items of theText, {B}}

set {theText, AppleScript'stext item delimiters} to {theTextItems as text, TID}

return theText

end replace

Oct 27, 2014 4:47 AM in response to riotmike

Thanks for your feedback. I'm glad I could help you.


There was a slight error in the script though. The 6th line should have been written like this:

set thisName to replace(space & space, space, thisName) -- replace 2 spaces with 1 space

However, that line was just for folder names like "Bob default bob" with a space before and after "default".

Actually, the error was not mine. It was due to the forum editor, which seems to automatically replace two

consecutive spaces with only one space.

Oct 27, 2014 10:51 PM in response to Pierre L.

Another questions Pierre, what about folders with dates within brackets, can I tell applescript to delete these.


So if I have folders named "Bob (1996)", "Tom (Old)" and "John (1986)" how could I have it remove the dates and brackets but not remove words within brackets.

So change to "Bob", "John" and leave "Tom (Old)" the same.

Thanks for any help pierre.

Oct 28, 2014 11:54 AM in response to riotmike

The following should do the trick (if there's always a space before the left parenthesis):


set theFolder to choose folder


tell application "Finder" to set theNames to name of folders of theFolder


set theNewNames to {}

repeat with thisName in theNames

set P1 to offset of "(" in thisName

set P2 to offset of ")" in thisName

if P1 * P2 > 0 then

try

get (text (P1 + 1) thru (P2 - 1) of thisName) as integer

set theString to text P1 thru P2 of thisName

set thisName to replace(space & theString, "", thisName)

on error

-- no change

end try

end if

copy thisName to the end of theNewNames

end repeat


tell application "Finder" to repeat with k from 1 to (count theNames)

try

set name of folder (item k of theNames) of theFolder to (item k of theNewNames)

end try

end repeat


on replace(A, B, theText)

set {TID, AppleScript'stext item delimiters} to {AppleScript'stext item delimiters, {A}}

set {theTextItems, AppleScript'stext item delimiters} to {text items of theText, {B}}

set {theText, AppleScript'stext item delimiters} to {theTextItems as text, TID}

return theText

end replace

Nov 6, 2014 8:38 AM in response to Pierre L.

Hi Pierre,

Recently, I have studied one of your former reply regarding control of System Preference using AppleScript. And after that, I intended to write a script simplifying the workflow of --click "Sharing" on the System PreferencePane-- Click "Internet Sharing" -- Click "Turn on Wi-Fi" -- Click "start". Here is my script which can be complied successfully :


tell application "System Preferences" to activate

tell application "System Events"

tell process "System Preferences"

tell menu bar 1

tell menu bar item "View"

delay 1

tell menu 1

click menu item "Sharing"

end tell

end tell

end tell

delay 1

tell window "Sharing"

click checkbox 1 of row 7 of table 1 of scroll area 1 of group 1

delay 1

if (exists sheet 1) then

if (exists button "Turn Wi-Fi On" of sheet 1) then

click button "Turn Wi-Fi On" of sheet 1

delay 1

if (exists button "Start" of sheet 1) then

click button "Start" of sheet 1

delay 1

end if

end if

else

do shell script "/usr/sbin/networksetup -setairportpower en1 off"

delay 1

end if

end tell


Unfortunately, when I run the script, it prompts a error saying :


error "System Events got an error: Can’t get window \"Sharing\" of process \"System Preferences\"." number -1728 from window "Sharing" of process "System Preferences"

However, if I run the script after activating the System Preference manually, it works perfectly. So I am here seeking for an solution to my problem, any suggestion would be appreciated ! Thank you ! By the way, I am running Mac OS X 10.09 Maverick now.


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.

Use AppleScript to remove a specific word/phrase from Multiple Folder names

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