Applescript to tell active application

Hello,

In my everyday tasks I have to quite often switch between different versions of Adobe InDesign (CS6, CC, CC 2015) and thought that I can have one repository of scripts (JavaScript) that can be accessible to all the versions of InDesign.

Here’s the example that works for Adobe InDesign 2015:

tell application "Adobe InDesign CC 2015"
   do script "Macintosh HD:Users:myUsername:Documents:Scripts:SomeScript.jsx" language javascript
end tell

Now in order launch the same script in CS6 (or other), I have to change:
tell application "Adobe InDesign CS6"

Now, I thought I could get Applescript to see what the active InDesign (theApp) is and run the script that way:

tell application "System Events"
set theApp to ((path to frontmost application) as text)
tell application theApp
   do script "Macintosh HD:Users:myUsername:Documents:Scripts:SomeScript.jsx" language javascript
   end tell
end tell

But this doesn’t seem to work and will result in the error:

/var/folders/62/mm456bv51s197w72nbc2rtn8r_khhm/T/Keyboard-Maestro-Script-62493417-A7CA-4085-892C-D489B667981D:256:264: script error: Expected end of line but found identifier. (-2741)

Any ideas welcome!

This approach won't work most of the time, because the AppleScript compiler does not know the actual app that will be used.

So, you have to setup a bunch of switch cases (but AppleScript doesn't have "Switch"), so use "if/else if":

##applescript -- Adjust as needed

tell application "System Events"
  set appPath to ((path to frontmost application) as text)
end tell

set cmdStr to "Macintosh HD:Users:myUsername:Documents:Scripts:SomeScript.jsx"

### ADJUST ACTUAL APP NAME/ID AS NEEDED ###
#  Remove the # Comment marker below to use the tell application

if (appPath contains "Adobe InDesign CC 2015") then
  #  tell application "Adobe InDesign CC 2015" to do script cmdStr language javascript
  
else if (appPath contains "Adobe InDesign CS6") then
  #  tell application  "Adobe InDesign CS6" do script cmdStr language javascript
  
  --- TEST:  I don't have either of the above ---
  
else if (appPath contains "Acrobat XI Pro") then
  tell application id "com.adobe.Acrobat.Pro"
    #  do script cmdStr language javascript
    display notification "tell com.adobe.Acrobat.Pro"
  end tell
  
end if

(*
FOR ADOBE ACROBAT XI

NAME:  Acrobat
PATH:  Macintosh HD:Applications:Adobe Acrobat XI Pro:Adobe Acrobat Pro.app:
ID:    com.adobe.Acrobat.Pro
*)

Please let us know if this works for you.

You may find the below script helpful to get the info about the frontmost app.
Put this script in a Macro ("Execute AppleScript" Action) in a Global Group, and trigger when the app of interest is frontmost.

##applescript -- Get Info of FrontMost App

tell application "System Events"
  tell (first process whose frontmost is true)
    set appName to displayed name
    set appPath to ((path to frontmost application) as text)
  end tell
end tell

tell application appPath
  set appID to id
end tell

return ("NAME: " & appName & return & "PATH: " & appPath & return & "ID: " & appID)

For convenience, here's the macro that uses this script:

##Macro Library   Get Info about Current FrontMost App (Name, Path, ID) @Example


####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/a/adb5feac24c673c91de8c130c0730a931334bb0a.kmmacros">Get Info about Current FrontMost App (Name- Path- ID) @Example.kmmacros</a> (2.3 KB)

---

<img src="/uploads/default/original/2X/e/e14990073fa16e0a0b357ef659c2eb30b0d2ce9e.png" width="502" height="840">
2 Likes

Hey Peter,

Tell-by-reference NEVER works – UNLESS the AppleScript terminology you're using is universally available.

The reason we have tell-blocks to begin with is to make that terminology available to the script.

This happens at compile-time – NOT run-time.

There are a variety of workarounds, but nothing is all that neat and clean.

Probably your best bet is to use InDesign's application id like so:

tell application id "com.adobe.InDesign"
-- rest of code here
end tell

This should talk to the running version of the application.

If that doesn't work report back, and we'll investigate a bit more.

-Chris

2 Likes

Perfect! Thank you :slight_smile:

Thanks for that info. I wasn’t aware of that subtle difference.

As we have discussed, you can't use an approach with setting the "tell application" app dynamically, like this:

tell application "System Events"
set theApp to ((path to frontmost application) as text)
tell application theApp
   do script "Macintosh HD:Users:myUsername:Documents:Scripts:SomeScript.jsx" language javascript
   end tell
end tell

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.

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

##applescript "tell application" Blocks

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

--- 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"

I see where you going - I am fortunate to need the solution for InDesign and recently using it for Illustrator too. I have to say that @ccstone solution works very well. Obviously you have to be careful if you have two versions of InDesign or Illustrator opened (the newest version always be prioritised).

Thank to all of you for the support!