Display dialog problem in folder action

Here is a script I wrote to label pdf’s, scpt’s, and workflows. A display dialog at the end of the script summarizes the actions taken.
It works intermittently no matter what combination of files is used which is crazy.

It definitely seems to have a problem when two pdf’s are added to the target directory. They get added and when the script “works” they get labeled with a blue label. The display dialog at the end however never shows up.

it is as if the variable pdfstring is “” even though pdfcount is >1

I cannot figure it out. Please help. I am not looking for a script overhaul (although I’m sure the talent out there is quite capable of such a thing), I want to understand what is wrong.

Here is my folder action:

on adding folder items to folder_ after receiving added_items

--set added_items to (choose file with multiple selections allowed)

set pdfcount to 0 as integer
set scptcount to 0 as integer
set workflowcount to 0 as integer
set pdfstring to ""
set scptstring to ""
set workflowstring to ""
set displaystring to ""
repeat with new_file in added_items
	set type_of_file to (do shell script "mdls -name kMDItemContentType " & quoted form of (POSIX path of new_file))
	if type_of_file contains "com.adobe.pdf" then
		tell application "Finder" to set label index of new_file to 4 --blue
		set pdfcount to pdfcount + 1
	else if type_of_file contains "com.apple.applescript.script" then
		tell application "Finder" to set label index of new_file to 2 --red
		set scptcount to scptcount + 1
	else if type_of_file contains "com.apple.automator-workflow" then
		tell application "Finder" to set label index of new_file to 2 --red
		set workflowcount to workflowcount + 1
	end if
end repeat
--display dialog "PDF " & pdfcount & "   SCPT " & scptcount & "   Workflow " & workflowcount used for debugging
if (pdfcount = 0) then
	set pdfstring to ""
else if (pdfcount = 1) then
	set pdfstring to "1 New PDF File Has Been Labeled Blue." & return
else
	set pdfstring to pdfcount & " New PDF Files Have Been Labeled Blue." & return
end if
if (scptcount = 0) then
	set scptstring to ""
else if (scptcount = 1) then
	set scptstring to "1 New SCPT File Has Been Labeled Red." & return
else
	set scptstring to scptcount & " New SCPT Files Have Been Labeled Red." & return
end if
if (workflowcount = 0) then
	set workflowstring to ""
else if (workflowcount = 1) then
	set workflowstring to "1 New WORKFLOW File Has Been Labeled Red."
else
	set workflowstring to workflowcount & " New WORKFLOW Files Have Been Labeled Red."
end if
if (pdfstring is "") and (scptstring is "") and (workflowstring is "") then
	error number -128
end if
set displaystring to (pdfstring & scptstring & workflowstring)
display dialog displaystring

end adding folder items to

Hi,

you’re mixing up integers with string data types which causes an error.
In case of an error folder actions abort silently.

For example, the result of


set pdfcount to 2
set pdfstring to pdfcount & " New PDF Files Have Been Labeled Blue." & return
display dialog pdfstring

is a list

{2, " New PDF Files Have Been Labeled Blue." , return}

and throws an error, because pdfcount is an integer and the concatenation takes the class of the leftmost element as the class of the result

Coercing the integer to text solves the problem


set pdfcount to 2 as text
set pdfstring to pdfcount & " New PDF Files Have Been Labeled Blue." & return
display dialog pdfstring