Friday, October 22, 2021

#1 2021-10-09 12:19:11 am

lotr
Member
Registered: 2011-08-22
Posts: 109
Website

Check if application exists with apps folder

Check if application exists with apps folder. Is there such a way that does not cause the script to error when the app does not exist? My approach almost doubles the check by initially working with error, but this still brings up a script error.

This additional check routine was a later thought & is not a necessity but be handy if user makes an error with program selection. The whole script handles 5 different programs, each with their own specific needs. This Appfind routine is simply a check to see if the app is installed within the apps folder. (If not the user is notified and asks if they made correct selection.)

Applescript:

on XXXAppfind() -- after selecting xxx, checks if xxx is installed in Apps folder.
   
   tell application "Finder"
       try
           set xxxapp to ((path to applications folder as text) & "xxx.app") as alias
       on error
           my xxxCheck() -- double-checks with user they made correct selection for program earlier on.
       end try
   end tell
   
   tell application "Finder"
       
       set xxxapp to ((path to applications folder as text) & "xxx.app") as alias
       
       if exists file xxxapp then --- checks to see if the user has xxx within app folder.
           return true
           my xxxSettingsfolder()
           my xxxSomethinginstall()
           
       else if not (exists file xxxapp) then
           my xxxCheck() -- double-checks with user they made correct selection for program earlier on.
       end if
   end tell
   
end XXXAppfind

Is there a better approach?

Browser: Firefox 78.0
Operating System: macOS 10.11


The shy can light the dull dark room rich with life when their eyes mirror their inner sunrise

Offline

 

#2 2021-10-09 03:33:25 am

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 134

Re: Check if application exists with apps folder

It's difficult for me to work with your code as I have no idea what xxxcheck is doing. Sometimes there are issues with variables and functions that may cause errors and you don't say what error you're getting.

That said, what about just asking if the path to the app exists? These shouldn't generate an error.

Applescript:

tell application "Finder" to exists (path to applications folder as text) & "Firefox Nightly.app"
--> true

tell application "Finder" to exists (path to applications folder as text) & "Firefox Night.app"
--> false

If false, you can re-check with the user or do whatever. If true, you can start working with the app:

Applescript:


set xAppN to "Firefox Nightly.app"
tell application "Finder"
   if exists (path to applications folder as text) & xAppN then
       set xApp to application id "org.mozilla.nightly"
       tell xApp to activate
   else
       display dialog "[" & xAppN & "]" & " … doesn't exist so try again"
   end if
end tell

On another note, I'm not sure how you're presenting the app choice to the user but could you not simply confirm whether the apps exist at the beginning of your script and then only present those that do? Assuming they're on the local system, it shouldn't be sluggish.

Offline

 

#3 2021-10-09 04:01:58 am

lotr
Member
Registered: 2011-08-22
Posts: 109
Website

Re: Check if application exists with apps folder

Thank you. That works.

It's set up to use a List selector. Your idea of only offering those apps that exist is an impressive idea.


The shy can light the dull dark room rich with life when their eyes mirror their inner sunrise

Offline

 

#4 2021-10-09 04:17:23 am

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 134

Re: Check if application exists with apps folder

Glad it helps. It might be worth playing around with the various 'application' commands as some will trigger the app's launching and some will provide the path to the app. Also, sometimes if the app can't be determined then a 'choose app' dialogue will open.

You can find some details here in the Class Reference section of the Language Guide:

    https://developer.apple.com/library/arc … 3-CH1g-SW2

Offline

 

#5 2021-10-09 08:08:37 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1089

Re: Check if application exists with apps folder

lotr wrote:

Check if application exists with apps folder.... Is there a better approach?



Mockman has answered the OP's question but I thought I would add a quick comment FWIW. The use of an alias in a try statement can be a simple and efficient way to check if an item exists. For example:

Applescript:

set theApp to "Safari.app" --> true
set theApp to "afari.app" --> false

try
   set theApp to alias ((path to applications folder as text) & theApp)
   set theAppExists to true
on error
   set theAppExists to false
end try

I ran a timing test with Script Geek and using an alias in a try statement to check if an application file exists takes 1 millisecond while using Finder's exists command takes 11 milliseconds.

Last edited by peavine (2021-10-09 08:27:49 am)


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

#6 2021-10-09 07:14:23 pm

lotr
Member
Registered: 2011-08-22
Posts: 109
Website

Re: Check if application exists with apps folder

Ever since Mockman’s first answer, I had been wondering how effective that approach might be.
Reason: The installer I’ll be providing will also be provided with the specific program installer where possible.

I recall the Run Once issue with using program ID, either Run Once or Run Once AND either reboot/relog before the system recognises the program ID - - I’m not sure MacOS has changed in this regard over the years. I decided not to comment about this at the time. But he did suggest looking at alternatives. These could include either or both existence of program & settings. (Always a chance someone has deleted settings folder to solve a performance issue or has not yet run the program due to initial installation.) It gets tricky for using my script directly after first installation of the program itself. ~ Edit: In regards to only showing options of programs detected on the system.

Last edited by lotr (2021-10-09 07:18:34 pm)


The shy can light the dull dark room rich with life when their eyes mirror their inner sunrise

Offline

 

#7 2021-10-10 12:47:54 am

lotr
Member
Registered: 2011-08-22
Posts: 109
Website

Re: Check if application exists with apps folder

This was my original script before adding the Error handling at beginning (as a test.)
Problem was the setting to location if it did not exist. The combo installer would error, more-so if the installer script looped back to a previous menu dialog by simply choosing that routine in the dialog options.
Looking at the alternate ways you have provided, the answer appears to be to simply set location in a try statement. Something I should be doing anyway I guess.

Below is the original script routine that was erroring:

Applescript:

on BBAppfind() -- after selecting app from selection options, checks if app is installed in Apps folder.
   set BBapp to ((path to applications folder as text) & "BB.app") as alias
   tell application "Finder"

       if exists file BBapp then --- checks to see if the user has BB within app folder.
           return true
           my BBChoices()
           
       else if not (exists file BBapp) then
           my BBCheck()
       end if
   end tell
   
end BBAppfind

Thanks to both of you for your help.

Last edited by lotr (2021-10-10 12:49:19 am)


The shy can light the dull dark room rich with life when their eyes mirror their inner sunrise

Offline

 

#8 2021-10-10 01:43:42 am

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

Re: Check if application exists with apps folder

In principle, the OP is pleased with the answer, but I want to show the following for those users who, like me, would like to check the existence of the application on the computer as a whole, and not just in the Applications folder. Because the application is either known to the system (installed) or not (not installed) and very often not in the Applications folder. Also, the application can be located inside some collection of applications, located in the Applications folder.

The script takes more time (1.5 seconds for me), but it gives a more professional approach.

Applescript:


return my appExistsByName("Safari") -- true

on appExistsByName(appName)
   -- chek if application is installed (anywhere) by its name
   set theAppInfo to (do shell script "system_profiler SPApplicationsDataType" without altering line endings)
   theAppInfo contains linefeed & linefeed & space & space & space & space & appName & ":" & linefeed & linefeed & space & space & space & space
end appExistsByName

Last edited by KniazidisR (2021-10-10 10:55:29 pm)


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

Offline

 

#9 2021-10-10 04:53:37 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 901

Re: Check if application exists with apps folder

I find this approach to be useful but that also means the user need to know the bundle id of
the target application.

UPDATED: To include different approaches to get status.

Applescript:

use framework "AppKit"
use scripting additions

log status of (its checkAppByBundleID:"com.apple.Terminal")
log status of (its checkAppByBundleID:"com.apple.Terminal1")

set theCheckAppByBundleID to (its checkAppByBundleID:"com.apple.12345")
set theStatus to status of theCheckAppByBundleID
if theStatus = "false" then errorMessage of theCheckAppByBundleID

on checkAppByBundleID:theBundleID
   set bundleWithIdentifier to (current application's NSWorkspace's sharedWorkspace())'s absolutePathForAppBundleWithIdentifier:theBundleID
   if not bundleWithIdentifier = missing value then
       return {status:"true", path:(bundleWithIdentifier as text)} as record
   else
       return {status:"false", errorMessage:"Couldn't find the Application..."} as record
   end if
end checkAppByBundleID:

Last edited by Fredrik71 (2021-10-10 07:52:38 am)


if you are the expert, who will you call if its not your imagination.

Offline

 

#10 2021-10-10 05:08:03 am

lotr
Member
Registered: 2011-08-22
Posts: 109
Website

Re: Check if application exists with apps folder

KniazidisR wrote:

... The script takes more time (1.5 seconds for me), but it gives a more professional approach.

Thanks for an alternative. But strange results.
I timed it at 61 seconds on my system. big_smile
Is the result supposed to be false? I'm guessing it is.

I intentionally searched for made-up names but the script resulted in false and even gave me strange locations.

I tested it for Firefox & it gave me       Location: /Volumes/My Applications/Applications/Adobe After Effects CS5/Adobe After Effects Render Engine.app

Safari also resulted in false.

Yeah I do have a drive for several 3rd party apps, something macos did not mind occurring in the old days. I also have numerous apps on my system, including source codes, some of which have multitudes of old versions. I'm guessing this is one reason the search takes so long on my older system. But my computer is not the average joe system.

Last edited by lotr (2021-10-10 05:10:24 am)


The shy can light the dull dark room rich with life when their eyes mirror their inner sunrise

Offline

 

#11 2021-10-10 05:51:05 am

lotr
Member
Registered: 2011-08-22
Posts: 109
Website

Re: Check if application exists with apps folder

Fredrik71 wrote:

I find this approach to be useful but that also means the user need to know the bundle id of
the target application. ...

Thanks.
Appears to work for apps I am sure I have not utilized and is super fast. Even able to find apps on mounted images.


The shy can light the dull dark room rich with life when their eyes mirror their inner sunrise

Offline

 

#12 2021-10-10 05:53:09 pm

lotr
Member
Registered: 2011-08-22
Posts: 109
Website

Re: Check if application exists with apps folder

Apologies to subscribers & moderators. I've decided to put my somewhat off-topic query into a new topic thread re: Script Additions & end users without scripting additions.

Last edited by lotr (2021-10-10 06:47:44 pm)


The shy can light the dull dark room rich with life when their eyes mirror their inner sunrise

Offline

 

#13 2021-10-10 11:00:26 pm

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

Re: Check if application exists with apps folder

lotr wrote:
KniazidisR wrote:

... The script takes more time (1.5 seconds for me), but it gives a more professional approach.

Thanks for an alternative. But strange results.


I updated the script, because it had 2 errors: 1) 4 spaces were forgotten on the left-right side of the application name 2) I forgot to instruct the do shell script so that it would not change the endings of the lines.


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

Offline

 

#14 2021-10-11 12:30:13 am

lotr
Member
Registered: 2011-08-22
Posts: 109
Website

Re: Check if application exists with apps folder

KniazidisR wrote:

I updated the script, because it had 2 errors: 1) 4 spaces were forgotten on the left-right side of the application name 2) I forgot to instruct the do shell script so that it would not change the endings of the lines.

Thanks that works. smile (Though still takes similar time period. I also generally run quite a few apps simultaneously which may slow things down. The slowness on my system will probably not be how others experience your approach.)

@Fredrik71: Thanks, I appreciate the edited version. wink

Last edited by lotr (2021-10-11 12:31:56 am)


The shy can light the dull dark room rich with life when their eyes mirror their inner sunrise

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)