TIP: How to Reference an App in AppleScript and JXA

###How to Reference an App in AppleScript and JXA
Note: Most of this, if not all, apples to both AppleScript and JXA. I will use AppleScript in most of the examples.

For reference:
####AppleScript

tell application "Some App Reference"
end tell

is the same as:
###JXA

var myApp = Application("Some App Reference");

where “Some App Reference” is defined in this Apple guide:
Accessing Applications



There are times when you might be tempted to dynamically determine the App name using a text variable, something like this:


```applescript
tell application "System Events"
set theApp to ((path to frontmost application) as text)
tell application theApp
   --- Your statements here based on App ---
   end tell
end tell
```
**⚠️ Warning!  This will not work on a reliable basis.**

In order for AppleScript to compile correctly using the proper "terms" from the proper app, you must explicitly identify the app in the "tell" statement.

Please see these Apple references:

*  [Accessing Applications (JXA Release Notes)](https://developer.apple.com/library/content/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/OSX10-10.html#//apple_ref/doc/uid/TP40014508-CH109-SW6).  
  * It is written for JXA, but also applies to AppleScript .

* I can't find a reference for a direct usage in AppleScript, but this is close:
[AppleScript Language Guide, Class Reference for "application" section](https://developer.apple.com/library/content/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html#//apple_ref/doc/uid/TP40000983-CH1g-SW2). 
(You will need to page down one or two screens.)

For example, you can use any ONE of these, but maybe with different results:

###AppleScript "tell application" Blocks
```applescript
--- USE THE APP NAME (normal approach)
tell application "Adobe Acrobat Pro"
  set verName to version
end tell

--- USE THE APP BUNDLE ID ---
tell application id "com.adobe.Acrobat.Pro"
  set verID to version
end tell

--- USE THE APP PATH ---
tell application "/Applications/Adobe Acrobat XI Pro/Adobe Acrobat Pro.app"
  set verPath to version
end tell

log ("verName: " & verName)
log ("verID:   " & verID)
log ("verPath: " & verPath)

### RESULTS WHEN Acrobat X IS RUNNING ###
(*verName: 10.1.12*)
(*verID:   10.1.12*)
(*verPath: 11.0.19*)
```

**You might think the Bundle ID is unique, but it is not.**

###Results from Get App Info Macro

> **Acrobat X:**

> appName:            Acrobat
> appID:                 **com.adobe.Acrobat.Pro**
> appPathHFS:      Macintosh HD:Applications:Adobe Acrobat X Pro:Adobe Acrobat Pro.app:
> appPathPOSIX:   /Applications/Adobe Acrobat X Pro/Adobe Acrobat Pro.app


> **Acrobat XI:**

> appName:            Acrobat
> appID:                  **com.adobe.Acrobat.Pro**
> appPathHFS:       Macintosh HD:Applications:Adobe Acrobat XI Pro:Adobe Acrobat Pro.app:
> appPathPOSIX:   /Applications/Adobe Acrobat XI Pro/Adobe Acrobat Pro.app

So, as you can see, both Acrobat X and Acrobat XI have the same Bundle ID:
"`com.adobe.Acrobat.Pro`"

**The only way to be sure you are targeting the App of interest is to use its path:**
"`/Applications/Adobe Acrobat XI Pro/Adobe Acrobat Pro.app`"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Please feel free to post any follow-up questions or suggestions for improvements.