Skip navigation

How to replace a character in a string

3446 Views 5 Replies Latest reply: Dec 9, 2012 2:51 PM by twtwtw RSS
MrJustJ Level 1 Level 1 (0 points)
Currently Being Moderated
Dec 9, 2012 12:11 PM

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)
  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    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
    
  • Pierre L. Level 4 Level 4 (3,860 points)
    Currently Being Moderated
    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.

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    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.

     

    property SS : "."

    property RS : " "

     

    tell application "Finder"

      repeat with anItem in (get the selection) -- step through each item of the selection

      set name of anItem to my replaceText(name of anItem, SS, RS)

      end repeat

    end tell

     

    to replaceText(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 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


  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    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

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.