Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 2.92 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Applescript: trim spaces and return line
  2. set some_file to "Macintosh HD:Users:Zade:Library:Application Support:Notational Data:Words.txt" as alias
  3. set the_text to read some_file as string
  4. set the text item delimiters of AppleScript to ", "
  5. set the_lines to (every text item of the_text)
  6. return some item of the_lines
  7.        
  8. set theFile to (choose file)
  9. set theLines to paragraphs 28 thru -1 of (read theFile)
  10.  
  11. repeat -- forever
  12.  
  13.     set someLine to some item of theLines
  14.  
  15.     -- trim characters at the start
  16.     if someLine is not "" then repeat until the first character of someLine is not in {space, tab, quote}
  17.         if (count someLine) is 1 then
  18.             set someLine to ""
  19.             exit repeat
  20.         end if
  21.         set someLine to text 2 thru -1 of someLine
  22.     end repeat
  23.  
  24.     -- trim characters at the end
  25.     if someLine is not "" then repeat until the last character of someLine is not in {quote}
  26.         if (count someLine) is 1 then
  27.             set someLine to ""
  28.             exit repeat
  29.         end if
  30.         set someLine to text 1 thru -2 of someLine
  31.     end repeat
  32.  
  33.     if someLine is not "" then exit repeat
  34. end repeat
  35.  
  36. return someLine
  37.        
  38. set the text item delimiters of AppleScript to return
  39.        
  40. set AppleScript's text item delimiters to ""
  41. return text items 2 thru -2 of some_string
  42.        
  43. set some_file to "Macintosh HD:Users:Zade:Library:Application Support:Notational Data:Words.txt" as alias
  44. set the_text to read some_file as string
  45. set the_lines to (every paragraph of the_text)
  46. set this_line to some item of the_lines
  47. if this_line is not "" then
  48.     set AppleScript's text item delimiters to ""
  49.     set this_line to (text items 2 thru -2 of this_line) --remove the quotes
  50.     set AppleScript's text item delimiters to space
  51.     set these_items to (every text item of this_code)
  52.     set the this_line to (item 1 of these_items & this_line)
  53.     set AppleScript's text item delimiters to ""
  54.     return this_line
  55. end if
  56.        
  57. if paragraph i of some_text is not "" then do_something()
  58.        
  59. on trimWhiteSpace(aString)
  60.     if aString is not "" then
  61.         -- setup for no delimiter
  62.         set savedTextItemDelimiters to AppleScript's text item delimiters
  63.         set AppleScript's text item delimiters to ""
  64.         -- start with the tail end by revering the list
  65.         set these_items to reverse of (every text item of aString)
  66.         -- keep peeling off 1st space
  67.         repeat while item 1 of these_items is space
  68.             set these_items to rest of these_items
  69.         end repeat
  70.         -- flip the list, now do the leading characters
  71.         set these_items to reverse of these_items
  72.         repeat while item 1 of these_items is space
  73.             set these_items to rest of these_items
  74.         end repeat
  75.         -- reconstruct the string
  76.         set these_items to these_items as string
  77.         -- restore and return
  78.         set AppleScript's text item delimiters to savedTextItemDelimiters
  79.         return these_items
  80.     end if
  81. end trimWhiteSpace