Advertisement
rccharles

common on blocks

Mar 9th, 2021
4,720
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (* =================== Subroutines ================== *)
  2. -- Required by debug routine.  
  3. global debugRunning
  4. set debugRunning to ""
  5.  
  6. -- ------------------------------------------------------
  7. (*
  8. alterString
  9.   thisText is the input string to change
  10.   delim is what string to change.  It doesn't have to be a single character.
  11.   replacement is the new string
  12.  
  13.   returns the changed string.
  14. *)
  15.  
  16. on alterString(thisText, delim, replacement)
  17.     set resultList to {}
  18.     set {tid, my text item delimiters} to {my text item delimiters, delim}
  19.     try
  20.         set resultList to every text item of thisText
  21.         set text item delimiters to replacement
  22.         set resultString to resultList as string
  23.         set my text item delimiters to tid
  24.     on error
  25.         set my text item delimiters to tid
  26.     end try
  27.     return resultString
  28. end alterString
  29.  
  30. -- ------------------------------------------------------  
  31. (*
  32. *)
  33. on appendToFile(fileId, theData)
  34.    
  35.     local theSize, writeWhere
  36.    
  37.     set theSize to (get eof fileId)
  38.     set writeWhere to theSize + 1 as integer
  39.     write theData to fileId starting at writeWhere
  40.    
  41. end appendToFile
  42.  
  43. -- ------------------------------------------------------  
  44. (*
  45.  debug(<string>)
  46.  Write messages to a log file.
  47.  
  48.   -- Need to place these two lines in the calling routine.
  49.     global debugRunning
  50.     set debugRunning to ""
  51.  -- references appendToFile()  
  52.   -- example:
  53.     debug("start program. Reading from " & listOfFiles)
  54.    
  55.    found here: /Users/mac/Documents/BJ\ Prior\ Years/BJ2004/sendmailapp2\ copy
  56.  
  57. *)
  58. on debug(theMessage)
  59.     -- return
  60.     global debugRunning
  61.     local theSize, startupDiskName, pathToLog, fileReference
  62.    
  63.     set pathToLog to (path to home folder as text) & "tryAttachmentsLog.txt"
  64.     -- log "pathToLog is " & pathToLog
  65.     -- display dialog "pathToLog is " & pathToLog giving up after 4
  66.    
  67.     try
  68.         -- Complete the path.
  69.         set pathToLog to pathToLog as text
  70.         set fileReference to (open for access file pathToLog ¬
  71.             with write permission)
  72.        
  73.         if debugRunning = "" then
  74.             set theSize to (get eof fileReference)
  75.             if theSize > 0 then
  76.                 appendToFile(fileReference, " " & return)
  77.             end if
  78.             appendToFile(fileReference, "   --- debug on " & ((current date) as string) & "   --- " & return)
  79.             set debugRunning to "running"
  80.         end if
  81.         -- log "theMessage " & theMessage
  82.         -- display dialog "in debug..." & return & "theMessage " & theMessage giving up after 3
  83.         appendToFile(fileReference, theMessage & return)
  84.        
  85.         close access fileReference
  86.         tell application "Finder"
  87.             set the creator type of the file pathToLog ¬
  88.                 to "R*ch"
  89.         end tell
  90.     on error mes number n
  91.         try
  92.             set commonErr to "error ... " & mes & " error number is " & n
  93.             log commonErr
  94.             close access fileReference
  95.             display dialog commonErr giving up after 4
  96.         end try
  97.        
  98.     end try
  99.     -- log "end of debug"
  100. end debug
  101.  
  102. -- ------------------------------------------------------
  103. (*
  104. write log message to script editor log and to our file log
  105. *)
  106. on debugLog(theMessage)
  107.     log "debugLog: " & theMessage
  108.     return debug(theMessage)
  109. end debugLog
  110.  
  111. -- ------------------------------------------------------  
  112. (*  
  113. A sub-routine for encoding ASCII characters.  
  114.  
  115. encode_char("$")  
  116. --> returns: "%24"  
  117.  
  118. based on:  
  119. https://www.macosxautomation.com/applescript/sbrt/sbrt-08.html  
  120.  
  121. alternative way
  122. https://stackoverflow.com/questions/11085654/apple-script-how-can-i-copy-html-content-to-the-clipboard
  123. set clipboardDataQuoted to quoted form of clipboardData
  124.     log "quoted form is " & clipboardDataQuoted
  125.     set toUnix to "/bin/echo -n " & clipboardDataQuoted & " | hexdump -ve '1/1 \"%.2x\"'"
  126.  
  127. *)
  128.  
  129.  
  130. on encodeCharactersInHex(theCharacters)
  131.     set charactersTranslated to ""
  132.     repeat with i from 1 to length of theCharacters
  133.         set this_char to text (i) thru i of theCharacters
  134.         set the ASCII_num to (the ASCII number this_char)
  135.         set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
  136.         set x to item ((ASCII_num div 16) + 1) of the hex_list
  137.         set y to item ((ASCII_num mod 16) + 1) of the hex_list
  138.         set charactersTranslated to charactersTranslated & (x & y) as string
  139.     end repeat
  140.    
  141.     return charactersTranslated
  142.    
  143. end encodeCharactersInHex
  144.  
  145. -- ------------------------------------------------------  
  146. (*
  147.   http://krypted.com/mac-os-x/to-hex-and-back/
  148. *)
  149. on hexToString(hex)
  150.     log "in hexToString"
  151.     log "hex string is " & hex
  152.     set toUnix to "echo " & hex & " | xxd -r -p "
  153.     log "toUnix is " & toUnix
  154.     try
  155.         set fromUnix to do shell script toUnix
  156.         log "fromUnix is " & fromUnix
  157.     on error errMsg number n
  158.         log "convert hex string to string failed. " & errMsg & " with number " & n
  159.     end try
  160. end hexToString
  161.  
  162. -- ------------------------------------------------------  
  163. (*
  164.   ideas from:
  165.   https://stackoverflow.com/questions/3469389/applescript-testing-for-file-existence
  166.  
  167.  
  168.  use the alias way.  Others ways ran into trouble eventually.
  169. *)
  170. on fileExists(theFile) -- (String) as Boolean
  171.     (* "System Events" and "Finder" checking for file existance revealed problems. l*)
  172.     set debugging to false
  173.     if debugging then log "  fileExists: theFile is " & theFile
  174.     try
  175.         set theAlias to theFile as alias
  176.         set theExistance to true
  177.     on error errMsg number n
  178.         if debugging then log "  fileExists: n is " & n
  179.         -- File or folder doesn't exist.
  180.         if n is not -43 then
  181.             set commonError to "on error the errMsg is " & errMsg & " number is " & n
  182.             if debugging then log "  fileExists: " & commonError
  183.             display dialog commonError giving up after 10
  184.             -- cause grief above.
  185.             error "Failure of alias." number -1
  186.         else
  187.             set theExistance to false
  188.         end if
  189.     end try
  190.     if debugging then log "  fileExists: theExistance is " & theExistance
  191.     return theExistance
  192. end fileExists
  193.  
  194. -- ------------------------------------------------------  
  195. (*
  196. has problems, see above.
  197.   Philip Regan
  198.   https://stackoverflow.com/questions/3469389/applescript-testing-for-file-existence
  199.  
  200. on fileExists(theFile) -- (String) as Boolean
  201.     tell application "System Events"
  202.         if exists file theFile then
  203.             return true
  204.         else
  205.             return false
  206.         end if
  207.     end tell
  208. end fileExists
  209. *)
  210.  
  211. -- ------------------------------------------------------  
  212. (*
  213. Yvan Koenig
  214. https://macscripter.net/viewtopic.php?id=43133
  215. *)
  216. on findExtension(fileName)
  217.     set saveTID to AppleScript's text item delimiters
  218.     set AppleScript's text item delimiters to {"."}
  219.     set theExt to last text item of fileName
  220.     set AppleScript's text item delimiters to saveTID
  221.     --log "theExt is " & theExt
  222.     if theExt ends with ":" then set theExt to text 1 thru -2 of theExt
  223.     --log "theExt is " & theExt
  224.     return theExt
  225. end findExtension
  226.  
  227.  
  228. -- ------------------------------------------------------
  229. (*
  230.   find free name in source and target folder
  231. *)
  232. on findFreeName(sourcePath, targetPath, fileName)
  233.     log "findFreeName: sourcePath is " & (sourcePath as text) & " sourcePath is " & (targetPath as text) & "  fileName is " & fileName
  234.     log "sourcePath of class = " & class of sourcePath & "sourcePath of class = " & class of sourcePath & "fileName of class = " & class of fileName
  235.     set longWait to 20
  236.    
  237.     -- set new_name to (prefix & "--" & characters 1 through (-2 - extension_length) of currentName) & "--" & companyCode & "." & name extension of file thisItem as text
  238.    
  239.    
  240.    
  241.     set clearCount to 0
  242.     set theExtension to my findExtension(fileName)
  243.     tell application "Finder"
  244.         set justName to characters 1 through (-2 - (length of theExtension)) of fileName
  245.     end tell
  246.     log "findFreeName: theExtension is " & theExtension & "; justName is " & justName
  247.    
  248.     set sourceFile to (sourcePath as text) & fileName
  249.     set targetFile to (targetPath as text) & fileName
  250.     log "sourceFile is " & sourceFile & "; targetFile is " & targetFile
  251.     try
  252.        
  253.         repeat while my fileExists(sourceFile) or fileExists(targetFile)
  254.             log "findFreeName: looping" & " clearCount is " & clearCount
  255.             if clearCount > 3 then
  256.                 set commonError to "findFreeName: could not find free name when searching for  "
  257.                 set commonError to commonError & fileName & ". clearCount is " & clearCount & " clean up  folders. "
  258.                 set commonError to commonError & return & (sourceFile as text) & return & (targetFile as text)
  259.                 my debugLog(commonError)
  260.                
  261.                 -- have to throw an error to get out of this repeat. Could have set a switch I guess.
  262.                 display dialog commonError giving up after longWait
  263.                 error "cannot find free file name" number 8110
  264.             end if
  265.             set clearCount to clearCount + 1
  266.             log "findFreeName: clearCount is " & clearCount
  267.             set fileName to justName & " #"
  268.             log "findFreeName: fileName is " & fileName
  269.             set fileName to fileName & (clearCount as text)
  270.             log "findFreeName: fileName is " & fileName
  271.             set fileName to fileName & "."
  272.             log "findFreeName: fileName is " & fileName
  273.             set fileName to fileName & (theExtension as text)
  274.             log "findFreeName: fileName is " & fileName
  275.            
  276.             set sourceFile to (sourcePath as text) & fileName
  277.             set targetFile to (targetPath as text) & fileName
  278.             log "sourceFile is " & sourceFile & "; targetFile is " & targetFile
  279.             -- my debugLog("findFreeName: searching for free name. " & fileName & " clearCount is " & clearCount & return & " destPath is " & destPath)
  280.         end repeat -- while  
  281.     on error errMsg number n
  282.         log "error of some type"
  283.         --display dialog errMsg & " with " & n
  284.         if n is not 8110 then
  285.             set displayFileName to fileName as text
  286.             set commonError to "findFreeName: attempting to create free filename " & displayFileName & return & " on error the errMsg is " & errMsg & " number is " & n
  287.             display dialog commonError giving up after longWait
  288.             my debugLog(commonError)
  289.             return "" -- hopefully this will never happen.  Haven't implement extensiver error recovery.
  290.         else
  291.             error "cannot find free file name. returning." number 8110
  292.            
  293.         end if
  294.        
  295.     end try
  296.     log "findFreeName: return from findFreeName is " & fileName
  297.     return fileName
  298.    
  299. end findFreeName
  300.  
  301.  
  302. -- ------------------------------------------------------
  303. (*
  304.    Yvan Koenig
  305.    https://macscripter.net/viewtopic.php?id=43133
  306.    with mods for no extension present
  307.    
  308. *)
  309. on getExt(theName)
  310.     if (offset of "." in theName) is greater than 0 then
  311.         set saveTID to AppleScript's text item delimiters
  312.         set AppleScript's text item delimiters to {"."}
  313.         set theExt to last text item of theName
  314.         set AppleScript's text item delimiters to saveTID
  315.         if theExt ends with ":" then set theExt to text 1 thru -2 of theExt
  316.     else
  317.         set theExt to ""
  318.     end if
  319.     return theExt
  320. end getExt
  321.  
  322. -- ------------------------------------------------------
  323. (*
  324. length of inputLfBuffer & " in hex " & integerToHex(length of inputLfBuffer)
  325. *)
  326. on getIntegerAndHex(aNumber)
  327.     global debug
  328.     if debug ≥ 5 then log "in ~~~ getIntegerAndHex ~~~"
  329.    
  330.     return aNumber & " in Hex " & integerToHex(aNumber)
  331. end getIntegerAndHex
  332.  
  333. -- ------------------------------------------------------
  334. (*
  335.     Input: a file with or without an extension.
  336.     hhas
  337.     https://forums.macrumors.com/threads/applescript-to-get-file-name.927338/
  338.    
  339.     may not work with folders with extensions like apps.
  340.    
  341.     Test cases:
  342.                 set fileName to " "                  
  343.                 set fileExt to my getExt(attachmentName)
  344.                 log "fileName  is " & fileName & " fileExt  is " & fileExt
  345.                 set fileExt to my getExt("testfileNo")
  346.                 log "fileName  is " & fileName & " fileExt  is " & fileExt
  347.                 set fileExt to my getExt("path:to:testfileNo:")
  348.                 log "fileName  is " & fileName & " fileExt  is " & fileExt
  349.                 log ">>>>>>>>>>>>>>>>>>>>>>"
  350.                 set {fileName, fileExt} to my getNameExt(attachmentName)
  351.                 log "fileName  is " & fileName & " fileExt  is " & fileExt
  352.                 set {fileName, fileExt} to my getNameExt("testfileNo")
  353.                 log "fileName  is " & fileName & " fileExt  is " & fileExt
  354.                 set {fileName, fileExt} to my getNameExt("path:to:testfileNo:")
  355.                 log "fileName  is " & fileName & " fileExt  is " & fileExt
  356. *)
  357. on getNameExt(fileName)
  358.     set saveTID to AppleScript's text item delimiters
  359.     set AppleScript's text item delimiters to "."
  360.     if fileName contains "." then
  361.         set {displayName, nameExt} to {text 1 thru text item -2, text item -1} of fileName
  362.     else
  363.         set {displayName, nameExt} to {fileName, ""}
  364.     end if
  365.     set AppleScript's text item delimiters to saveTID
  366.    
  367.     return {displayName, nameExt}
  368. end getNameExt
  369.  
  370. (*
  371.   http://krypted.com/mac-os-x/to-hex-and-back/
  372.                0    2    4    6    8    a    c    e     0 2 4 6 8 a c e
  373. 0000000:   3c 703e 5369 6d70 6c65 2070 7574 2c20   <p>Simple put,
  374.             *)
  375. on hexDumpFormatOne(textMessage, hex)
  376.     global debug
  377.    
  378.     set aNul to character id 1
  379.    
  380.     if debug ≥ 7 then log "in ~~~ hexDumpFormatOne ~~~"
  381.     if debug ≥ 8 then log "    hexDumpFormatOne: input string is " & return & hex
  382.    
  383.     -- -r -p
  384.     set displayValue to aNul & hex
  385.     set toUnix to "/bin/echo -n " & (quoted form of displayValue) & " | xxd  "
  386.     if debug ≥ 8 then log "    hexDumpFormatOne: toUnix is " & toUnix
  387.    
  388.     try
  389.         set fromUnix to do shell script toUnix
  390.        
  391.         -- two hex digits
  392.         set displayText to replaceCharacter(fromUnix, 10, "  ")
  393.         if debug ≥ 8 then
  394.             log "    hexDumpFormatOne: " & return & displayText
  395.             log "    hexDumpFormatOne: length of displayText is " & length of displayText
  396.         end if
  397.         -- one character
  398.         set displayText to replaceCharacter(displayText, 51, " ")
  399.         if debug ≥ 8 then
  400.             log "    hexDumpFormatOne: " & return & displayText
  401.             log "    hexDumpFormatOne: almost there ..... length of displayText is " & length of displayText
  402.         end if
  403.         log "variable " & textMessage & " in hex is " & return & "         0    2    4    6    8    a    c    e     0 2 4 6 8 a c e" & return & displayText
  404.     on error errMsg number n
  405.         log "    hexDumpFormatOne: ==> convert hex string to string failed. " & errMsg & " with number " & n
  406.     end try
  407.     if debug ≥ 8 then
  408.         log "leaving ~.~ hexDumpFormatOne ~.~"
  409.     end if
  410. end hexDumpFormatOne
  411.  
  412. -- ------------------------------------------------------
  413. (* I think this is the way to force display dialog to the top of the heap of
  414.    windows.
  415.  *)
  416. on howToDisplayDialog()
  417.     -user3439894
  418.     -- https://apple.stackexchange.com/a/251490/44531
  419.     --
  420.     set theRichTextFileName to POSIX path of (path to desktop as text) & "New RichText Filename.rtf"
  421.    
  422.     tell application "Finder"
  423.        
  424.         if exists theRichTextFileName as POSIX file then
  425.             tell current application
  426.                 display dialog "The file \"" & theRichTextFileName & "\" already exists!" & "
  427.  
  428. " & "Do you want to overwrite the file?" buttons {"No", "Yes"} default button 1 with title "File Already Exists..." with icon caution
  429.                
  430.                 if the button returned of the result is "No" then
  431.                     return
  432.                 else
  433.                     tell application "Finder"
  434.                         delete the file (theRichTextFileName as POSIX file)
  435.                     end tell
  436.                 end if
  437.             end tell
  438.         end if
  439.        
  440.        
  441.        
  442.     end tell
  443.    
  444. end howToDisplayDialog
  445.  
  446. -- ------------------------------------------------------
  447. (*
  448. https://macscripter.net/viewtopic.php?id=43713
  449.   *)
  450. on integerToHex(nDec)
  451.     global debug
  452.     if debug ≥ 5 then log "in ~~~ integerToHex ~~~"
  453.     try
  454.         set nHex to do shell script "perl -e 'printf(\"%X\", " & nDec & ")'" --> "F0"
  455.     on error errMsg number n
  456.         log "==> convert integer to hex. " & errMsg & " with number " & n
  457.         set nHex to ""
  458.     end try
  459.     return nHex
  460. end integerToHex
  461.  
  462.  
  463. -- ------------------------------------------------------
  464. (*
  465.    ls_l is list file with options  
  466.   the format is best for debuging.
  467.  
  468.   example usage:
  469.   set {fileExists, fromUnix} to ls_l(attachmentNamePath, "-l")
  470.   log "attachmentNamePath fileExists is " & fileExists & return & " fromUnix is " & fromUnix
  471.  
  472.   *)
  473. on ls_l(attachmentNamePath, options)
  474.     --log "ls_l"
  475.     --log options
  476.    
  477.     set unixAttachmentNamePath to POSIX path of attachmentNamePath
  478.     --log "unixDesktopPath = " & unixAttachmentNamePath
  479.    
  480.     set quotedUnixAttachmentNamePath to quoted form of unixAttachmentNamePath
  481.     --log "quoted form is " & quotedUnixAttachmentNamePath
  482.     try
  483.         set fromUnix to do shell script "ls " & options & " " & quotedUnixAttachmentNamePath
  484.         set fromUnix to "ls " & options & return & fromUnix
  485.         set fileExists to true
  486.     on error errMsg number n
  487.         set fromUnix to "ls " & options & "  error..." & errMsg & " with number " & n
  488.         set fileExists to false
  489.        
  490.     end try
  491.     return {fileExists, fromUnix}
  492. end ls_l
  493.  
  494.  
  495. -- ------------------------------------------------------
  496. (*
  497. print out the items in a list
  498.  
  499. *)
  500.  
  501. on printList(name, splits)
  502.     set theCount to 1
  503.     repeat with theEntry in splits
  504.         log "------- " & name & theCount & " is " & return & theEntry
  505.         set theCount to theCount + 1
  506.     end repeat
  507. end printList
  508.  
  509.  
  510. -- ------------------------------------------------------  
  511. (*
  512. got errors when compiling.
  513.   found here: /Users/mac/Documents/BJ\ Prior\ Years/BJ2004/sendmailapp2\ copy
  514.  
  515. FYI: I have a full applescript app for sending email.
  516.  
  517.  
  518. *)
  519. on sendEmail(theEmail)
  520.     set theSubject to "Buffalo Jambalaya Information"
  521.     set theContent to "This is the test message."
  522.     set theSender to "BuffaloJambalaya@earthlink.net"
  523.    
  524.     set theRecipientName to theEmail
  525.     set theRecipientAddress to theEmail
  526.    
  527.     tell application "Mail"
  528.        
  529.         set newMessage to make new outgoing with properties ¬
  530.             {|subject|:theSubject, |content|:theContent, |sender|:theSender, visible:true}
  531.        
  532.         (*tell newMessage
  533.             make new  recipient at end of to recipients with properties ¬
  534.                 {name:theRecipientName, address:theRecipientAddress}
  535.         end tell*)
  536.     end tell
  537.    
  538. end sendEmail
  539.  
  540. -- ------------------------------------------------------
  541. (*
  542. textToList seems to be what you are trying to do
  543.   thisText is the input string
  544.   delim is what to split on
  545.  
  546.   returns a list of strings.  
  547.  
  548. - textToList was found here:
  549. - http://macscripter.net/viewtopic.php?id=15423
  550.  
  551. *)
  552.  
  553. on textToList(thisText, delim)
  554.     set resultList to {}
  555.     set {tid, my text item delimiters} to {my text item delimiters, delim}
  556.    
  557.     try
  558.         set resultList to every text item of thisText
  559.         set my text item delimiters to tid
  560.     on error
  561.         set my text item delimiters to tid
  562.     end try
  563.     return resultList
  564. end textToList
  565.  
  566. -- ------------------------------------------------------
  567. (*
  568.    Thanks to apple.com
  569.     trim_indicator values are:
  570.      0 = beginning, 1 = end, 2 = both
  571.    
  572.    example:
  573.     trim_line(theName, " ", 2)
  574.  
  575. *)
  576. on trim_line(this_text, trim_chars, trim_indicator)
  577.     -- 0 = beginning, 1 = end, 2 = both
  578.     set x to the length of the trim_chars
  579.     -- TRIM BEGINNING
  580.     if the trim_indicator is in {0, 2} then
  581.         repeat while this_text begins with the trim_chars
  582.             try
  583.                 set this_text to characters (x + 1) thru -1 of this_text as string
  584.             on error
  585.                 -- the text contains nothing but the trim characters
  586.                 return ""
  587.             end try
  588.         end repeat
  589.     end if
  590.     -- TRIM ENDING
  591.     if the trim_indicator is in {1, 2} then
  592.         repeat while this_text ends with the trim_chars
  593.             try
  594.                 set this_text to characters 1 thru -(x + 1) of this_text as string
  595.             on error
  596.                 -- the text contains nothing but the trim characters
  597.                 return ""
  598.             end try
  599.         end repeat
  600.     end if
  601.    
  602.     return this_text
  603. end trim_line
  604.  
  605. -- ------------------------------------------------------
  606. (*
  607. Foound here:
  608.     https://www.macosxautomation.com/applescript/sbrt/sbrt-06.html
  609.    
  610. Seems the same as Apple's version but with more comments.
  611.    
  612. Text Manipulation
  613.  
  614. Manipulating text strings is one of the most common tasks performed in scripts. The following sub-routines perform some of the most common text manipulation tasks.
  615. Trim Line
  616.  
  617. The following sub-routine can be used to trim text from the beginning or end of a string. It has three passed parameters:
  618.  
  619.     The text to trim
  620.     The characters to trim from the passed text
  621.     The direction indicator
  622.  
  623. The direction indicator has three possible numeric values:
  624.  
  625. 0, which tells the routine to trim the indicated characters from the beginning of the passed string:
  626.  
  627. set this_text to "----1----"
  628. trim_line(this_text, "-", 0)
  629. --> returns: "1----"
  630.  
  631. 1, which tells the routine to trim the indicated characters from the end of the passed string:
  632.  
  633. set this_text to "12345.txt"
  634. trim_line(this_text, ".txt", 1)
  635. --> returns: "12345"
  636.  
  637. 2, which tells this routine to trim the indicated characters from both ends of the passed string:
  638.  
  639. set this_text to "*-*-fred*-*-"
  640. trim_line(this_text, "*-", 2)
  641. --> returns: "fred"
  642. *)
  643.  
  644. on trim_line_version_2(this_text, trim_chars, trim_indicator)
  645.     -- 0 = beginning, 1 = end, 2 = both
  646.     set x to the length of the trim_chars
  647.     -- TRIM BEGINNING
  648.     if the trim_indicator is in {0, 2} then
  649.         repeat while this_text begins with the trim_chars
  650.             try
  651.                 set this_text to characters (x + 1) thru -1 of this_text as string
  652.             on error
  653.                 -- the text contains nothing but the trim characters
  654.                 return ""
  655.             end try
  656.         end repeat
  657.     end if
  658.     -- TRIM ENDING
  659.     if the trim_indicator is in {1, 2} then
  660.         repeat while this_text ends with the trim_chars
  661.             try
  662.                 set this_text to characters 1 thru -(x + 1) of this_text as string
  663.             on error
  664.                 -- the text contains nothing but the trim characters
  665.                 return ""
  666.             end try
  667.         end repeat
  668.     end if
  669.     return this_text
  670. end trim_line_version_2
  671.  
  672.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement