Help in Programming

Discussion in 'Mac Programming' started by mattsnapper, Apr 17, 2006.

  1. macrumors newbie

    Joined:
    Apr 17, 2006
    Location:
    Shrewsbury, UK
    #1
    I wish to set up an automated system where by if a 'hot' folder upon my desktop receives a newly created jpeg file, an email is then generated with an attatchment of the jpeg and sent out to a designated group of emails

    Is this possible..?
     
  2. Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #2
    You can do this using folder actions. Follow these steps (note that this assumes you use Mail, not a third party email program):

    1. Open Address Book and setup a group for your recipients. Note the name of the group.
    2. Open Script Editor (/Applications/AppleScript/Script Editor) and paste the following code:
      Code:
      property groupName : "ImageGroup" --The name of the group of recipients, as named in Address Book
      property subjectPrefix : "New image:" --Followed by the image name then used as the subject of the message
      property messageContent : "" --The content of the message
      property imageFormats : {"jpg", "jpeg"} --Valid formats for files added to the hot folder
      
      on adding folder items to hotFolder after receiving newItems
      	--Cycle through each file
      	repeat with aFile in newItems
      		
      		--Get information about the file
      		tell application "Finder"
      			set fileExtension to name extension of aFile
      			set filename to name of aFile
      		end tell
      		
      		--Check that the file extension is valid
      		if fileExtension is in imageFormats then
      			tell application "Mail"
      				--Create the new message
      				set theSubject to subjectPrefix & " " & filename
      				set newMessage to make new outgoing message with properties {subject:theSubject, content:messageContent & return}
      				
      				--Set the recipients and attach the file
      				make new to recipient at end of to recipients of newMessage with properties {address:groupName}
      				tell content of newMessage to make new attachment with properties {file name:aFile} at after the last paragraph
      				
      				--Send the message
      				send newMessage
      			end tell
      		end if
      	end repeat
      end adding folder items to
    3. Change the properties at the top of the script to fit your needs, especially the one called groupName. It should be the name of your new group in Address Book.
    4. Save the script using the script file format, with all options off. Save it in /Library/Scripts/Folder Action Scripts/.
    5. Create the "hot" folder on your Desktop.
    6. Control click/right click on the hot folder and choose "Attach a Folder Action..."
    7. Navigate to and select your recently saved script.
    8. It should now work as expected.

    You'll probably want to do some tests before making full use of this. I'd recommend creating a group in Address Book that just contains yourself, which gives you something safe to experiment with.

    Let me know if this helps.
     
  3. thread starter macrumors newbie

    Joined:
    Apr 17, 2006
    Location:
    Shrewsbury, UK
    #3
    you are a very very nice man indeed !!

    a thousand thanks !
     
  4. macrumors newbie

    Joined:
    Apr 17, 2006
    Location:
    Manchester
    #4
    Can the script be modified

    If I was to ftp into a hot folder from a different location, is there anyway that the script be modified to recognise that an incoming jpeg file has completed and would be ready to send.
    Many thanks:)
     
  5. macrumors regular

    sabor

    Joined:
    Mar 17, 2006
    #5
    God I hate Applescript... :mad:

    So I am creating a script based on yours. I tested it by setting the name of the file to one I knew existed so I could test within applescript. It did send the file as an attachment.

    However when adding the "on adding folder" saving it, attaching the action to a folder, and dragging items to the folder, it looks like its running but just returns me to Script Editor!! :mad: Any idea what this "enlightening" behavior might be? I get a 'true' on my result window, which I assume things went well. Here is my code:

    Code:
    property groupName : "foo@bar.org" --The name of the group of recipients, as named in Address Book
    property subjectPrefix : "IMAGES:" --Followed by the image name then used as the subject of the message
    property messageContent : "" --The content of the message
    
    on adding folder items to hotFolder after receiving newItems
    	--Cycle through each file
    	repeat with aFile in newItems
    		display dialog aFile
    		--set aFile to "OS X:temp:servicios.html"
    		
    		tell application "Mail"
    			tell application "Finder"
    				set the target_file to aFile as alias
    				set bodyvar to the name of target_file
    			end tell
    			set composeMessage to (make new outgoing message)
    			tell composeMessage
    				make new recipient at beginning of to recipients ¬
    					with properties {address:"foo@bar.org"}
    				set the subject to aFile
    				set the content to bodyvar
    				tell content
    					make new attachment with properties {file name:target_file} ¬
    						at after the last word of the last paragraph
    				end tell
    			end tell
    			
    			send composeMessage
    			display dialog "Success"
    		end tell
    	end repeat
    end adding folder items to
     
  6. Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #6
    I'm not quite sure what you're wanting here. Are these files being uploaded to the hot folder? If so, you should be able to use the script as is by setting it up as a folder action on the server, assuming it's running Mac OS X (folder actions and AppleScript are Mac only). If you want further help, please elaborate on what you're wanting.

    When you run it from Script Editor, you set aFile to a string, ie "OS X:temp:servicios.html". However, when it's run as a folder action, aFile isn't a string, it's a file. display dialog can't display files, so your script exits on the line display dialog aFile due to an error. To fix this, change it to display dialog (aFile as string).
     
  7. macrumors newbie

    Joined:
    Apr 17, 2006
    Location:
    Manchester
    #7
    Ftp

    Where as Matt is drag and dropping a file straight into the folder from his desktop, I am sending a jpeg file in to my home computer using ftp over a mobile wireless network, when the file hits the folder the script sends the file as soon as it is detected, just as it is supposed to do, the only problem is that the file is incomplete it has not fully loaded into the folder.
    My question should of been is it possible for the script to detect when a jpeg file is complete before sending.

    Many thanks for getting back in touch
     
  8. Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #8
    Ah, I understand now. This is a common problem with the on adding folder items folder actions - the script is run when beginning to add the items rather than when completed - and unfortunately there's no easy solution. A MacScripter thread discusses a few possible workarounds which you could use (with some example code), but none of them are perfect and might not be 100% accurate.
     
  9. macrumors regular

    Joined:
    Oct 13, 2005
    #9
    Getting started with Applescript

    I have so got to get my head around scripting. That was one cool script. I have the impression that I could make my Mac do some really cool things really easily. Sadly, the only time I've ever done Applescripting was to add a couple of simple commands to the voice recognition ( like "Front Row" - impresses guests no end when I yell that one out across the room :) )

    As a low level code cutter (I'm someone that uses C all day and is thrilled when an opportunity to work in assembler presents itself), is there a good start off reference for applescripting?
     
  10. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #10
    You're weird :D

    Only kidding, I used to like writing in assembler too.
     
  11. Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #11
    Here's a few resources:

    There are also a lot of sample scripts at /Library/Scripts/.

    Hope this helps.
     
  12. thread starter macrumors newbie

    Joined:
    Apr 17, 2006
    Location:
    Shrewsbury, UK
    #12
    the mail sending out script you wrote works fine !

    Is there a way so I have another 'hot' folder to FTP out images automatically to one or more FTP addresses behind the scenes so that I can spend my time and concentrate cropping and captioning images in photoshop and not worry about dragging and dropping JPEGS into multiple boxes on my desktop
     
  13. Moderator emeritus

    yellow

    Joined:
    Oct 21, 2003
    Location:
    Portland, OR
  14. thread starter macrumors newbie

    Joined:
    Apr 17, 2006
    Location:
    Shrewsbury, UK
    #14

    thanks yellow, I tried that but using the sync application I could not connect because of errors in the datastream ? Using other FTP applications worked first time, but I want things to be automatic
     
  15. macrumors 6502a

    Joined:
    Jun 17, 2003
    #15
    Given the problems with using Applescript due to the file existing but not being complete on the server I'd probably end up using a unix script that is fired off as a cron job every 5 minutes or so.

    The script would record the time that the script is being run, then check the modification times of the files in the "hot" folder. For each file in the "hot" folder, if the modification time is more than 5 minutes old (assuming that FTP connections to this machine are stable and reasonably fast) then create the email and send it out using unix "mail" and move the file to an archive folder.

    Use cron to run the script, or have the script add itself to the "at" job list each time it finishes.

    This assumes that you can accept a 5 minute delay between the file being "finished" and the email being sent out.
     
  16. Moderator emeritus

    yellow

    Joined:
    Oct 21, 2003
    Location:
    Portland, OR
    #16
    I believe Transmit will allow you to create an automated drop box for FTPing items.. http://www.panic.com
     

Share This Page