URGENT need help

Hello guys,

I’m new in apple script and I was trying to create a script that sends me a mail whenever a file is dropped in a folder. This mail should include some info about the file and attach the file itself to the mail. I have found a script provided by apple that does approximately the same, the name is ADD - NEW ITEM ALERT I edited to fit my needs. but I have had some problems with the path of the attachment… the attachment isn’t being attached to the mail.

(*
ADD - NEW ITEM ALERT
©2002 Apple Computer

This Folder Action script is designed for use with Mac OS X version 10.2 and higher.

This Folder Action handler is triggered whenever items are added to the attached folder.  The script will display an alert 
containing the number of items added and offering the user the option to reveal the added items in Finder.
*)

property dialog_timeout : 20 -- set the amount of time before dialogs auto-answer.

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
		--create the alert string
		set alert_message to ("Folder Actions Alert:" & return & return) as Unicode text
		if the item_count is greater than 1 then
			set alert_message to alert_message & (the item_count as text) & " new items named:" & return & «data utxt201C» & the added_items & «data utxt201D» & return & "have "
		else
			set alert_message to alert_message & "One new item named: " & return & «data utxt201C» & the added_items & «data utxt201D» & return & "has "
		end if
		set alert_message to alert_message & "been placed in folder: " & return & «data utxt201C» & the folder_name & «data utxt201D» & "."
		
		
		
		--display dialog the alert_message giving up after dialog_timeout
		---------------------------------------------------------		
		--This part is when the added_items will attach 
		--and send to the designated address.
		---------------------------------------------------------
--mark
		-- Display a list of all the user's defined signatures. Skip if no signatures are defined.
		tell application "Mail" to set everySignature to name of every signature
		set theSignature to ""
		if (count of everySignature) is greater than 0 then
			set everySignature to {"None"} & everySignature
			set theResult to choose from list everySignature with prompt ¬
				"Select a signature to use with this message:" default items {"None"} without multiple selections allowed
			if theResult is not equal to false then
				tell application "Mail" to set theSignature to signature (item 1 of theResult)
			end if
		end if
		
		-- Go through each account and constuct a list of possible addresses
		-- to use as a return address for this message.
		tell application "Mail"
			set listOfSenders to {}
			set everyAccount to every account
			repeat with eachAccount in everyAccount
				set everyEmailAddress to email addresses of eachAccount
				if (everyEmailAddress is not equal to missing value) then
					repeat with eachEmailAddress in everyEmailAddress
						set listOfSenders to listOfSenders & {(full name of eachAccount & " <" & eachEmailAddress & ">") as string}
					end repeat
				end if
			end repeat
		end tell
		
		-- Prompt the user to select which account to send this message from.
		
		tell application "Mail"
			-- Properties can be specified in a record when creating the message or
			-- afterwards by setting individual property values.
			set newMessage to make new outgoing message with properties {subject:"test Delivery Action", content:alert_message & return & «data utxt201C»}
			tell newMessage
				-- Default is false. Determines whether the compose window will
				-- show on the screen or whether it will happen in the background.
				set visible to false
				set sender to "psycho@mac.com"
				make new to recipient at end of to recipients with properties {name:"psycho-ed", address:"psycho-ed@mac.com"}
				make new attachment at after the last paragraph with properties {file name:the added_items}
				
				
				
				if (theSignature is not equal to "") then
					set message signature to theSignature
				end if
			end tell
			-- Bring the new compose window to the foreground, in all its glory
			activate
			send newMessage
		end tell
		
	end try
end adding folder items to

That is the script that is not working!! Even tough if you write in the code:


--all the code until the comment mark then add this
set theAttahment to "" & (added_items)
display dialog the theAttachment
--then continue the code

the correct path name will pop!!
if u put in the code:


--all the begening code till you reach the comment mark
set theAttachment to choose file
--all the code other code till you reach the attachment part again
make new attachment at after the last paragraph with properties {file name:theAttachment}

it will work but you will have to choose the attachment manually… i don’t want that … i think this can be solved easely by someone who knows what he’s doing [unlike me…] please i need help urgently…

thanks in advance,
Psycho-ed

psyco-ed,

This is just a guess but maybe try,

set theAttachment to  "" & (added_items) as alias

Also you may have to put this in a Finder tell block. If added_items is more than one item you may have to loop to get a single file.

Hope it helps.

PreTech

thanks a lot PreTech,

your guess turned out to be working… =)…
but i need one more thing, i need to get the size of the folder/file attached how can this be done?
I tryed to use the “data size” function but it was always returning a zero… any sugestions?

Psycho-ed

Psyco-ed,

This is a script that I modify for the purposes as I need it to find information about files and folders.

tell application "Finder"
	set theFile to file "blah" of folder "blah" of desktop
	get properties of theFile
end tell

Anyway, for getting the size of a file or a folder:

get physical size of aFile -- or aFolder

This returns a value as such: 3.272704E+6 which is in bytes.

Hope this helps.

PreTech

Hi

well i have been working on the same subject, i needed to find the size of a certain folder.
The thing is that on adding file, i am sending an email with this attached file or folder but if its size was bigger than 5 MB it wont b attached.
So i want to reject files or folders greater than 5 MB. To do so i have to know the size.
What i am doing is always returning me a zero value.

tell application "Finder"
				activate
				data size of added_items
				display dialog the (data size)
			end tell
			

Please can you write me a more detailed script because the one of the physical size didnt work. I dont know if there is something missing.

Thnx a lot!!

tony

Model: Mac OS X Server
Browser: Safari 125.9
Operating System: Mac OS X (10.3.5)

Well guys,

I don’t know quite what to say. I played around with this test script a little.

on adding folder items to thisFolder after receiving addedItems
	tell application "Finder"
		repeat with anItem in addedItems
			if class of anItem is folder then
				set theSize to physical size of every item in anItem
			else
				set theSize to the physical size of anItem
				
			end if
                     display dialog theSize as string
		end repeat
	end tell
end adding folder items to

It will return “missing value” if I try to copy a folder to the location and then if I drag the copy out and then back in it returns the physical size. If I just drag the original folder in it works fine. If I copy a file it has no problems. I don’t know if this is a referencing problem or maybe a bug?

Maybe we can get Nigel, JJ or Jobu or one of the other more learned gurus to help with this one. I’ll keep trying though.

PreTech

An edit to this post: I have found that my line: if class of anItem is folder then is at fault somehow. The class of the item returned is alias so it does not seem to reference the actual folder itself. Still working on it though.

Model: dual 1.8 G5
AppleScript: 2.1 (80)
Browser: Safari 412.5
Operating System: Mac OS X (10.4)

Ok, so this is what I have now.

on adding folder items to thisFolder after receiving addedItems
	set newList to {}
	tell application "Finder"
		set theList to every item of thisFolder
		repeat with anItem in theList
			set theName to name of anItem
			display dialog "the name is " & theName
			if class of anItem is folder then
				display dialog "folder"
				set theSize to size of anItem
				
			else
				display dialog "file"
				set theSize to the physical size of anItem
			end if
			display dialog "The size is " & theSize as string
		end repeat
	end tell
end adding folder items to

However, you’ll need to get rid of any files already in the folder that this would be attached to unless you don’t mind it going through items already in there. I’m having problems with the addedItems and those being alias references. I can’t figure out how to reference the actual folder or file. Probably something simple. Hope this helps.

P.S. The dialogs are there for troubleshooting purposes.

PreTech

Model: dual 1.8 G5
AppleScript: 2.1 (80)
Browser: Safari 412.5
Operating System: Mac OS X (10.4)

Just jumping in here but this might help. Although I not sure about the details of the particular problem:

on adding folder items to thisFolder after receiving addedItems
tell application “Finder”
repeat with anItem in addedItems
set k to kind of anItem
if k = “Folder” then
set theSize to the size of anItem
display dialog theSize as string
else
set theSize to the size of anItem
display dialog theSize as string
end if
end repeat
end tell
end adding folder items to

It worked fine and explains the missing value error caused by the position of the display dialog in the loop.

Thank you guys for the help. I had things working because of you.
But i am facing a new porblem now. it might be related to applescript bug or not.
Anyways this whats happenin, in my work i need to control every delivered file or folder to a certain location so this why on adding files or folders to a certain places i ll receive an email. the problem is that a client delivered a file which was already there, he will be asked to replace and if he does my script wont work although he sent a new file with different information.
Help me on this one guys!!
thx!!

t o n y.

tonykhater,

You could make a new folder so when an item is placed, and the email sent, the new item is then moved to the new folder that way the folder being checked for new items is always empty. Maybe?

PreTech

PreTech

This cannot work. I forgot to tell you that those client have write only access to the delivered folder and there are other clients that have read only access and i am in a different bldg. so i need to know whats going on.
I cannot delete the files directly because as i said there are people who needs them.

Sorry for the disturbance…!! if you can do something about it…if not its okay i ll see what i ll do…!!
thx again!

t o n y

How about a stay open app that checks the modification date periodically? If the modification date has changed then a new email is sent.

PreTech

Okay, i think this could be one of the solutions, i thought of an application that chechs the name and if a name already exists it would delete the new item…but i think ur idea is better…the thing is that i dont know how to handle the date scripts yet…Can you send me any tips related to this subject and my previous code…!!
thx!

t o n y

Riverman here
Don’t see the problem with PreTech’s suggestions. It’s not you or the users that do the moving/deleting of the files but the mail script. Or am I missing something

How about this:

property fileList : {}

on idle
	tell application "Finder"
		set theFolder to folder "theDest" of desktop -- your folder location
		set theTime to the time of (current date)
		set testDate to (current date) - theTime -- the current day at 12:00 AM
		--display dialog theDate as string
		set fileList to every item of theFolder
		if time of (current date) is greater than or equal to 21600 then -- 6:00 AM current day
			repeat with anItem in fileList
				if modification date of anItem is greater than testDate then
					display dialog "Send message" -- this is where your email code would go
				end if
			end repeat
		end if
	end tell
end idle

The property stays the same until you change it in the script or recompile the script.

PreTech