Mac AppleScript - Preventing "Choose Application" dialog

floyde

macrumors 6502a
Original poster
Apr 7, 2005
808
1
Monterrey, México
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:
Code:
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
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
floyde said:
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
Code:
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:
Code:
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

macrumors G4
Nov 3, 2005
10,410
10,695
UK
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?
Code:
--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

macrumors 6502a
Original poster
Apr 7, 2005
808
1
Monterrey, México
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 :eek: I found an interesting approach 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:

Code:
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

macrumors G4
Nov 3, 2005
10,410
10,695
UK
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

macrumors 68000
Feb 1, 2005
1,887
1
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.

Code:
set mypath to path to me
open <filename> of mypath
a 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

macrumors newbie
Feb 14, 2008
8
0
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.