Creating a folder action to play a sound using applescript

Hello All,

I’ve been trying to write an applescript to make the mac play a sound whenever a download finishes. This is assuming that the download is placed in the “downloads” folder, and I assigned a folder actions script to that folder, which plays a selected sound when a new item is added. This script was modified from the default “add item alert” folder action script provided by Apple. Though this script does run, no sound actually plays when an item is added to this folder. I attached the script to the folder through the folder actions setup dialog.




on adding folder items to this_folder after receiving added_items
	try
		tell application "Finder"
			--get the name of the folder
			set the folder_name to the name of this_folder
		end tell
		
		-- find out how many new items have been placed in the folder
		set the item_count to the number of items in the added_items
		if the item_count is greater than 1 then
			do shell script "afplay '/System/Library/Sounds/Purr.aiff'"
		end if
	end try
end adding folder items to

I am also totally blind, and use VoiceOver on the mac to navigate and use the computer. I’m not sure if I entered the applescript correctly with the brackets, but hopefully it worked :slight_smile: if I did do this incorrectly, please let me know so I can correct it in the future. :slight_smile:
Thank you all os much,
Rocco

Hi rfscripter,

There’s a better way to play sounds by adding an ASObjC program to your library. I’m a little bit rusty on this but it used to work quite well.

gl,
kel

It’s not the shell command that doesn’t work. So why replace such a simple line of code with an more complex script library?

@rfscripter: The code is good but the script only plays a sound when 2 or more items are added to the folder.

if the item_count is greater than 1 then

should be:

if the item_count is greater than 0 then

Awesome, thank you so much!! Works like a charm now. :slight_smile:
Rocco