Applescript: Replace Text Script with File

AppleScripts run a bit faster when saved as pre-compiled .scpt files than when run as text scripts via Keyboard Maestro.

The following macro will use the text script of the currently selected Execute an AppleScript action to create a .scpt file. It will then add a new action to your macro that executes this file.

CleanShot 2023-11-29 at 07.58.44

AppleScript- Convert Text Script to Executed .scpt File.kmmacros (86 KB)

Macro screenshot

Setup:

Create a folder for your compiled scripts and set its path in the green action.

11 Likes

This is great man, thanks for doing this!

1 Like

Updated:

  • Organises scripts by group and macro subfolders.
  • Ensures unique script file basenames.
1 Like

Hey I noticed that when the text AppleScript action has a custom name, that is auto...

Well I think your new version likely addresses the issue I was just writing about. :laughing:

1 Like
I did notice now, however, that the saved files don’t appear to be compiled when I open them in Script Debugger. (click to expand/collapse)

Also, if I edit them and want to save them, it does not allow me to do so. I have to do Save As and replace the existing file. Any work arounds for that? (click to expand/collapse)

Screenshot 2023-11-26 at 08.14.22

Updated: Placeholder name in prompt will be the name of the current macro.

One more thing: I noticed the & symbol is replaced with the unicode? format, becoming &... however this breaks things.

See this screenshot for an example. (click to expand/collapse)

I thought this would work, but it throws AS errors when compiling:

Compile Script - TEST.kmmacros (20 KB)

Macro screenshot

Select one of the .scpt files and run the macro.

@ccstone is an AppleScript ninja, so maybe he could point us in the right direction.

In the meantime, my scripts are running quicker this way, pre-compiled or not...

Ok this is serious enough to take the topic down for a bit.

Looking at the osacompile man page (osacompile Man Page - macOS - SS64.com) it would seem to me that you can`t compile a file into itself which is what you’re trying to do.

1 Like

Great!

This works!

Compile Script - Working-.kmmacros (21 KB)

Macro screenshot

Thanks for the helpful comments, @cdthomer and @tiffle!

Updated:

  • Fixed errant unicode character conversion.
  • Scripts are now saved pre-compiled.
  • Inserted action mirrors original action's settings (display type, error notification, etc...)
2 Likes

Great! It’s working well for me in my limited testing. I’ll take a closer look this evening and tomorrow and report back if I run into any issues.

1 Like

Updated: notifies that script syntax may be problematic if compiling fails.

Hey @noisneil the macro is working great. Quick question: would it be possible to either remove the script text from the pasted action, or set that as an option? The pasted action still contains the script if you switch it from “Execute script file” to “Execute script text”, and removing the text would cut down on the macro size which is another benefit for macros with large/complex AppleScripts.

I've been hesitant to remove the text from the action, in case there's an issue (e.g. we accidentally overwrite the script file during macro testing) and we want to be able to access the text version again. Same reason I've been disabling rather than removing the original action.

What happens when we share a macro that relies on these external scripts and we've deleted the text version? It's all a bit anxious-making! :joy:

Here's a more destructive version nonetheless!

AppleScript- Convert Text Script to Executed .scpt File.kmmacros (76 KB)

Macro screenshot

1 Like

In an ideal world, it would convert every AppleScript action in the current macro. If you run it again it would convert them back. These functions could also be incorporated into our upload macro, checking for potential orphaned scripts before converting (and then converting back after upload if necessary).

1 Like

...or alternatively, the upload macro could check for any file paths ending in .scpt in the macro XML, zip these files together and include them in the upload...

Or... we could just be a bit careful about which macros get the script conversion treatment... :sweat_smile:

1 Like

Or alternatively you could run osadecompile on the compiled script to get back to the text. If you do that you may want to research into the possible drawbacks of using that shell command…

Updated:

If you run the macro with a script file version of the action selected, it will convert it back to a text version and tidy up your compiled scripts folder by deleting the script file and trashing its parent folders if they are empty.

The reason I've decided not to decompile and remove the text from the action is that a 10,000 character script takes up only 10KB, and I'd rather maintain that text as a backup in case a user's compiled scripts folder goes walkabout for some reason.

It also means that, when sharing macros, the recipient can switch the actions to use text and still have a working macro.

1 Like