Satimage Previous
change
Home Documentation Smile Text commands change  
change replaces text using standard strings or Regular expression patterns: to toggle into regular expression mode you specify change [...] with regexp. If needed, change will convert the direct parameter, the in parameter and the into parameter into Unicode text.
changereplace all occurrences of a substring
string -- the substring to search for
into string/Unicode text -- the replacement string
in anything -- a string or an alias
[starting at] integer -- default: 0
[for] integer -- length of text after starting
[case sensitive] boolean -- default true
[regexp] boolean -- default false
[whole word] boolean -- only without regexp. default false
[regexpflag] list of string -- a subset of {"IGNORECASE", "EXTEND", "MULTILINE", "SINGLELINE", "FIND LONGEST", "FIND NOT EMPTY", "DONT CAPTURE GROUP", "NOTBOL", "NOTEOL"}; default {}
[syntax] string -- the syntax for regular expressions. Relevant if regexp parameter is true. A string among ("POSIX" | "POSIX_EXTENDED" | "EMACS" | "GREP" | "GNU_REGEX" | "JAVA" | "PERL" | "RUBY"). Default: "RUBY"
Result: anything -- the new string if the "in" parameter is a string, otherwise the number of hits in the file
  • Be aware that, when the in parameter is an AppleScript variable, change does not really change its content. The changed string is the result that change returns: if your script does not store the result into a variable, the changed text is lost.
    set the_str to "hello"
    change "e" into "u" in the_str
    the_str
      --  "hello"
  • change accepts a list as its direct parameter. You must provide a list of the same size as the into parameter.
    change {"e", "o"} into {"E", "O"} in "hello world"
      --  "hEllO wOrld"
  • change can work on a list of strings: they accept a list as their in parameter.
    change "([0-9]{1,2})/([0-9]{1,2})" into "\\2/\\1" in {"31/12/2007", "29/2/2008"} with regexp
      -- {"12/31/2007", "2/29/2008"}
  • change accepts a reference to a file as their in parameter. In this case, changedoes change the file and returns the number of changes performed. change supports UTF-16 and UTF-8 files. However, if you use change on a file which is neither a valid UTF-16 nor UTF-8, change will assume it is a macintosh-encoded file.

  • change accepts a reference to a text window or a Unicode window of Smile as its in parameter.

    Using change on a text window may be slower than on a string but it preserves the styles. (change does not accept a list when working on windows).
Examples
  • The example below reads a file into a variable s, as ASCII text. Then it removes the leading spaces, then the trailing spaces, then it replaces by one tab any sequence of spaces optionally preceded by tab.

    set s to read f
    set s to change "^[ ]+" into "" in s with regexp
    set s to change "[ ]+$" into "" in s with regexp
    set s to change "\\t?[ ]+" into "\\t" in s with regexp

    This example demonstrates some frequently used Regular expressions metacharacters: ^ and $ stand respectively for the beginning and the end of a line, [...] is how one defines a class of characters (here the class restricts to the white space), + and ? specify respectively "1 or several" and "0 or 1" occurrences of the preceding character.

    You may want to note here that you define an AppleScript string by bracketing characters between double-quotes "...", and that, inside an AppleScript string, you escape the backslash and the double-quotes with backslash: "\\" and "\"".

  • The following example change all occurrences of "this" into "that" in all files of extensions "txt", in a chosen folder (and its subfolders).

    set {f} to navchoose folder
    set theFiles to list files f of extension "txt"
    repeat with i from 1 to (count theFiles)
          change "this" into "that" in (get item i of theFiles)
    end repeat

Copyright ©2008 Paris, Satimage