Friday, October 31, 2014

#1 2005-08-30 06:27:49 pm

calendrical
Member
Registered: 2005-08-30
Posts: 3

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:

Applescript:

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:

Applescript:

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

Applescript:

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.

Offline

 

#2 2005-08-31 07:18:55 pm

kel
Member
Registered: 2002-11-19
Posts: 2031

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

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,


Kel

Filed under: Finder

Offline

 

#3 2005-09-01 05:14:15 pm

PCheese
Member
Registered: 2005-07-14
Posts: 15

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

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.

Applescript:

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

Last edited by PCheese (2005-09-01 05:17:37 pm)


Filed under: Finder, System

Offline

 

#4 2005-09-06 12:10:04 pm

calendrical
Member
Registered: 2005-08-30
Posts: 3

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

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:


Applescript:


   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.

Offline

 

#5 2011-07-01 12:19:58 am

yiam-jin-qui
Member
Registered: 2010-10-10
Posts: 34

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

kel wrote:

Hi calendrica,

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,

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:

Applescript:

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

Last edited by yiam-jin-qui (2011-07-02 12:53:05 am)


天地不仁, 仁者人也

Houthakker / RobTrew

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.022 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)