Opening an application from a text box entry

I’m having trouble with this… I need it to open the application when you type in a box in xcode (ill do that part later), the problem is, the script just doesnt work.

set the_variable to "Safari"
tell application "Finder"
	set matching_apps to (name of every application whose name contains the_variable) as list
	if the (count of matching_apps) < 1 then
		set my_app to item of matching_apps as text
		open application my_app
	else
		display dialog "un error"
	end if
end tell

This works for me.

set the_variable to "Safari"
tell application the_variable to activate

– Rob

the problem with that is that the application would have to be scriptable, and the user would have to enter the full name of the application (eg. “Adobe Photoshop 7.0” instead of “photoshop”)

Hi,

What if you use the ‘choose application’ command:

set the_app to (choose application)
tell the_app to activate

I think most apps will open even if they aren’t scriptable. Or, have the user choose from a list of app names.

gl,

Depending on your exact needs, maybe one of these will work. The second example works on most apps that haven’t been assigned a creator code (such as Apple’s Address Book).

set the_variable to "Safari"

if the_variable is "Safari" then
	try
		tell application "Finder" to open application file id "sfri" -- creator code
	on error
		-- not found
	end try
end if

-- OR --
set the_variable to "Safari"

if the_variable is "Safari" then
	try
		tell application "Finder" to open application file id "com.apple.safari" -- CFBundleIdentifier
	on error
		-- not found
	end try
end if

– Rob

hmmm… but that requires me to have an if statement for every appplication, and i don’t know what applications people might have

hmmm… but that requires me to have an if statement for every appplication, and i don’t know what applications people might have

Rob’s initial suggestion works best, I think. Just about every app, even if it isn’t scriptable, accepts high AppleEvents (activate, quit, etc) so the scripatbility thing isn’t an issue. If the user doesn’t enter the exact name of the application, AppleScript will be kind enough to present them with a dialog listing every known app on their machine–much more quickly than you could create it in your script–and they can click the browse button to quickly go to the actual file if they know where it is.

You could also make your app “learn” this new name so that if the user enters the name once and AppleScript can’t resolve it and asks the user to choose the app, you trap what app becomes active so the next time the user enters that app name, you can just go to that app path.

Jon