7

I want to create an executable file that when run will send a popup. I have the code for the popup

osascript -e 'tell app "Finder" to display dialog "message"'

but don't know how to create an executable file.

  • Firstly, welcome to Ask Different! :) I hope you come to find this site has a lot to offer! In case you haven't already, it's worth taking the time to read the tour. All the best with your question. – Monomeeth May 16 at 10:58
  • 2
    FWIW, you may not want to tell Finder to display the dialog, because that requires having the permissions to control that app via accessibility settings in recent macOS versions. You should be able to just do display dialog "message" – Alexander O'Mara May 16 at 19:50
15
  1. Write your AppleScript in Script Editor.

  2. Choose FileSave and select Application as File Format.

This gives you an .app which will run your AppleScript when opened.

5
  1. Write your shell script in a text file, including the shebang.

    If you're just using AppleScript, you can use osascript as the shebang:

    #!/usr/bin/osascript
    tell app "Finder" to display dialog "message"
    
  2. Set the executable bit on the file.

    chmod +x /path/to/file
    

This gives you a file which will run your shell script when opened.

  • Why a bash shell script when you can make the shebang #!/usr/bin/osascript and then the content of the file can be a multi-line applescript (tell app "Finder" to display dialog "message") ... but your answer of saving as an application using Script Editor is a far better solution – Josh May 16 at 13:24
  • @Josh This is already mentioned in my answer; I gave the shell solution first given that’s how the code is presented in the question, but I also mention osascript shebang which would be preferable – grg May 16 at 13:45
  • apologies @grg I didn't see the osascript in the question until after commenting and after that point it was too late to retract my downvote. If you make an edit to the answer then the site will let me change my vote! – Josh May 16 at 14:15
  • Since # is a comment character in AppleScript, it should be possible to combine grg's answer and this one to create a polyglot file that is both an app and a script. – Jörg W Mittag May 16 at 19:14
  • @JörgWMittag, have you tested that? Since .app files are actually directories, I would not expect that to work. – Wildcard May 16 at 19:58
2

You can save it as a script by putting this in a plaintext file and save it as something like dialog.sh:

#!/bin/bash
osascript -e 'tell app "Finder" to display dialog "message"'

Then making the file executable in the terminal by running chmod +x /path/to/dialog.sh.
Execute in terminal by running /path/to/dialog.sh

  • This is creating a bash shell script which executes osascript... seems to me you should just make the shebang #!/usr/bin/osascript and then the content of the file can be a multi-line applescript (tell app "Finder" to display dialog "message") ... but still seems like a workaround for the "proper" way of making an AppleScript executable from the finder: saving as an application – Josh May 16 at 13:23
  • apologies I didn't see the osascript in the question until after commenting and after that point it was too late to retract my downvote. If you make an edit to the answer then the site will let me change my vote! – Josh May 16 at 14:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .