Useful search and replace script

Posted on
Sun May 09, 2004 8:50 pm
gregjsmith offline
Posts: 946
Joined: Apr 01, 2003
Location: Rio Rancho, NM

Useful search and replace script

I found this handy script that's very useful for saving device parameters in the variable lists.

--[url=applescript://com.apple.scripteditor/?action=new&script=on%20searchReplace(theText,%20SearchString,%20ReplaceString)%0A%09set%20OldDelims%20to%20AppleScript's%20text%20item%20delimiters%0A%09set%20AppleScript's%20text%20item%20delimiters%20to%20SearchString%0A%09set%20newText%20to%20text%20items%20of%20theText%0A%09set%20AppleScript's%20text%20item%20delimiters%20to%20ReplaceString%0A%09set%20newText%20to%20newText%20as%20text%0A%09set%20AppleScript's%20text%20item%20delimiters%20to%20OldDelims%0A%09return%20newText%0Aend%20searchReplace]Click here to open this script in a new Script Editor window[/url].

on searchReplace(theText, SearchString, ReplaceString)
     set OldDelims to AppleScript's text item delimiters
     set AppleScript's text item delimiters to SearchString
     set newText to text items of theText
     set AppleScript's text item delimiters to ReplaceString
     set newText to newText as text
     set AppleScript's text item delimiters to OldDelims
     return newText
end searchReplace



I have several attachment scripts that I pass device names to. I need to keep track of some parameter of that device, like dim level for example. Problem with that is you cannot use spaces in the variable name. This script will allow you make variable friendly names.



Code: Select all
set UnitName to "Main Bedroom Lamp"
set NewUnitName to searchReplace(UnitName, " ", "_")
--Result: "Main_Bedroom_Lamp"


You can then create a variable based on NewUnitName. You can get variables using the same method.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 28 guests

cron