Sunday, May 9, 2021

#1 2021-04-11 04:17:47 pm

MitchBVI
Member
Registered: 2006-02-14
Posts: 261

Pass Application as a variable

I am trying to check if an application is running. This script will not work if I try and pass the application as a variable.

Applescript:

--CheckAppOpen
--Checks if an appllication is open
display dialog "Enter Name of app or accept default " buttons {"Cancel", "OK"} default button "OK" default answer "Microsoft Excel"
set appName to text returned of result
tell application "System Events"
   if (get name of every application process) contains appName then
       tell application "Microsoft Excel"--Works when specific not when passed as a variable
           quit
       end tell
   else
       display dialog "is not" buttons {"Cancel", "OK"} default button "OK"
   end if
end tell

If I try and use the variable "appName" the script runs but does not close the application. I guess its something simple I am missing. I have tried text, string and specifier etc.

Offline

 

#2 2021-04-11 07:15:32 pm

wch1zpink
Member
Registered: 2011-08-20
Posts: 16

Re: Pass Application as a variable

This should work

Applescript:

set appName to text returned of (display dialog "Enter Name of app or accept default " buttons ¬
   {"Cancel", "OK"} default button "OK" default answer "Microsoft Excel")
tell application "System Events"
   if (get name of every application process) contains appName then
       quit my application appName
   else
       display dialog "is not" buttons {"Cancel", "OK"} default button "OK"
   end if
end tell

Last edited by wch1zpink (2021-04-11 07:24:50 pm)

Online

 

#3 2021-04-11 11:00:07 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1788

Re: Pass Application as a variable

No need System Events, and processes. Many applications have names that differ from their process name, and this will create additional problems for you..

Applescript:


set appName to text returned of (display dialog "Enter Name of app or accept default " buttons {"Cancel", "OK"} default button "OK" default answer "Microsoft Excel")

if running of application appName then -- THIS
   tell application appName to quit
else
   display dialog "Application \"" & appName & "\" is not running" buttons {"Cancel", "OK"} default button "OK"
end if

Better, using display notification:

Applescript:


set appName to text returned of (display dialog "Enter Name of app or accept default " buttons {"Cancel", "OK"} default button "OK" default answer "Microsoft Excel")

if running of application appName then -- THIS
   tell application appName to quit
else
   display notification "Application \"" & appName & "\" is not running" with title "notification:"
end if

Last edited by KniazidisR (2021-04-11 11:40:59 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#4 2021-04-12 10:20:48 am

MitchBVI
Member
Registered: 2006-02-14
Posts: 261

Re: Pass Application as a variable

Thank you both and of course both work.

KniazidisR, I have had the application and process name problems in the past so your solution works better for me.

wch1zpink, for the life of me I do not understand why your's works and mine does not. You have removed my tell block but you seem to be saying the same thing as mine with the addition of "my".And as I said in my post using the actual name rather than passing it as a variable worked.

Anyway thank you again. without a doubt this is the best site for solving AppleScript issues.

Offline

 

#5 2021-04-12 11:53:32 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1788

Re: Pass Application as a variable

MitchBVI wrote:

wch1zpink, for the life of me I do not understand why your's works and mine does not. You have removed my tell block but you seem to be saying the same thing as mine with the addition of "my".And as I said in my post using the actual name rather than passing it as a variable worked.


Since no explanation has yet been received from wch1zpink, I will try to give an answer.

In your original script, the tell application myApp block is nested within the "System Events" tell block. That is, you tell AppleScript to look for application myApp of application "System Events", which cannot be. All applications are children of the current applicaton, which in this case is the script editor.

The working script for post #2 uses the my specifier, which tells AppleScript to look for myApp among the children of the script editor, not among the children of "System Events". That's why it works.

So, your original script should work with me or my specifier:

Applescript:


--CheckAppOpen
--Checks if an appllication is open
display dialog "Enter Name of app or accept default " buttons {"Cancel", "OK"} default button "OK" default answer "Microsoft Excel"
set appName to text returned of result
tell application "System Events"
   if (get name of every application process) contains appName then
       tell application appName of me --or, tell my application appName
           quit
       end tell
   else
       display dialog "is not" buttons {"Cancel", "OK"} default button "OK"
   end if
end tell

Last edited by KniazidisR (2021-04-12 12:03:25 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#6 2021-04-15 12:39:22 pm

MitchBVI
Member
Registered: 2006-02-14
Posts: 261

Re: Pass Application as a variable

Understand and again my apologies for the delay in responding to your post

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)