PDA

View Full Version : AppleScript - Preventing "Choose Application" dialog




floyde
Jun 2, 2006, 04:58 PM
I need an AppleScript that launches Photoshop, but the users' machines have any of the following versions: CS2, CS, 7. I wrote a test script the tries to launch the most recent version. If that fails, it tries to open the next and so on:

set PS1 to "Adobe Photoshop CS2"
set PS2 to "Adobe Photoshop CS"
set PS3 to "Adobe Photoshop 7.0"

try
tell application PS1
activate
end tell
on error
try
tell application PS2
activate
end tell
on error
tell application PS3
activate
end tell
end try
end try

The problem I have, is that when an app isn't found, it launches a "Choose Application" dialog for the user to locate the executable. Is there a way to get rid of the dialog? Thanks



SC68Cal
Jun 2, 2006, 07:50 PM
Don't assign a program to a variable. That's a bad practice.

Soulstorm
Jun 3, 2006, 01:57 AM
The problem I have, is that when an app isn't found, it launches a "Choose Application" dialog for the user to locate the executable. Is there a way to get rid of the dialog? Thanks
The program does not even check wether the application specified exists. No matter whhat the application, when you write something like this: "tell application "myapp" to activate" the compiler will ask for the location of the application no matter what. There is no way to get rid of that, unless you use direct path names to the applications, which I think will make the purpose of your program useless.

One way to do what you want is to use the terminal commands. for example, when you write at the terminal open -a "disk utility" it will open that application for you not matter in what location it is. In applescript, you can do it this way:
do shell script("open -a \"disk utility\"")I think you now know what to do.
NOTE: The \" that I put is a command to display the quotes in the terminal or in the finder dialogs/

Eraserhead
Jun 3, 2006, 06:36 AM
I don't actually have photoshop installed on my system (just elements), doesn't photoshop sit inside a folder in your applications folder (like elements) and wouldn't a script like this work?

--Storing Variables
set CS2 to false
set CS to false
set Photoshop7 to false
--Getting Photoshop Folders
tell application "Finder"
set theList to the name of every folder of folder (path to applications folder) whose name contains "Photoshop"
end tell
--Processing Photoshop Folders
repeat with theItem in theList
if theItem contains "CS2" then
set CS2 to true
else if theItem contains "CS" then
set CS to true
else if theItem contains "Photoshop 7" then
set Photoshop7 to true
end if
end repeat
--Launching the Applications as appropriate
if CS2
tell application "Adobe Photoshop CS2" to activate
else if CS
tell application "Adobe Photoshop CS" to activate
else if Photoshop7
tell application "Adobe Photoshop 7.0" to activate
else
display dialog "Error, no versions of Photoshop Installed"
end if

floyde
Jun 3, 2006, 08:27 PM
Thanks guys, I knew I wasn't using the best approach, but I'm new to AppleScript and I just couldn't figure it out :o I found an interesting approach (http://applescriptsourcebook.com/articles/145_0_3_0_C/) that works even if the user renames de app.

The script uses the bundle identifier (ie com.adobe.Photoshop) to locate the app. I think all Photoshop versions, at least since 7.0, share the same bundle id. So, theoretically, the script would work with all 3 versions without the need to check which one is installed (I'm going to verify this of course). Here's the script:

tell application "Finder" to set appName to name of application file id "com.adobe.Photoshop"
launch application appName
tell application appName to ...

I was thinking I might use this script and I would also add a parameter to let the calling app (the script will be executed by another app) set the bundle identifier in case they decide to change it in future versions.
So what do you guys think, does it sound like a good idea?

Eraserhead
Jun 4, 2006, 04:40 AM
Should be workable, you just need to add an on run {Param1,Param2,...} to the top (and end to the bottom) and you can pass parameters to it just like with java.

You can even launch the script from the command line (which means you can program it into other apps.) using 'osascript <insert path of applescript here>.scpt Param1 Param2,...'.

Soulstorm
Jun 5, 2006, 06:54 AM
it's not a good idea to presume that an application sits inside a certain folder, this is bad programming technique. Instead, you can use pathnames to the application's folder.

set mypath to path to me
open <filename> of mypatha thing such as this will always work. Alternatively, the method that floyde described could work, but it has a flaw. In OS X, the files of type 'com.creatorname.appname' are created after the user has run the application for the first time. So, if a user hasn't ran one of the mentioned applications for the first time, the script won't be able to function properly.

semicolons
Feb 14, 2008, 06:27 PM
On a side note, if you want to find the id of an app, you can simply look in <app>:Contents:Info.plist under CFBundleExecutable.