What is Causing Macro Script Error "Application Isn't Running"?

@peternlewis,

I have an AppleScript which consistently runs perfectly without error from Script Debugger 7, but I get a Macro error (NOT a script error) when running from KM:
Application isn’t running

Any ideas or suggestions to fix or workaround would be greatly appreciated.

Here's the complete error msg from the KM Engine Log.
Note I am logging errors from the script (see below) and I do NOT get any script errors.

--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
MACRO ERROR OCCURRED on 2020-09-09 21:50:30
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This info was pulled from the KM Engine Log for the
Last Macro error that occurred.

MACRO: Periodic Process Of Outlook 365 Inbox @OL

Action Type: Execute Script
Action Name: Execute an AppleScript Update Subject of Outlook 365 Messages From EN Forum @OL

ACTION ERROR: Keyboard Maestro Engine got an error: Application isn’t running. (-600) In macro “Periodic Process Of Outlook 365 Inbox @OL” (while executing SCRIPT: Update Subject of Outlook 365 Messages From EN Forum (AS)).
Script Path: /Users/Shared/Dropbox/SW/DEV/KM/Scripts/Update Subject of Outlook 365 Messages From EN Forum @OL.scpt

KM Engine Log:
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
Execute an AppleScript failed with script error: /Users/Shared/Dropbox/SW/DEV/KM/Scripts/Update Subject of Outlook 365 Messages From EN Forum @OL.scpt: execution error: Keyboard Maestro Engine got an error: Application isn’t running. (-600) In macro “Periodic Process Of Outlook 365 Inbox @OL” (while executing SCRIPT: Update Subject of Outlook 365 Messages From EN Forum (AS)).
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

Log File: ~/Library/Logs/Keyboard Maestro/Engine.log

Here's the script:

property ptyScriptName : "Update Subject of Outlook 365 Messages From EN Forum"
property ptyScriptVer : "1.1" --  ADD Option to Process Selected Messages
property ptyScriptDate : "2020-09-09"
property ptyScriptAuthor : "JMichaelTX"

(*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PURPOSE:  

KM VARIABLES REQUIRED: (must be set before calling this script)
  • KM Var Name
  
KM VARIABLES SET: (by this script)
  • KM Var Name
  
REQUIRED:
  1.  macOS El Capitan 10.11.6+
      (may work on Yosemite 10.10.5, but no guarantees)
      
  2.  Mac Applications
        • Keyboard Maestro 7.3+
        
  3.  EXTERNAL OSAX Additions/LIBRARIES/FUNCTIONS
        • Name of OSAX/Lib/Handler
          
  4.  INTERNAL HANDLERS (Functions):
        • on getKMVar(pKMVarNameStr, pDefaultValueStr) -- Get KM Variable; set to default if doesn't exist
        • Handler Name

INSTALLATION:   See http://tinyurl.com/install-as

REF:  The following were used in some way in the writing of this script.
  1.  
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
*)
use AppleScript version "2.4"
use scripting additions

property LF : linefeed

--- TO BE RETURNED TO KM ---
--  Will be either actual results, or Error message
set scriptResults to "TBD"

try
  --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  set kmInst to system attribute "KMINSTANCE"
  tell application "Keyboard Maestro Engine"
    set sourceOfMsg to getvariable "Local__SourceOfMsg" instance kmInst
  end tell
  
  set frontApp to path to frontmost application as text -- use for dialogs
  
  tell (current date) to set dateNow to it - (its time)
  
  tell application "Microsoft Outlook"
    
    if (sourceOfMsg = "Selection") then
      
      --- PROCESS ONLY USER SELECTED MESSAGES ---
      set msgList to current messages
      set processSelection to true
      
    else
      
      --- PROCESS LAST NNN Messages ---
      
      set processSelection to false
      
      set folderList to mail folders whose name contains "Inbox"
      set oInBox to item 3 of folderList -- ApolloBiz Account
      
      set msgList to messages 1 thru 200 of oInBox -- gets the last 200 msg received
      
    end if --(sourceOfMsg = "Selection")
    
    set numMsg to 0
    
    repeat with oMsg in msgList
      set subjectStr to subject of oMsg
      --log subjectStr
      
      set enBeta to false
      
      if (subjectStr does not start with "[EN Mac Beta]") then
        
        set oSender to sender of oMsg
        set senderName to name of oSender
        
        if (senderName contains "Evernote User Forum") then
          set msgBody to plain text content of oMsg
          if (processSelection or (msgBody contains "beta")) then set enBeta to true
        end if
        
      end if -- (subjectStr does not start with "[EN Mac Beta]")
      
      if (enBeta) then
        
        set newSubjectStr to "[EN Mac Beta] " & subjectStr
        set subject of oMsg to newSubjectStr
        set numMsg to numMsg + 1
        
      end if -- 
      
    end repeat
    
  end tell
  
  set scriptResults to (numMsg as text) & " Messages Updated"
  
  if (numMsg > 0) then
    set msgStr to (numMsg as text) & " Messages Updated"
    set msgTitleStr to ptyScriptName
    display notification msgStr with title msgTitleStr sound name "Tink"
    
  end if
  
  --~~~~~~~~~~~~~ END TRY ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  
  --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  --»                          ON ERROR
  --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on error errMsg number errNum
  
  if errNum = -128 then ## User Canceled
    set errMsg to "[USER_CANCELED]"
  end if
  
  set scriptResults to "[ERROR] [MACRO CANCELLED] " & errMsg
  
  --tell application "System Events"
  --  set gCurrentApp to name of first application process whose frontmost is true
  --end tell
  --  
  --tell application gCurrentApp
  display notification errMsg ¬
    with title ("SCRIPT:  " & ptyScriptName & " " & ptyScriptVer) ¬
    subtitle "[ERROR] MACRO CANCELLED " sound name "glass"
  --end tell
  
  my postScriptLog(ptyScriptName, ptyScriptVer, errMsg)
  
  --»  THROW ERROR ~~~~~~~
  error scriptResults
  
end try
--~~~~~~~~~~~~~~~~END ON ERROR ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- RETURN THE RESULTS TO THE KM EXECUTE SCRIPT ACTION ---
return scriptResults

--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--    HANDLERS (functions)
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

on postScriptLog(pScriptName, pScriptVer, pErrMsg)
  
  set isoDT to ((current date) as «class isot» as string)
  set isoDT to (text 1 thru 10 of isoDT) & " " & (text 12 thru -1 of isoDT)
  
  set errLog to isoDT & tab & "SCRIPT:  " & pScriptName & "  " & pScriptVer & tab & "ERROR:  " & pErrMsg
  
  tell application "Keyboard Maestro Engine"
    set kmScriptLog to getvariable "DND_APO__ScriptLog"
    set kmScriptLog to errLog & LF & LF & kmScriptLog
    setvariable "DND_APO__ScriptLog" to kmScriptLog
  end tell
  
end postScriptLog

on getIsoDate(pType)
  set isoDT to ((current date) as «class isot» as string)
  if pType = "DT" then
    set isoDT to (text 1 thru 10 of isoDT) & " " & (text 12 thru -1 of isoDT)
  else
    set isoDT to (text 1 thru 10 of isoDT)
  end if
  return isoDT
  
end getIsoDate


on getStringBetweenText(pSourceStr, pStartStr, pEndStr)
  
  set startIndex to 1
  if (pStartStr ≠ "") then
    set startIndex to offset of pStartStr in pSourceStr
  end if
  set endIndex to offset of pEndStr in pSourceStr
  
  set foundStr to ""
  if ((startIndex > 0) and (endIndex > 0)) then
    set startIndex to startIndex + (length of pStartStr)
    set foundStr to text startIndex thru endIndex of pSourceStr
  end if
  
  return foundStr
  
end getStringBetweenText

--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on getKMVar(pKMVarNameStr, pDefaultValueStr) --  @KM @Get @Variable
  (*  VER: 1.0    2017-03-15
---------------------------------------------------------------------------------
  PURPOSE:  Get Value of KM Variable & Set to Default if it doesn't exist
  PARAMETERS:
    • pKMVarNameStr        | text  | Keyboard Maestro Variable Name
    • pDefaultValueStr    | text  | Default value if Variable doesn't exist
  RETURNS:  KM Var Value
  AUTHOR:  JMichaelTX
  —————————————————————————————————————————————————————————————————————————————————
*)
  local kmVarStr
  
  tell application "Keyboard Maestro Engine"
    set kmVarStr to getvariable pKMVarNameStr
  end tell
  if (kmVarStr = "") then set kmVarStr to pDefaultValueStr
  
  return kmVarStr
  
end getKMVar
--~~~~~~~~~~~~~~~ END OF handler getKMVar ~~~~~~~~~~~~~~~~~~~~~~~~~


Here's the Macro:

NOTE: it is the SECOND script that is failing

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MACRO:   Periodic Process Of Outlook 365 Inbox @OL

-~~~ VER: 1.0    2020-09-09 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Periodic Process Of Outlook 365 Inbox @OL.kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


I'm afraid I don't know.

OSStatus says -600 is procNotFound, so presumably it is not finding an application.

A quick look at your script seems to show it using two application, Keyboard Maestro Engine and Microsoft Outlook, so I would guess it is one of them (or any other application I didn't see in the script).

I would try a number of things to narrow it down:

  • Run it in the Terminal using osascript - that is essentially what Keyboard Maestro does, so that could eliminate Keyboard Maestro entirely as part of the issue. Otherwise, if it works there then it may be a difference in the two environments.
  • Try some printf debugging (I told you that is what I always use, since it works in any environment). Write a subroutine to log a line of text to a file, and then scatter them through your code and see what it is actually doing up to the point of the failure.