Using ShellCheck with BBEdit

Several people I follow in Twitter have pointed a quite useful looking tools called ShellCheck. It will scan shell script code for common problems that may lead to errors later, especially quoting.

However, the script is written in Haskell, which brings with it a rat’s tail of dependencies if you want to install it on your own Mac.

Since I did not want to bother with that I wrote a script that will grab the code from the frontmost BBEdit window and paste it into the webpage:

tell application "BBEdit"
if not (exists text document 1) then
return
end if
if source language of text document 1 is not "UNIX Shell Script" then
set thename to name of text document 1
display dialog "Document '" & thename & "' does not seem to be a shell script!"
return
end if
set theCode to text of text document 1
end tell
-- escape some crticial characters
set theCode to replace_chars(theCode, "\\", "\\\\")
set theCode to replace_chars(theCode, quote, "\"")
set theCode to replace_chars(theCode, "'", "\\'")
set theCode to replace_chars(theCode, "\n", "\\n")
tell application "Safari"
open location "http://shellcheck.net"
delay 1
do JavaScript "document.getElementById('code').value = '" & theCode & "';" in document 1
do JavaScript "transfer('#code', '#console')" in document 1
end tell
-- from the ever useful http://macosxautomation.com/applescript/sbrt/sbrt-06.html
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars

Drop this in your BBEdit Scripts folder and enjoy!

Published by

ab

Mac Admin, Consultant, and Author

8 thoughts on “Using ShellCheck with BBEdit”

Comments are closed.