How to replace a character in a string

Hello Powerusers


First i'me new in the applescripting so hope you can tell me a few hint to get on.

I Have been trying to find a way to replace characters in a string using applescript. but with no luck


Anybody having a script that can give me a clue on this.

I'me normally programming in windows.... yeah i know there is a first time for everything.

macbook pro, Mac OS X (10.6.3)

Posted on Dec 9, 2012 12:11 PM

Reply
Question marked as ⚠️ Top-ranking reply

Posted on Dec 9, 2012 12:35 PM

There are a few ways. One way is to use AppleScript's text item delimiters, another is to use one of the many shell script utilities.

on run -- example      display dialog quoted form of replaceText("This:is:some:test:text:", ":", space)      display dialog quoted form of (do shell script "echo \"" & "This:is:some:test:text:" & "\" | sed 's/:/ /g'") end run to replaceText(someText, oldItem, newItem)      (*      replace all occurances of oldItem with newItem           parameters -     someText [text]: the text containing the item(s) to change                     oldItem [text, list of text]: the item to be replaced                     newItem [text]: the item to replace with           returns [text]:     the text with the item(s) replaced      *)      set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, oldItem}      try           set {itemList, AppleScript's text item delimiters} to {text items of someText, newItem}           set {someText, AppleScript's text item delimiters} to {itemList as text, tempTID}      on error errorMessage number errorNumber -- oops           set AppleScript's text item delimiters to tempTID           error errorMessage number errorNumber -- pass it on      end try            return someText end replaceText

5 replies
Sort By: 
Question marked as ⚠️ Top-ranking reply

Dec 9, 2012 12:35 PM in response to MrJustJ

There are a few ways. One way is to use AppleScript's text item delimiters, another is to use one of the many shell script utilities.

on run -- example      display dialog quoted form of replaceText("This:is:some:test:text:", ":", space)      display dialog quoted form of (do shell script "echo \"" & "This:is:some:test:text:" & "\" | sed 's/:/ /g'") end run to replaceText(someText, oldItem, newItem)      (*      replace all occurances of oldItem with newItem           parameters -     someText [text]: the text containing the item(s) to change                     oldItem [text, list of text]: the item to be replaced                     newItem [text]: the item to replace with           returns [text]:     the text with the item(s) replaced      *)      set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, oldItem}      try           set {itemList, AppleScript's text item delimiters} to {text items of someText, newItem}           set {someText, AppleScript's text item delimiters} to {itemList as text, tempTID}      on error errorMessage number errorNumber -- oops           set AppleScript's text item delimiters to tempTID           error errorMessage number errorNumber -- pass it on      end try            return someText end replaceText

Reply

Dec 9, 2012 12:53 PM in response to MrJustJ

Red_menace has suggested the best method. Here's another one:


replace("iii", "a", "iiibriiiciiidiiibriii") --> "abracadabra"


on replace(A, B, theText)

set L to length of A

set K to L - 1

set P to offset of A in theText

repeat until P = 0

if P = 1 then

set theText to B & text (L + 1) through -1 of theText

else if P = (length of theText) - K then

set theText to text 1 through -(L + 1) of theText & B

else

set theText to text 1 through (P - 1) of theText & B & text (P + L) through -1 of theText

end if

set P to offset of A in theText

end repeat

return theText

end replace


See offset in the AppleScript Language Guide.


Message was edited by: Pierre L.

Reply

Dec 9, 2012 1:06 PM in response to red_menace

Okay i solved my problem but why arent the folder renamed?



propertyoldfolder : ""

property newfolder : ""

property SS : "."

property RS : " "


to replaceText(someText, oldItem, newItem)


(*

replace all occurances of oldItem with newItem

parameters - someText [text]: the text containing the item(s) to change

oldItem [text, list of text]: the item to be replaced

newItem [text]: the item to replace with

returns [text]: the text with the item(s) replaced

*)

set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, oldItem}

try

set {itemList, AppleScript's text item delimiters} to {text items of someText, newItem}

set {someText, AppleScript's text item delimiters} to {itemList as text, tempTID}

on error errorMessagenumbererrorNumber-- oops

set AppleScript's text item delimiters to tempTID

error errorMessagenumbererrorNumber-- pass it on

end try


return someText

end replaceText



tell application "Finder"

set these_items to the selection

end tell

repeat with i from 1 to the count of these_items

set this_item to (itemi of these_items) as alias

set this_info to info forthis_item

set oldfolder to name of this_info

set newfolder to replaceText(oldfolder, SS, RS)

set name of this_info to newfolder

end repeat

Reply

Dec 9, 2012 2:05 PM in response to MrJustJ

When you get info for anItem, it returns a record containing various information, such as the name. Later, you are setting a new value for the name property of this record, rather than the file item. Note that info for has been deprecated for a while, so you should avoid using it and use the Finder or System Events instead. Also note that the name of a file/folder includes the extension, so you might want to make sure you are dealing with just the name part.


propertySS : "."

propertyRS : " "


tellapplication"Finder"


repeatwithanItemin (gettheselection) -- step through each item of the selection


setnameofanItemtomyreplaceText(nameofanItem, SS, RS)


endrepeat

endtell


toreplaceText(someText, oldItem, newItem)


set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, oldItem}


try


set {itemList, AppleScript's text item delimiters} to {text itemsofsomeText, newItem}


set {someText, AppleScript's text item delimiters} to {itemListastext, tempTID}


onerrorerrorMessagenumbererrorNumber-- oops


setAppleScript's text item delimiterstotempTID


errorerrorMessagenumbererrorNumber-- pass it on


endtry



returnsomeText

endreplaceText

Reply

Dec 9, 2012 2:51 PM in response to MrJustJ

just to add to the noise, I keep this handler around to handle text items. the nice thing about it is that it converts both ways: if you give it a string it returns text items, if you give it a list it returns a string.


set t to "a.b.c.d.e"

set t to tid(t, ".") -- converts to a list of text items

set t to tid(t, " ") -- converts back to tex a list odf text items

-- result "a b c d e"


on tid(input, delim)

set {oldTID, my text item delimiters} to {my text item delimiters, delim}

if class of input is list then

set output to input as text

else

set output to text items of input

end if

set my text item delimiters to oldTID

return output

end tid

Reply

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 replace a character in a string

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