How can the "Choose application" dialog be supressed ?

I have a script which runs in a macOS Service. It needs to run in the user’s choice of web browser. This is the relevant part of the script:

 set app_name to short name of (info for (path to frontmost application))
if app_name is "Safari" then
	using terms from application "Safari"
		tell application app_name
			set video_URL to URL of current tab of first window
		end tell
	end using terms from
else if app_name is "Firefox" then
	tell application app_name to activate
	tell application "System Events"
		keystroke "l" using command down
		keystroke "c" using command down
	end tell
	delay 0.5
	set video_URL to the clipboard
else if app_name is "Opera" then
	tell application app_name
		set video_URL to URL of front document as string
	end tell
else if app_name is "Google Chrome" then
	using terms from application "Google Chrome"
		tell application "Google Chrome"
			set video_URL to URL of active tab of front window
		end tell
	end using terms from
end if

I don’t have Opera or Chrome installed. When I run the service I always get a “Where is Google Chrome?” dialog. I never get a similar dialog for Opera (I don’t remember ever having it installed).

I have tried various ideas mentioned on MacScripter and StackOverflow including: adding a “using terms from” block; adding a “try/end try” block; changing from app “short name” to “bundle identifier”; changed test from “Google Chrome” to “Chrome”. Nothing I’ve tried has worked. I always get the “Where is” dialog.

Is there a way of suppressing that “Where is…” dialog ?

Thanks.

One way is to metascript it.


set app_name to short name of (info for (path to frontmost application))
if app_name is "Safari" then
	set videoURL to run script "
			tell application \"Safari\"
			return URL of current tab of first window
		end tell"
else if app_name is "Firefox" then
	tell application app_name to activate
	tell application "System Events"
		keystroke "l" using command down
		keystroke "c" using command down
	end tell
	delay 0.5
	set video_URL to the clipboard
else if app_name is "Opera" then
	set videoURL to run script "
tell application \"Opera\"
		return URL of front document as string
	end tell"
else if app_name is "Chrome" then
	set videoURL to run script "
		tell application \"Google Chrome\"
			return URL of active tab of front window
		end tell"
end if

Use the tell application id… form, and wrap it in a try block.

Shane, t.spoon, many thanks. Another option is GUI scripting but that has permissions complications.

I did try the bundle identifier idea inside a try block but forgot to include the “id” in

Apologies for not seeing my goof.

Garry

Have been trying all morning but can’t get this to work if using bundle identifier approach and Chrome is not installed. For example,

tell application id "com.google.Chrome"

returns the error “Can’t get application id “com.google.Chrome”.”.

In some situations I get the “Expected end of line but found property” error which has been discussed elsewhere https://leancrew.com/all-this/2011/09/applescript-browser-tab-weirdness/. Solutions offered in that discussion don’t work when Chrome is not installed.

I have the same problem with Chromium (which is predictable).

I don’t understand why I have little trouble with the code testing for Opera including on a Mac which has never had Opera installed.

I’m going back to t.spoon’s suggestion.

Cheers.

That’s why I said to wrap it in a try block. Then if you get an error you know the app isn’t present, so you can just ignore it.

Yes, and using “use terms from” is a bit pointless if the app is not installed. I did test a try block in various combinations (just not shown in my example code above). I think the “Expected end of line but found property” error still appeared and prevented compilation. Anyway, I’ll give it a try.

Cheers.