Application created with Script Editor ignores the selected item on the ‘tell current application’ popup menu

applescript

Consider the following AppleScript program:

tell the application "TextEdit" to make new document

I saved this script as an Application, and ran it successfully by double-clicking the application icon.

I then simplified the script by setting the 'tell current application' popup menu to 'TextEdit' and replacing the program with the following line:

make new document

Simplified AppleScript program

I saved the script and ran it from within Script Editor. Everything worked OK. I then ran the application by double-clicking the application icon, but now the following error message appeared:

Can't make class document
Can't make class document. (-2710)

Can't make class document error message

What is the reason for this behavior?

Best Answer

You cannot create an AppleScript application that only contains the single line of code make new document as it will not execute within the same context it does in Script Editor having made a selection from the "tell" application menu.

As an AppleScript application you need to explicitly tell the appropriate application to make new document.

Example: tell application TextEdit to make new document

If you only code make new document and save it as, e.g. Code Test.app then make new document executes as tell current application to make new document or more literally in this case, tell application "Code Test" to make new document and the Code Test.app lacks the ability to create a new document and therefore errors out.

While in Script Editor, the "tell" application menu is a handy shortcut to avoid fully typing out tell application ... and is only for testing code. It is not translated to the literal code that you must be fully type so as to be fully functional within proper context to save as an application (or script for that matter).