AS works as a droplet but not as Folder Action

Hi there,

–I have the below script which works well when used via a file dropped on it.

on open DroppedFile
tell application “Finder”
set myMsg to “email address”
set NameOfFile to (name of (info for DroppedFile))
set myFile to DroppedFile as alias
–display dialog NameOfFile
end tell

display dialog NameOfFile & return & return & "Amend or a v1 Build?" buttons {"Amend", "v1 Build", "Cancel"} default button 2
set x to button returned of result
if x is "Amend" then
	
	tell application "Microsoft Outlook"
		set mySubject to (NameOfFile & "  AMEND FOR APPROVAL" as text)
		set myContent to "<p style=font-family:Calibri>...</p><p style=font-family:Calibri>Attached is a low res PDF for approval for the amendment to job  " & NameOfFile & "<p style=font-family:Calibri>Thanks</p>"
		set myAttachment to DroppedFile
		set myMsg to make new outgoing message with properties {subject:mySubject, content:myContent}
		tell myMsg
			make new attachment with properties {file:myFile}
			make new recipient at myMsg with properties {email address:{name:"Dave Leicester", address:"test@email.co.uk"}}
			
		end tell
		open myMsg
		tell application "Microsoft Outlook" to activate
		
		
	end tell
	
	
	
else if x = "v1 Build" then
	tell application "Microsoft Outlook"
		set mySubject to (NameOfFile & "  v1 BUILD FOR APPROVAL" as text)
		set myContent to "<p style=font-family:Calibri>...</p><p style=font-family:Calibri>Attached is a low res PDF for Version 1 Build approval on the job  " & NameOfFile & "<p style=font-family:Calibri>Thanks</p>"
		set myAttachment to DroppedFile
		set myMsg to make new outgoing message with properties {subject:mySubject, content:myContent}
		tell myMsg
			make new attachment with properties {file:myFile}
			make new recipient at myMsg with properties {email address:{name:"Dave Leicester", address:"test@email.co.uk"}}
			
		end tell
		open myMsg
		tell application "Microsoft Outlook" to activate
		
	end tell
	
else if x = "Cancel" then
	display dialog "You Picked Cancelled" default button 1
end if

end open

–However, I now want this to work via a Folder Action, i.e., when a file is dropped into a folder it is assigned to it invokes the script.
It’s not working at all. I’ve searched around and altered the script to the below, but this still does not work. Any ideas what I’m doing wrong?

on adding folder items to thisFolder after receiving DroppedFile
tell application “Finder”
set myMsg to “email address”
set NameOfFile to (name of (info for DroppedFile))
set myFile to DroppedFile as alias
–display dialog NameOfFile
end tell

display dialog NameOfFile & return & return & "Amend or a v1 Build?" buttons {"Amend", "v1 Build", "Cancel"} default button 2
set x to button returned of result
if x is "Amend" then
	
	tell application "Microsoft Outlook"
		set mySubject to (NameOfFile & "  AMEND FOR APPROVAL" as text)
		set myContent to "<p style=font-family:Calibri>...</p><p style=font-family:Calibri>Attached is a low res PDF for approval for the amendment to job  " & NameOfFile & "<p style=font-family:Calibri>Thanks</p>"
		set myAttachment to DroppedFile
		set myMsg to make new outgoing message with properties {subject:mySubject, content:myContent}
		tell myMsg
			make new attachment with properties {file:myFile}
			make new recipient at myMsg with properties {email address:{name:"Dave Leicester", address:"test@email.co.uk"}}
			
		end tell
		open myMsg
		tell application "Microsoft Outlook" to activate
		
		
	end tell
	
	
	
else if x = "v1 Build" then
	tell application "Microsoft Outlook"
		set mySubject to (NameOfFile & "  v1 BUILD FOR APPROVAL" as text)
		set myContent to "<p style=font-family:Calibri>...</p><p style=font-family:Calibri>Attached is a low res PDF for Version 1 Build approval on the job  " & NameOfFile & "<p style=font-family:Calibri>Thanks</p>"
		set myAttachment to DroppedFile
		set myMsg to make new outgoing message with properties {subject:mySubject, content:myContent}
		tell myMsg
			make new attachment with properties {file:myFile}
			make new recipient at myMsg with properties {email address:{name:"Dave Leicester", address:"test@email.co.uk"}}
			
		end tell
		open myMsg
		tell application "Microsoft Outlook" to activate
		
	end tell
	
else if x = "Cancel" then
	display dialog "You Picked Cancelled" default button 1
end if

end adding folder items to

–I’ve added the Folder to the Folder Actions Workflow and assigned the script to it - but when i place a file in there I get nothing.

Please help -it’s driving me mental!!

Regards

Dave

I don’t use/own M…soft products so, I can’t test.
But you may try this cleaned version:

on adding folder items to thisFolder after receiving DroppedFile -- Here, DroppedFile is a list
	set DroppedFile to item 1 of DroppedFile
	-- tell application "Finder"
		set myMsg to "email address"
		set NameOfFile to (name of (info for DroppedFile)) -- NameOfFile is a text object
		set myFile to DroppedFile -- as alias
		--display dialog NameOfFile
	-- end tell
	
	display dialog NameOfFile & return & return & "Amend or a v1 Build?" buttons {"Amend", "v1 Build", "Cancel"} default button 2 cancel button 3
	set x to button returned of result

	-- selecting Cancel never send you here.

	if x is "Amend" then
		set mySubject to NameOfFile & "  AMEND FOR APPROVAL"-- as NameOfFile is a text object no need for coercion
		set myContent to "<p style=font-family:Calibri>...</p><p style=font-family:Calibri>Attached is a low res PDF for approval for the amendment to job  " & NameOfFile & "<p style=font-family:Calibri>Thanks</p>"
	else
		-- here x ="v1 Build"
		set mySubject to NameOfFile & "  v1 BUILD FOR APPROVAL"-- as NameOfFile is a text object no need for coercion
		set myContent to "<p style=font-family:Calibri>...</p><p style=font-family:Calibri>Attached is a low res PDF for Version 1 Build approval on the job  " & NameOfFile & "<p style=font-family:Calibri>Thanks</p>"
	end if
	tell application "Microsoft Outlook"
		activate -- I guess that this instruction must be here
		set myAttachment to DroppedFile
		set myMsg to make new outgoing message with properties {subject:mySubject, content:myContent}
		tell myMsg
			make new attachment with properties {file:myFile}
			make new recipient at myMsg with properties {email address:{name:"Dave Leicester", address:"test@email.co.uk"}}
			
		end tell
		open myMsg
	end tell
end adding folder items to

Of course you may apply the same cleaning to the droplet version.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 5 mars 2020 17:27:51

Cleaned & optimised version of your on open & on adding adding folder items to handlers:


on open DroppedFiles
	set myFile to item 1 of DroppedFiles
	tell application "Finder" to set NameOfFile to name of myFile
	try
		set x to button returned of (display dialog NameOfFile & return & return ¬
			& "Amend or a v1 Build?" buttons {"Amend", "v1 Build", "Cancel"} default button 2)
	on error number -128
		display notification "You Picked Cancelled"
		return
	end try
	
	if x is "Amend" then
		set mySubject to NameOfFile & "  AMEND FOR APPROVAL"
		set myContent to "<p style=font-family:Calibri>...</p><p style=font-family:Calibri>Attached is a low res PDF for approval for the amendment to job  " & NameOfFile & "<p style=font-family:Calibri>Thanks</p>"
	else
		set mySubject to NameOfFile & "  v1 BUILD FOR APPROVAL"
		set myContent to "<p style=font-family:Calibri>...</p><p style=font-family:Calibri>Attached is a low res PDF for Version 1 Build approval on the job  " & NameOfFile & "<p style=font-family:Calibri>Thanks</p>"
	end if
	
	tell application "Microsoft Outlook"
		tell (make new outgoing message with properties {subject:mySubject, content:myContent})
			make new attachment with properties {file:myFile}
			make new recipient with properties {email address:{name:"Dave Leicester", address:"test@email.co.uk"}}
			open
		end tell
		activate
	end tell
end open

---------------------------------------------------------------------------------------------------------------------------
on adding folder items to thisFolder after receiving DroppedFiles
	set myFile to item 1 of DroppedFiles
	tell application "Finder" to set NameOfFile to name of myFile
	try
		set x to button returned of (display dialog NameOfFile & return & return ¬
			& "Amend or a v1 Build?" buttons {"Amend", "v1 Build", "Cancel"} default button 2)
	on error number -128
		display notification "You Picked Cancelled"
		return
	end try
	
	if x is "Amend" then
		set mySubject to NameOfFile & " AMEND FOR APPROVAL"
		set myContent to "<p style=font-family:Calibri>...</p><p style=font-family:Calibri>Attached is a low res PDF for approval for the amendment to job " & NameOfFile & "<p style=font-family:Calibri>Thanks</p>"
	else
		set mySubject to NameOfFile & " v1 BUILD FOR APPROVAL"
		set myContent to "<p style=font-family:Calibri>...</p><p style=font-family:Calibri>Attached is a low res PDF for Version 1 Build approval on the job " & NameOfFile & "<p style=font-family:Calibri>Thanks</p>"
	end if
	
	tell application "Microsoft Outlook"
		tell (make new outgoing message with properties {subject:mySubject, content:myContent})
			make new attachment with properties {file:myFile}
			make new recipient with properties {email address:{name:"Dave Leicester", address:"test@email.co.uk"}}
			open
		end tell
		activate
	end tell
end adding folder items to

Just a question:
is there a real need to put the block:

  
       if x is "Amend" then
           set mySubject to NameOfFile & " AMEND FOR APPROVAL"
           set myContent to "<p style=font-family:Calibri>...</p><p style=font-family:Calibri>Attached is a low res PDF for approval for the amendment to job " & NameOfFile & "<p style=font-family:Calibri>Thanks</p>"
       else
           set mySubject to NameOfFile & " v1 BUILD FOR APPROVAL"
           set myContent to "<p style=font-family:Calibri>...</p><p style=font-family:Calibri>Attached is a low res PDF for Version 1 Build approval on the job " & NameOfFile & "<p style=font-family:Calibri>Thanks</p>"
       end if

in the tell Outlook block ?
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 5 mars 2020 20:54:03

Yes, you are right, it was wrong. I updated the script.

In my first message I forgot to say that I saw no reason why the script failed in the folder action version except if several files where dropped in the hot folder.
In such case, the System which silently resolve the anomaly when a single file is dropped fail to grab the name of the passed [list of several files].
In fact, I was surprised by the fact that it doesn’t fail when it is asked to extract the name of a [list of a single file].
Knowing that the passed data is a list, when my code is supposed to treat a single item I always apply one of these schemes:
(1) issue an error if the passed list contain more than one item
(2) extract the first item of the list and apply to it.
The late scheme is not guaranteed to do the correct job because the extraneous file passed may be the first of the dropped ones.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 6 mars 2020 00:23:46

Thanks so much all for your input and script cleaning!
I will check these out once things quieten down (suddenly manic!) and report back with success (or any issues).
Thanks again