Create application from AppleScript [SOLVED]

I have an AppleScript template that opens a URL with a specific browser and then I use that with ScriptDebugger to create an app file so when I double click it, it automatically opens the URL with Chrome like this:


(* original title
title here

tell application "Google Chrome" to open location ""

I know I could use KM's action to do that, but the URLs are private stuff that I have inside an encrypted folder, so I don't want it to be easily available to others.
Is it possible to use KM (maybe Shell or something) to convert the AS into an app file?

I guess I'm a bit confused: If you have the URLs in KM, even if you use KM to compile the AS, anyone who gets to your Mac will have access to KM, so they could find the URLs anyway.

Or am I not understanding the request?


You'll have to use osacompile to make an "execute-only" app -- that won't be "secure", but it'll stop obvious things like trying to open the app in Script Editor to view the contents. Do man osacompile in Terminal for instructions. If you already have the script as a file -- in this case, it's compileTest.scpt on the Desktop -- then:

osacompile -x -o ~/Desktop/ ~/Desktop/compileTest.scpt

...will save it as an execute-only app.

You can also include the script in the command, although quotes can get a bit crazy! If you wrap the command in single-quotes then your double-quoted strings will be OK, but remember to escape any singles in the script's text. So for your example above:

osacompile -e 'tell application "Google Chrome" to open location ""' -x -o ~/Desktop/

But if you are that concerned about security, you've probably got a proper password manager already. In which case you might be able to do something with "private notes" or similar. Or use an encrypted disk image to store the scripts -- run the macro which mounts the image, you enter your password, the macro runs the script then unmounts the image again. As Rob implies -- if they've already got access to your account to the extent that they can read KM macros, you've probably got bigger problems than a few "private" URLs!

For fun, proof of concept using Notes -- which, of course, you can "Lock" behind a password. The macro is simple:

Open URL from Secure Note.kmmacros (1.9 KB)

...and to open the BBC website in Safari you pass the parameter

tell%20application%20%22Safari%22%20to%20open%20location%20%22https%3A%2F%2Fwww%2Ebbc%2Eco%2Euk%22 you'd create a Note like:

(you can get the first bit of that from the "Or by URL:" section of your macro;s triggers).

There's some work in URL encoding the parameter -- but KM has a filter for that, so you should be able to automate it to an extent.

The result is password protected Note(s) with clickable URLs to open sites in specific browsers.

I want to create a macro that prompts me to add the Title and the URL and it will create the .application file
Then I move that file to the encrypted folder.

So basically KM won't have any URLs there. It will be just a toll to convert the script to an app, which is what I'm doing manually at the moment. I have to open ScriptDebugger, add the script, save as application.

OK, that makes sense.

You should be able to do that using osacompile as shown above—either on the command line by passing in a variable with the script, or from a file you could build then delete when done; @Nige_S' post covers both methods.


I tried your second script, but I got this:

Execute an AppleScript failed with script error: text-script:12:15: script error: A unknown token can’t go after this identifier. (-2740)

Then I tried your first script, but it doesn't work either... like this:

osacompile -x -o ~/Desktop/ ~/My\ Files/Inbox\ Global/123.scpt
Execute an AppleScript failed with script error: text-script:15:18: script error: A unknown token can’t go after this identifier

I don't. I manage all my passwords individually, myself.

Thing is, I want those "apps" to be in a specific folder where I have other files that are not supposed to be converted to an application, so Notes wouldn't be a good option.

Basically my workflow is: open encrypted folder, have access to all files (apps or not) and work from there. Sometimes I just need to access the files, sometimes the "app" files. I've been using Cryptomator as my encrypting system, but creating the app files is always too much of an hassle and I wanted to make it as fast as possible.

Notes is definitely not the option I need in this case, because of what I described above, and I also want to have the app files next to certain files (it could be a .doc, .jpeg, etc) so if I'm accessing one of those files and I want to then open the URL, the app file is right there ready to just double click.

For some reason none of the examples work for me. Check my reply to see what kind of errors I'm getting

The command line script compiled fine here—I think this may be (or wonder if it may be) a permissions issue with the added protection Apple has given to the Desktop folder (and other key folders). If you give Terminal Full Disk Access in Privacy & Security, does it work then?


Ok so I tried it with Terminal and it worked, at least this one:

osacompile -e 'tell application "Google Chrome" to open location ""' -x -o ~/My\ Files/Inbox\ Global/

So the issue seems to be when I run it with KM:

Then when I try to open the app file, I can't, because it's read-only.
When I create the file manually via Script Debugger and use the Open With option, I can edit the app file, which is what I would like to do, in case I need to edit it in the future.

Also, I would like to include a comment in the beginning of the script, the way I shared on my original post:

(* original title
title here

tell application "Google Chrome" to open location ""

Will I be able to do these things?

New test with Terminal...
Tried this and it worked:

osacompile -x -o ~/My\ Files/Inbox\ Global/ ~/My\ Files/Inbox\ Global/111.scpt

So that solves one of the issues, I guess, which is the option to add the script and include the comment with the original title.

I still can't edit the app file once it's created.

I guess I would be able to create the .scpt via KM as well?
So I would get a Prompt action to add the title and the URL
It would create the .scpt file, convert it to .application, then delete the .scpt file.
Would that work?

If so, then I just need to make KM work when it comes to convert the .scpt file to .application, which is not. And then being able to edit the .application file as well

That's because the -x flag makes it executable only. Leave that out and you can open it in the editor.

Here's a proof of concept macro that prompts for some values and creates the script, then opens that script's folder in Finder. NOTE: The demo script uses Safari as I don't use Google's products so can't test with Chrome. But it should work fine just changing the script to refer to Chrome as you did before.

I avoid all the quoting issues with the shell by using something called a here document, which forces the shell to not try to interpret its contents.

Download Macro(s): Compile AppleScript.kmmacros (4.8 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 14.4.1
  • Keyboard Maestro v11.0.2

You can modify this to your needs, of course. The basic flow just needs to be (1) assign values to the variables you want to set in the script, (2) add those variables to the script variable, and (3) run the shell script.

And yes, ~/Desktop is some sort of protected place; the script will fail if you try to build there (and probably Documents and some other spots).

Hope that helps!


Perhaps because you are trying to run a shell script inside an "Execute AppleScript" action?

@griffman's covered the rest, although I'm surprised you're both getting failures when writing to the Desktop -- works fine here (macOS 14.3.1). My guess is that you're on the wrong action, as mentioned above. Rob, you aren't by chance passing in a ~ path, are you?

If you're going with Rob's here method then make use of the easy multiple lines that grants you and change the AppleScript to include an activate statement -- that way your newly-opened page will be the frontmost thing:

tell application "%Variable%Local__browserChoice%"
   open location "Variable%local__URL%
end tell

No, I'd never ever ... oops. All better now! :slight_smile:


1 Like

Thank you so much @Nige_S and @griffman !
It's working now (thanks @Nige_S for punching me in the face with that obvious thing of me trying to run a shell inside an AppleScript action... sometimes I have no idea what I'm doing haha)

Here's the final version that suits my workflow

Create links.kmmacros (6.9 KB)

Keyboard Maestro Export

I know I always want to save those app files to the same location so I saved that as a variable and then used the filter to add the full path, that way the macro will work if I have a new machine or OS where the username is different :slight_smile:

The way I copy the URL and Title is always the same so I just "automated" the fields in the prompt to give me the last 2 clipboard "snippets".

Also in the prompt I don't need 2 options for the title, because I will only have one.
Then I added the "activate" command as @Nige_S suggested.

Again, really appreciate your time and help with this! :raised_hands: