1*7330f729Sjoerg-- In this file, change "/path/to/" to the path where you installed clang-format 2*7330f729Sjoerg-- and save it to ~/Library/Application Support/BBEdit/Scripts. You can then 3*7330f729Sjoerg-- select the script from the Script menu and clang-format will format the 4*7330f729Sjoerg-- selection. Note that you can rename the menu item by renaming the script, and 5*7330f729Sjoerg-- can assign the menu item a keyboard shortcut in the BBEdit preferences, under 6*7330f729Sjoerg-- Menus & Shortcuts. 7*7330f729Sjoergon urlToPOSIXPath(theURL) 8*7330f729Sjoerg return do shell script "python -c \"import urllib, urlparse, sys; print urllib.unquote(urlparse.urlparse(sys.argv[1])[2])\" " & quoted form of theURL 9*7330f729Sjoergend urlToPOSIXPath 10*7330f729Sjoerg 11*7330f729Sjoergtell application "BBEdit" 12*7330f729Sjoerg set selectionOffset to characterOffset of selection 13*7330f729Sjoerg set selectionLength to length of selection 14*7330f729Sjoerg set fileURL to URL of text document 1 15*7330f729Sjoergend tell 16*7330f729Sjoerg 17*7330f729Sjoergset filePath to urlToPOSIXPath(fileURL) 18*7330f729Sjoergset newContents to do shell script "/path/to/clang-format -offset=" & selectionOffset & " -length=" & selectionLength & " " & quoted form of filePath 19*7330f729Sjoerg 20*7330f729Sjoergtell application "BBEdit" 21*7330f729Sjoerg -- "set contents of text document 1 to newContents" scrolls to the bottom while 22*7330f729Sjoerg -- replacing a selection flashes a bit but doesn't affect the scroll position. 23*7330f729Sjoerg set currentLength to length of contents of text document 1 24*7330f729Sjoerg select characters 1 thru currentLength of text document 1 25*7330f729Sjoerg set text of selection to newContents 26*7330f729Sjoerg select characters selectionOffset thru (selectionOffset + selectionLength - 1) of text document 1 27*7330f729Sjoergend tell 28