tell an application -- but only if user has that app installed

How does one get Applescript Studio to issue commands to an optional application that a user may have installed, if they have installed i, without forcing them to installing that application?

For example, suppose a button that says “get url from NetNewsWire” and one of my test users doesn’t have it.
If I just use:

try
	tell application newsReader
		set myURL to selectedHeadline
	end tell
end try

My alpha tester can’t even boot my application – he gets stuck at a “Where is NetNewsWire” splashscreen.

Conceptually, what I would like to do is to an issue a command that says (for example) something like this:

newReaderApp = "NetNewsWire.app"
if exists application newsReaderApp then
	tell application newsReaderApp
		set myURL to selectedHeadline
	end tell
end if

But none of the variants on this I have tried “ including “double tells” and “using terms” “ seem to work; everything seem to either (rock) require the user to have NNW to even get my program started or else (hard place) to not to properly pass the contents of the tell to NNW.

The simple

tell application newsReader
	set myURL to selectedHeadline
end tell

works fine if I want to compile only for myself, but not to help with users who lack NNW.

Any way of navigating between these twin perils would be much appreciated.

Hi calendrica,

Try this. First see if your app has a creator type:

set f to choose file
file creator of (info for f)

After running that, use the alphanumeric, four character code to get a reference to your app and see if it exists. For instance, I chose Acrobat Reader and got “CARO”. Then:

tell application “Finder”
set appExists to (exists application file id “CARO”)
end tell
if appExists then
– do something
else
– do something else
end if

Your app may not have a file creator. Then this wouldn’t work.

gl,

Give this a try. I just tested it quickly after writing it and it seems to work well enough.

Specify the application name without the .app in the property.
You probably have to save your properties with defaults write (for AS Studio), or your script will keep requesting the supporting application even if the user has confirmed they have it.

property someAppName : "xyzzy"
property hasApplication : false

using terms from application "xyzzy"
	if hasApplication is false then
		-- once the app has been found, we stop checking our own way
		set hasApplication to appExists(someAppName)
	end if
	if hasApplication then
		tell application someAppName
			-- do something weird
		end tell
	end if
end using terms from

on appExists(appName)
	try
		-- is it running?
		tell application "System Events"
			if (name of processes) contains appName then return true
		end tell
		-- is it in a standard location on the hard drive?
		tell application "Finder"
			--if exists file (appName & ".app") of (path to applications folder) then return true
			--this should work regardless of whether the app name end in .app :
			if exists file (appName) of (path to applications folder from system domain) then return true
			if exists file (appName) of (path to applications folder from user domain) then return true
			if exists file (appName) of (path to applications folder from local domain) then return true
		end tell
		display dialog "The application \"" & appName & "\" was not found." & return & "If you have this application, click Continue to launch it (you may have to select it from a list of applications)." buttons {"Cancel", "Continue"} default button 2
		if the button returned of the result is "Cancel" then error number -128
		return true
	on error
		return false
	end try
end appExists

Thanks, PCheese! You’re a genius – that worked perfectly, not only solving the exists problem (as fellow macscrpter member Kel had) but getting at my core problem: how to use a “tell” statement for a possibly installed application without demanding at run-time that the relevant application be installed…

By the way in the interim I discovered a hack – “hiding” my tell statement from the compiler like this:


	set theURL to (do shell script "osascript -e 'tell application \"NetNewsWire\" to get the URL of the selectedHeadline'")

Your solution via “using terms” is much faster.

Thanks so much.

No need for a file creator. Bundle identifiers suffice.

With OmniFocus for example, one now needs to be able to detect which version (vanilla or Mac App Store) is installed:

on GetCachePath()
	tell application "Finder"
		if exists application file id "com.omnigroup.OmniFocus" then
			return "~/Library/Caches/com.omnigroup.OmniFocus/OmniFocusDatabase2"
		else if exists application file id "com.omnigroup.OmniFocus.MacAppStore" then
			return "~/Library/Caches/com.omnigroup.OmniFocus.MacAppStore/OmniFocusDatabase2"
		else
			error "OmniFocus not found on this system ..."
		end if
	end tell
end GetCachePath