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.)

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

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.

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:


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.

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.

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/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html#//apple_ref/doc/uid/TP40000983-CH1g-SW2

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:

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.

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.

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:

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.

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.


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

Thanks for an alternative. But strange results.
I timed it at 61 seconds on my system. :smiley:
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.

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.

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.

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. :slight_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: