Create Apple Script Programmatically

I am using privatized javascript which has access to python as ctypes and the whole mac api.

Programmatically I am trying to create an applescript file and set its icon.

I'm trying to follow this guide here on how to create profile shortcuts for macs:

Is it as easy as creating a text file and filling it with text?

I don't have a mac, just coding for my Mac users.


source to share

1 answer

You want to use the osacompile command line tool to create a compiled applescript file. You can check out his man page to see all of your options, but it's pretty straightforward. For example, suppose you wanted to write the following application code to a file ...

tell application "Safari"
end tell


You can do this with osacompile and use the -e option before every line of code. Please note that I put single quotes on each line too.

set savePath to (path to desktop as text) & "test.scpt"

do shell script "osacompile -e 'tell application \"Safari\"' -e 'activate' -e 'end tell' -o " & quoted form of POSIX path of savePath


If you don't like the -e option, you can echo the text of the code ...

set scriptText to "tell application \"Safari\"
end tell"

set savePath to (path to desktop as text) & "test.scpt"

do shell script "echo " & quoted form of scriptText & " | osacompile -o " & quoted form of POSIX path of savePath


The saved file will have an applescript icon by default. If you also want to change your icon, you can use a command line tool I wrote called SetFileIcon. Find it here . There are directions on the page.



All Articles