Tell by variable not behaving as expected...

I need to check for the presence of VLC before actually using it. I don’t want the user to have to find it if it is not present. The following works, but read on…

tell application "Finder" to set vlcname to name of application file id "org.videolan.vlc"
set vob to "/Volumes/SpareHead2/DVD Library/Transport Streams/PAL.ts"
tell application vlcname
	using terms from application "VLC"
		activate
		open POSIX file vob as alias
		fullscreen
	end using terms from
end tell

This works, but if this happens to be inside any other tell statement, it fails, and attempts to pass the activate, open and fullscreen events to the other application. This seems to contradict expected behavior. For example, this fails:

tell application "Finder" to set vlcname to name of application file id "org.videolan.vlc"
set vob to "/Volumes/SpareHead2/DVD Library/Transport Streams/PAL.ts"
tell application "System Events"
	--various statements that talk to System Events
	tell application vlcname
		using terms from application "VLC"
			activate
			open POSIX file vob as alias
			fullscreen
		end using terms from
	end tell
end tell

Are nested tell statements taboo? This fails with a:

“System Events got an error: application "VLC.app" doesn’t understand the fullscreen message.” (and the activate and open commands are passed to System Events.)

Also, I would have thought that a tell by path would be preferable:

tell application "Finder" to set vlcname to application file id "org.videolan.vlc"

But that doesn’t fly at all and results in a:

“Can’t get application (application file "VLC.app" of folder "Applications" of startup disk of application "Finder") of «script».”

Can anyone suggest what I am doing wrong, or a better way to do this? I am essentially using the example from the sourcebook…

http://applescriptsourcebook.com/viewtopic.php?pid=60341

Hi, Moonlight Mac.

They’re best avoided, if possible, when they concern different applications, since they can cause terminology confusion. The basic “ though not inflexible “ rule is that you don’t put anything inside a tell block that doesn’t concern the thing being told. When you use nested tells, the inner tell should ideally have some connection with the outer one “ say, it should tell some element of the thing being told by the outer tell, or it should tell the result of something produced by the outer one.

application file “VLC.app” of folder “Applications” of startup disk of application “Finder” isn’t a path but a Finder reference. You’d need to coerce it to Unicode text:

tell application "Finder" to set vlcname to application file id "org.videolan.vlc" as unicode text

However, that will error if the user doesn’t have the application installed, which is one of the things you’re trying to avoid. I’d formulate the script something like this:

tell application "Finder" to set vlcInstalled to (application file id "org.videolan.vlc" exists)

if (vlcInstalled) then
	
	-- Code involving VLC.
	
else
	
	-- Alternative code or apologetic message.
	
end if

If I remember correctly, any user can run a script containing code that addresses an application he or she doesn’t posses, provided that no attempt is made to execute that particular code. The system will ask where the application is if the script’s opened in a script editor, because it needs to quiz the application’s dictionary in order to decompile the script back into readable text.

using terms from won’t help if the application’s not actually there to carry out the tasks. Its main use is when the application may have different names on different machines. (Some applications’ names include their version numbers.) The scripter can compile code against his own version of the application, which can be addressed to a version with a different name on the running machine “ provided, of course, that the scripted features are implemented in both versions.

Hope that helps.

Thanks for clarifying things. I’ve ripped out the nested Tell statements, and all is well. I had even used the “as unicode text” in the same program to test for Toast. I don’t know how I missed that one!

I’m still using the “using terms from” because I have found that any Tell statement that references a program directly will trigger the “Please locate application X” dialog even if that Tell statment is not executed.

In another example I found on MacScripter, the programmer used hard coded events to get around using the “using terms from” construct. I think that for what I am doing, it is perfectly legit though.

try
	tell application "Finder" to set vlcpath to application file id "org.videolan.vlc" as unicode text
on error
	set vlcpath to ""
end try

set vob to "/Volumes/SpareHead2/DVD Library/Transport Streams/PAL.ts"
set play to false
tell application "Finder" to if exists vob as POSIX file then set play to true
if play and (vlcpath is not "") then
	tell application vlcpath
		using terms from application "VLC"
			activate
			open POSIX file vob as alias
			fullscreen
		end using terms from
	end tell
end if