Folder Action Script Problems

I am an applescript newbie. What little I know is pieced together from example scripts, and I’m in a real pickle.

My ultimate goal is a folder action script that will change the icon of the folder it is attached to depending on whether or not it has anything in it. I gave up on the icon-switching part of this, creating Quicksilver command files that do the job, but I still can not make a basic script react.

My skeleton script is as follows:


on adding folder items to this_folder after receiving these_items
	try
		set theCount to the number of items in this_folder
		if theCount is greater than 0 then
			set alert_messege to "Wam!" as Unicode text
		else
			set alert_messege to "Nada" as Unicode text
		end if
		display dialog alert_messege
	end try
end adding folder items to

I have no idea why this doesn’t work. The “set alert_message” lines will be replaced with a command to launch the icon changing files, but it doesn’t want to execute these commands. Thanks for your help.

Model: iBook G4
AppleScript: 1.10.7
Browser: Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en; rv:1.8.1.4) Gecko/20070509 Camino/1.5
Operating System: Mac OS X (10.4)

Few thing, you use count not number. The folder this_folder is an alias, You have to tell finder to count, and change alias into a reference.

Since add and remove effect the total. Add both and past to a shared handler, like this.

on adding folder items to this_folder after receiving these_items
	FolderCount(this_folder, these_items)
end adding folder items to

on removing folder items from this_folder after losing these_items
	FolderCount(this_folder, these_items)
end removing folder items from

on FolderCount(this_folder, these_items)
	tell application "Finder"
		tell this_folder as reference
			set theCount to count of items
		end tell
	end tell
	if theCount is greater than 0 then
		set alert_messege to "Wam!" as Unicode text
	else
		set alert_messege to "Nada" as Unicode text
	end if
	tell application "Finder"
		display dialog alert_messege & " - " & theCount as string
	end tell
end FolderCount

Have fun

Hi caseykoons, couple of ideas to try, I see bevos beat me to it :slight_smile:

on adding folder items to this_folder after receiving these_items
set theCount to the count of items in the these_items – added “the” changed “this_folder” to “these_items”
if theCount is greater than 0 then
set alert_messege to “Wam!” as Unicode text
else
set alert_messege to “Nada” as Unicode text
end if
display dialog alert_messege
end adding folder items to

or

on adding folder items to this_folder after receiving added_items
set the item_count to the count of items in the added_items
if the item_count is greater than 0 then
display dialog “Wam!”
else
display dialog “Nada”
end if
end adding folder items to

Budgie

[of topic] did you read youre messages bevos?

Yes, I read my post. Is there something wrong?

You are right if you add anything of course there items. But if you remove items doesn’t mean there none. So best to count you think.

Changing the icon is harder, should be as easy as change the icon property, but its not.

Bevan

To try and make sense of the above replies:

‘this_folder’ is an alias to the folder to which the action’s attached. ‘these_items’ is a list of aliases to the items that have just been added to it, triggering the action.

By itself, the script can get the number of items in a list, but not the number of items in a folder. You either have to get a list of the items (using the Finder or some other means like ‘list folder’) and count the list.

tell application "Finder" to set theCount to the number of (get items in this_folder)

-- Or:
set theCount to the number of (list folder this_folder without invisibles)

. or you can get the Finder to count the items directly without returning them to the script. In this case, though, you need to provide a valid Finder reference to the items so that the Finder’s own counting command can understand what has to be counted:

tell application "Finder" to set theCount to the number of items in folder this_folder
-- 'folder this_folder' turns the alias 'this_folder' into a valid part of the Finder reference 'items in folder this_folder'

Since the default element in a folder is an ‘item’ anyway, the above line could be reduced and made more efficient like this:

tell application "Finder" to set theCount to the number of folder this_folder

As Bevan’s pointed out, you could use ‘count’ instead of ‘number of’. I generally prefer ‘count’ because its faster. Both can be used either as language commands (which understand AppleScript objects) or as application commands (which are implemented by applications themselves to understand their own objects).

tell application "Finder" to set theCount to (count folder this_folder)

As has also been pointed out, if the folder action’s been triggered by adding items to the folder, there are bound to be more than zero items in the folder! :slight_smile:

Folder action scripts should be in a folder called “Folder Action Scripts” in your user “Scripts” folder.

path to Folder Action scripts -- Creates the folder if it doesn't already exist.
tell application "Finder" to open result

You got it right. When people are starting out. I get them to break it down. Not try to put everything in one line. So your line referencing the folder of alias this_folder would go like this:

tell application "Finder"
		tell folder this_folder
			set theCount to count
		end tell
	end tell

No slower breaking it out therefore not faster on one line.

Folder Action are done with system events, even throw it in the finder so you still have to tell finder where needed.