Author Topic: Helpful handlers  (Read 1978 times)

0 Members and 1 Guest are viewing this topic.

Offline Publi-Script

  • Administrator
  • Hero Member
  • *****
  • Posts: 596
  • Karma: 16
  • Gender: Male
  • Automate your Life!
    • Publi-Script.com
Helpful handlers
« on: March 23, 2007, 01:02:15 PM »
Hi all,

Here is a few handlers that I find mysef including alot in my scripts. Use them and modify them as you wish.
Also, should you have some nifty little "helpers", feel free to share them here!


on FindReplace(FindWhat, ReplaceBy, inThisString)
     copy the text item delimiters to OldDelims
     set the text item delimiters to {FindWhat}
     set TempList to every text item of inThisString
     set the text item delimiters to {ReplaceBy}
     set NewString to TempList as text
     set the text item delimiters to OldDelims
     return NewString
end FindReplace

on GetTextItem(ThisString, ThisDelim, ThisItem)
     -- ThisString -> String to look in
     -- ThisDelim -> Text element that delimit the string
     -- ThisItem -> Number of the element to return or (From,To) indexes
     copy the text item delimiters to OldDelims
     set the text item delimiters to ThisDelim
     if class of ThisItem is list then
          set fromItem to (item 1 of ThisItem) as integer
          set toitem to (item 2 of ThisItem) as integer
          set arrItem to (text items fromItem thru toitem of ThisString)
     else
          set arrItem to every text item of ThisString
     end if
     set the text item delimiters to OldDelims
     
     
     if class of ThisItem is list then
          return arrItem as text
     else
          if ThisItem is not 0 then
               return (item ThisItem of arrItem) as text
          else
               return arrItem -- return every items
          end if
     end if
end GetTextItem

on PadNumber(varNum, VarSize)
     -- varNum -> Number to pad
     -- varSize -> Lenght of the padding (i.e. PadNumber(1,3) returns 001)
     copy every character of (varNum as text) to NumList
     copy length of NumList to curSize
     if curSize < VarSize then
          repeat (VarSize - curSize) times
               set NumList to "0" & NumList
          end repeat
     end if
     return NumList
end PadNumber


I believe their purpose to be self-explanatory but don't be afraid to ask if that is not the case.

Hope this helps!

Michel
« Last Edit: May 26, 2007, 08:49:17 AM by Publi-Script »
Michel Lemieux
Forum Administrator
-------------------------
OS X 10.6.4
Quark 6.5 / 7.5 / 8.1.6
Adobe Design Premium CS / CS2 / CS3 / CS4 / CS5
Script Debugger 4.5