Running AppleScripts: Text Scripts Embedded in a Macro or Compiled AppleScript Files?

Dear all,

Having finally secured a bit of free time, I'm going to go about rejigging and reorganizing my KBM macros, which have become somewhat chaotic as I've developed my familiarity (by making experiments) with macro-building. I'd love some tips or resources as to how to best organize my macros, although I think I'll go with a mostly app-specific organization alongside aliases that fit into process or workspace palettes. Granted, there are many helpful resources already available on this forum regarding both the above and hotkey specifications.

One question I did have that I can't find thoroughly addressed in any of the current discussions is whether there is any consensus regarding scripts in KBM. Specifically, is it better to have the scripts embedded within the macro or have the macro run a file at a given path? I've generally done the former, however this means that I usually have to copy and paste the script (whether Apple, Java, Shell, Python, or whatever) into the appropriate text/script editor in order to make substantial changes. For this reason, as well as to version any possible future changes through something like GitHub, I'm considering whether I should take all of the scripts out of their macro and just run them from a folder that allows for easier editing and versioning. Would there be any drawbacks to this or, more generally, how do other people with growing macro libraries full of scripts go about this?

Thanks in advance!

1 Like

Great question!

IMO, that is the best approach:
Run all AppleScripts, JXA scripts, and JavaScripts from a file this referenced in the KM Execute Script Action, for these reasons:

  1. Compiled scripts (.scpt) will run faster because they do not have to be first compiled by KM.
  2. Keeping all of these scripts in a designated KM Scripts folder provides for much better script management and testing
    1. It is much easier to test/debug a script, open in Script Debugger 7, but run from a KM Macro.
    2. Allows you to quickly find scripts using macOS tools like Spotlight, especially when you use file tags.
  3. Allows you to take advantage of other systems like DropBox, GitHub, etc

The only drawback I know of is that when you want to share a KM Macro, you either have to also provide the script file, or copy the script into the KM Execute Script Action.

AFAIK, there is no official KM documentation on using script text vs a script file, but hopefully @peternlewis and @ccstone will jump in here to clarify.

I have a Script Library in which I keep most of my reusable script handlers. So for KM Macros for my own use, I make use of the use <Script Library> command to access these handlers from my scripts. I have a KM Macro which aids in the search, selection, and insertion of a call to a handler in this Library. It can also copy the handler directly into the target script, if I need to share that script without sharing my Script Library.

Questions?

1 Like

I don’t follow. Clarify what?

Can you confirm that it is faster to use a compiled script file (.scpt) than script text in an Execute AppleScript Action?

Are there any other advantages, or disadvantages, of using script files?

I have no idea which is faster when, and what might affect them. I expect it could change over time and depend on file caching and such. For example, if Keyboard Maestro has the text of the script, it writes it to disk and then executes the script that is written, but that written file will almost certainly be cached in memory and quite possibly never touch the disk at all.

.scpt files will store properties from the script, which is the most obvious practical difference. Text filed and text in Keyboard Maestro will not.

As always, if performance is important then the only real way to determine which is faster in your specific case is to test it.

For myself, I've never had any reason to move text scripts in Keyboard Maestro to a file, though I have lots of scripts (usually perl scripts) on disk that Keyboard Maestro might execute - but the choice is made for reasons other than performance. The difference in speed is unlikely to be of interest unless you are executing the script thousands (probably many thousands) of times.

For those who use the built-in Time Machine for backups, note that running scripts in Keyboard Maestro from a file will cause Time Machine to backup the files anew each time it backs up. While script files aren't usually large, this may cumulatively affect a user's amount of available backup space while potentially making it more difficult to locate past versions in Time Machine backups as there will be more instances of the script to consider for recovery.

Those aren't likely to be significant effects, but they've led me in many cases to keep script files as easily referenced copies but I execute them in Keyboard Maestro by embedding them within macros.

2 Likes

Yep.

It's not generally a huge difference, but I notice and therefore use compiled AppleScripts most of the time.

-Chris

Great info, thanks! Do you know if that also happens with *.applescript files?

It does, at least with HFS+ Time Machine backups. I don't know if that's true with macOS 11 Big Sur Time Machine backups which use APFS, the newer file system.

1 Like