Dynamic creation of an AppleScript via AppleScript


Markus Ruggiero
 

Hi,

I want to create an AppleScript .scpt file programmatically without using Script Editor. The goal is to have some Apple Script commands in a plain text file to be used as a template. I want to create an AppleScript that should ask the user a couple questions and then use the answers to fill in the "template" and finally save the resulting script to disk.

Simplified example:

Template.txt contains

set theApplication to XXXXX
tell application theApplication
-- many things....
end tell

My script should ask the user for the name of the application, then load the template and replace XXXXX by what the user has given. At the end the result should become a .scpt file on disk.

Thanks for any help
---markus---


Christopher Stone
 

On 09/03/2018, at 09:24, Markus Ruggiero <mailinglists@...> wrote:
I want to create an AppleScript .scpt file programmatically without using Script Editor.


Hey Markus,

Have a look at these two post on the Applescript Users List.

--
Best Regards,
Chris
use framework "Foundation"
use framework "OSAKit"

set sourceURL to current application's class "NSURL"'s fileURLWithPath:"path/to/file.applescript"
set destURL to current application's class "NSURL"'s fileURLWithPath:"path/to/file.scpt"
set theScript to current application's OSAScript's alloc()'s initWithContentsOfURL:sourceURL
set theResult to theScript's compileAndReturnError:(missing value)
if theResult as boolean then
theScript's writeToURL:destURL ofType:(current application's OSAStorageScriptType|error|:(missing value)
end if




osacompile -o <object-file> <source-file>  (see the man page)




2551phil
 


 The goal is to have some Apple Script commands in a plain text file to be used as a template.

On top of what Chris said, you can also create and run AppleScripts as .sh files.

Just write your AppleScript with a shebang targeting osascript:

#! /usr/bin/osascript
display dialog "hello, world"


Best


Phil