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