Sunday, October 16, 2022

AppleScripts to speed Java compile and execute with BBEdit

Students are often asked not to use an IDE (I like Visual Studio Java) for their Java projects. Instead they need to use BBEdit without an IDE. BBEdit does not appear to natively support facilitated compile/execute but it does run AppleScrips that can speed things a bit.

The davalign.com site has some scripts written for TextWranger. If you rename them to BBEdit they work well.

Reproducing them here in case that site vanishes. I didn't see much like this. Note if you open and save in macOS Script Editor they will be compiled. I like that the first action is to save the document, it's easy to forget to save before a compile.

Compile java.scpt

tell application "BBEdit"
save text document 1
set the_file to file of text document 1
end tell

set AppleScript's text item delimiters to ":"
set source_file to the last text item of (the_file as string)

tell application "Finder"
set the_folder to container of file the_file
end tell

tell application "Terminal"
activate
set p to POSIX path of (the_folder as string)
set shell_script to "cd " & (quoted form of p) & ¬
"; javac " & source_file
if (count windows) is 0 then
do script shell_script
else
do script shell_script in the front window
end if
end tell

Run java.scpt

tell application "BBEdit"
set the_file to file of text document 1
end tell

set AppleScript's text item delimiters to ":"
set source_file to the last text item of (the_file as string)
set compiled_file to text 1 thru -6 of source_file

tell application "Finder"
set the_folder to container of file the_file
end tell

tell application "Terminal"
activate
set p to POSIX path of (the_folder as string)
set shell_script to "cd " & (quoted form of p) & ¬
"; java " & compiled_file
if (count windows) is 0 then
do script shell_script
else
do script shell_script in the front window
end if
end tell

No comments:

Post a Comment