Advertisement
applehelpwriter

BBEdit: remove whitespace

Feb 15th, 2018
2,189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #######################
  2. -->> ABOUT
  3. #######################
  4. (*
  5.  
  6.  Phil Stokes -- 2018
  7.  applehelpwriter.com
  8.  sqwarq.com
  9.  
  10. *)
  11. #######################
  12. -->> DESCRIPTION
  13. #######################
  14. (*
  15.  
  16. Remove whitespace
  17. in BBEDit's front document
  18.  
  19. *)
  20. #######################
  21. -->> USAGE
  22. #######################
  23. (*
  24.  
  25. Save the script in BBEdit's Script folder, then run it on the front document by choosing
  26. it from the Script menu in BBEdit's menu bar.
  27.  
  28. *)
  29. #######################
  30. -->> IMPORT STATEMENTS
  31. #######################
  32.  
  33. use AppleScript version "2.4" -- Yosemite (10.10) or later
  34. use scripting additions
  35. use framework "Foundation"
  36.  
  37. #  classes, constants, and enums used
  38. property NSCharacterSet : a reference to current application's NSCharacterSet
  39. property NSString : a reference to current application's NSString
  40.  
  41. #######################
  42. -->> HANDLERS
  43. #######################
  44.  
  45. on removeWhitespaceInText:srcText
  46.     set stringList to {}
  47.     set returnStr to ""
  48.     set lineList to paragraphs of srcText
  49.     repeat with aLine in lineList
  50.         set aString to (NSString's stringWithString:aLine)
  51.         set remainingString to (aString's stringByTrimmingCharactersInSet:(NSCharacterSet's whitespaceAndNewlineCharacterSet()))
  52.         set applescriptStr to remainingString as text
  53.         if applescriptStr's length is greater than 0 then
  54.             set end of stringList to remainingString
  55.         end if
  56.     end repeat
  57.     repeat with str in stringList
  58.         set returnStr to returnStr & (str as text) & return
  59.     end repeat
  60.     return returnStr
  61. end removeWhitespaceInText:
  62.  
  63. #######################
  64. -->> COMMANDS
  65. #######################
  66.  
  67. tell application "BBEdit"
  68.     set t to text of front document
  69.     set t to my removeWhitespaceInText:t
  70.     set text of front document to t
  71. end tell
  72. #######################
  73. #EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement