Cannot find application from AppleScript

I have a script that plays a random sound from a known folder. It uses two external applications to actually play the sound depending on its sound type (mp3 or sfil). When I save it as an application, when it runs, it asks “Where is the external application?”. I tried setting the folder of the application in the script but apparently that is not working. I am an Applescript noob, so any help would be appreciated. The script used to work just fine, but after an upgrade somewhere along the line it stopped. Any ideas???

P.S. If you see anything that is done not very efficiently, an improvement suggestion would be helpful as well.

tell application "Finder"
	if folder "Startup Sounds" of folder "DocuMents" of folder "mkaz" of folder "Users" of startup disk exists then
		set ssf to folder "Startup Sounds" of folder "DocuMents" of folder "mkaz" of folder "Users" of startup disk
		set n2 to count of folders of ssf
		set x2 to 0
		repeat while x2 < 1
			set x2 to random number n2
		end repeat
		set n to count of files of folder x2 of ssf
		set x to 0
		repeat while x < 1
			set x to random number n
		end repeat
		set sfile to file x of folder x2 of ssf as text
		if ".mp3" is in sfile then
			-- "use QuickStartupSound"
			set qfold to folder "QuickStartupSound" of folder "Applications" of startup disk
			try
				delete file "defaultsound" of qfold
			end try
			make alias at qfold with properties {name:"defaultsound"} to sfile
			launch application "Quick Startup Sound" of qfold
		else
			-- use "ClassicStartupSound"
			set qfold to folder "ClassicStartupSound" of folder "Applications" of startup disk
			try
				delete file "defaultsound" of qfold
			end try
			make alias at qfold with properties {name:"defaultsound"} to sfile
			launch application "Classic Startup Sound" of qfold
		end if
	end if
end tell

The 'where is application…?" prompt is displayed whenever your script references an application that AppleScript cannot find. Are you sure the applications are on the disk and are named exactly as your script calls them?

In general you shouldn’t need to tell AppleScript where to find an application. In most cases, even if AppleScript can’t find the application in question you can select the app in the 'where is app…?" dialog and the script will remember it.

Try removing the ‘of qfold’ part where you try to launch the app and next time AppleScript prompts you make sure you select the right app and continue.

As for the script itself, there are several optimizations you can make, and a couple of gotchas in your script that could cause problems.

For a start, the code where you select a folder (or a file in that folder):

      set n2 to count of folders of ssf 
      set x2 to 0 
      repeat while x2 < 1 
         set x2 to random number n2 
      end repeat 

This will fail if there are no folders in ssf because ‘count folders of ssf’ will return 0 and you can’t get ‘random number 0’ that will be greater than 1 (a requirement to break out of the repeat loop). The same error occurs when you select a file. Sure, you may have no empty folders there now, but it’s a dependency that you should avoid.

A much, much cleaner way of doing this is to let the OS do the work for you:

      set ssf to folder "Startup Sounds" of folder "DocuMents" of folder "mkaz" of folder "Users" of startup disk 
tell application "Finder"
   try
      set x2 to some folder of ssf
      set x to some file of x2
   on error
      return
   end try
end tell

The ‘some file/folder’ statement takes care of your random number generation, and the ‘try/on error/end try’ statements catch any empty folders.

You could also optimize your script a little by using handlers, but it’s not a lot of saving, so probably isn’t worth it in this case.

You shouldn’t be telling the Finder to launch the applications but to open the application files. The full file names are needed for this, which may have “.app” extensions. I don’t know what your file names actually are, but instead of the ‘launch application’ lines, you’ll need something like this:

open application file "QuickStarupSound.app" of qfold

It doesn’t look bad actually. :slight_smile:

If ‘folder “mkaz” of folder “Users” of startup disk’ is your own home folder, you can refer to it in the Finder simply as ‘home’:

set ssf to folder "Startup Sounds" of folder "DocuMents" of home

A range can be specified for the ‘random number’ command, which would save having to use the repeat loops:

set x2 to random number from 1 to n

… though Camelot’s suggestion is even better. :slight_smile:

Finally, there’s no need to coerce to text when setting ‘sfile’. In fact, properly, you shouldn’t do that unless you refer to ‘file sfile’ in the ‘make alias’ lines — though it’s often possible to get away with it. If you just set sfile to ‘file x of folder x2’, what you’ve already got in those lines will be “correct”.

I applied the tips from both of the previous posts and all is well. It works!!
I did have to coerce the sfile variable, but I do not know why…

Thanks!!

tell application "Finder"
	if folder "Startup Sounds" of folder "DocuMents" of home exists then
		set ssf to folder "Startup Sounds" of folder "DocuMents" of home
		set n2 to count of folders of ssf
		set x2 to random number from 1 to n2
		set n to count of files of folder x2 of ssf
		set x to random number from 1 to n
		set sfile to file x of folder x2 of ssf as text
		if ".mp3" is in sfile then
			-- "use QuickStartupSound"
			set qfold to folder "QuickStartupSound" of folder "Applications" of startup disk
			try
				delete file "defaultsound" of qfold
			end try
			make alias at qfold with properties {name:"defaultsound"} to sfile
			launch application "Quick Startup Sound" of qfold
		else
			-- use "ClassicStartupSound"
			set qfold to folder "ClassicStartupSound" of folder "Applications" of startup disk
			try
				delete file "defaultsound" of qfold
			end try
			make alias at qfold with properties {name:"defaultsound"} to sfile
			launch application "Classic Startup Sound" of qfold
		end if
	end if
end tell

Doh! Because in the very next line, you test to see if the string contains “.mp3”! Sorry. I missed that last night. (It was 3:12 in the morning where I am.)

Hi,

Have you ever tried Play Sound:

http://microcosmsoftware.com/playsound/

It plays sfil now.

gl,

Thanks for the tip. It didn’t used to play sfil sounds.

Cheers.