Hello
Just for the record.
Another title would have been: Enter the Dragon! Access Clang from BBEdit or TextWrangler.
This script shows how to script a results browser of TextWrangler or BBEdit, by the simples means possible.
The script in itself is useful, if you’d like to compile Foundation examples, and see how they run from outside Xcode, as you may have a project there (or several), that you are working on.
So, if there are errors in the .m file, then a Results Browser window is thrown up as the front window of your BareBones Editor of choice. Otherwise, you are presented with the foreground Terminal window, (one is created if none exists), and your compiled example code is executed there.
Here is the script, you should really call it Compile Obj-C
After you have installed it, and assigned cmd-R as the short cut key, then use the file below to verify that it works.
Edit
And when you have affirmed that you are getting up the results browser, then please fix some of the errors, and see that you indeed get the executable running in terminal.
-- Copyright © 2014 McUsr All Rights Reserved.
# This script compiles an Objective-C file from BBEdit or TextWrangler
# The Objective-C file is intended to use the Foundation framework only.
# if you want to compile other stuff, then you'll have to configure the
# command line
tell application "BBEdit"
save text document 1
set sourceFile to file of text document 1
end tell
tell (a reference to text item delimiters)
set {tids, contents of it} to {contents of it, ":"}
set pieces to text items of (sourceFile as string)
set sourceFileName to the last text item of pieces
set contents of it to "/"
set sourceFileFolder to "/" & text items 2 thru -2 of pieces as text
set contents of it to tids
end tell
# we compile the file here, we redirect any standard error, and we amend the
# exit code of the comple-commandto the output, so that we understand what
# path to take
set compileScript to "cd " & (quoted form of sourceFileFolder) & "; clang -fobjc-arc -framework Foundation -Weverything " & quoted form of sourceFileName & " -o a.out 2>&1; echo $?"
set compilerOutput to paragraphs of (do shell script compileScript)
try
set exitCode to item -1 of compilerOutput as number
on error
display alert "Something is wrong with the compile script Aborting."
error number -128
end try
if exitCode = 0 then
# Time to run the command in a terminal window. to see some results :)
tell application "Terminal"
activate
set shell_script to "cd " & (quoted form of sourceFileFolder) & " ; a.out "
if (count windows) is 0 then
do script shell_script
else
do script shell_script in the front window
end if
end tell
else
set locationPars to {}
# we fire up a results browser in the app, so that we can see the errors.
repeat with anErrorLine in compilerOutput
set locationText to do shell script "sed -n '/.*[:][1-9]*[:][1-9]*.*[:].*/p' <<<" & quoted form of anErrorLine
if locationText is not "" then set end of locationPars to locationText
end repeat
set locationData to {}
repeat with aLocPar in locationPars
tell (a reference to text item delimiters)
set {tids, contents of it} to {contents of it, ":"}
set pieces to text items of aLocPar
set err_msg to the last text item of pieces
set lineNmbr to text item 2 of pieces
set theFname to text item 1 of pieces
set contents of it to tids
end tell
copy {err_msg, theFname, lineNmbr} to end of locationData
end repeat
tell application "BBEdit"
set resultItems to {}
repeat with location in locationData
set {locationMessage, locationFile, locationLine} to location
set hits to (text documents whose on disk is true and URL contains locationFile)
if (count of hits) > 0 then
set mydoc to first item of hits
set locationLine to locationLine as number
set myline to line locationLine of mydoc
set s_offset to characterOffset of myline
if (count of characters of myline) > 0 then
set e_offset to characterOffset of last character of myline
else
set e_offset to s_offset
end if
set resultEntry to {start_offset:(s_offset - 1), end_offset:e_offset, message:locationMessage, result_kind:error_kind, result_file:file of mydoc, result_line:locationLine}
copy resultEntry to end of resultItems
end if
end repeat
make new results browser with data resultItems with properties {name:"Compile Errors in " & sourceFileName}
end tell
end if
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSLog(@"Hello World");
NSInteger myint = "Stunned"
}
return 0;
}
Enjoy.