How to convert *applescript to *.scpt on the command line

In order to version control my applescripts I started to save them as text ('applescript’). However to execute them it’s better to have them avaivble as binary script ('.scpt’).

Is there a command line tool I can use for this conversion? Or do i have to do it via Script Editor / Script Debugger?

The very best would be a background tool which watches the directory and converts changed applescript text files on the fly into binaries…

The command line tool is osacompile.

Thanks Shane! Will check it out.

FWIW, a while back Shane posted a ASObjC script that converts scpt to applescript. Perhaps this could be modified to convert in the other direction?

See Post 4 in

https://macscripter.net/viewtopic.php?id=47430

You need to use OSAKit:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "OSAKit"
use scripting additions

set theFile to (choose file of type {"applescript"})
set aURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of theFile)
set destinationURL to aURL's URLByDeletingPathExtension()'s URLByAppendingPathExtension:"scpt"
set {theScript, theError} to current application's OSAScript's alloc()'s initWithContentsOfURL:aURL |error|:(reference)
if theScript is missing value then error theError's |description|() as text
set {theResult, theError} to theScript's compileAndReturnError:(reference)
if theResult as boolean is false then return theError's |description|() as text
set {theResult, theError} to theScript's writeToURL:destinationURL ofType:"scpt" usingStorageOptions:0 |error|:(reference)
if theResult as boolean is false then return theError's |description|() as text

Thanks Shane–that works great.

I should add that you can change the storage option to 1 (OSAPreventGetSource) if you want run-only versions.