PDA

View Full Version : Help in Programming




mattsnapper
Apr 17, 2006, 02:44 AM
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..?



HexMonkey
Apr 17, 2006, 03:53 AM
You can do this using folder actions. Follow these steps (note that this assumes you use Mail, not a third party email program):


Open Address Book and setup a group for your recipients. Note the name of the group.
Open Script Editor (/Applications/AppleScript/Script Editor) and paste the following 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
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.
Save the script using the script file format, with all options off. Save it in /Library/Scripts/Folder Action Scripts/.
Create the "hot" folder on your Desktop.
Control click/right click on the hot folder and choose "Attach a Folder Action..."
Navigate to and select your recently saved script.
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.

mattsnapper
Apr 17, 2006, 05:49 AM
you are a very very nice man indeed !!

a thousand thanks !

Wrexhamfc
Apr 17, 2006, 06:50 AM
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:)

sabor
Apr 17, 2006, 01:04 PM
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:

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

HexMonkey
Apr 17, 2006, 04:20 PM
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:)

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.

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:

<snip>

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).

Wrexhamfc
Apr 18, 2006, 06:41 AM
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

HexMonkey
Apr 18, 2006, 07:12 AM
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 (http://bbs.applescript.net/viewtopic.php?pid=40831) 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.

demallien
Apr 19, 2006, 01:39 AM
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?

caveman_uk
Apr 19, 2006, 02:45 AM
I'm someone that uses C all day and is thrilled when an opportunity to work in assembler presents itself
You're weird :D

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

HexMonkey
Apr 19, 2006, 04:37 AM
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?

Here's a few resources:
AppleScript Language Guide (http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangGuide/index.html) - An introduction to AppleScript
AppleScript Reference Library (http://developer.apple.com/referencelibrary/AppleScript/) - Complete reference for AppleScript
MacScripter (http://www.macscripter.net/) - AppleScript website and forums

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

Hope this helps.

mattsnapper
Apr 28, 2006, 02:24 PM
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

yellow
Apr 28, 2006, 02:28 PM
Mayhaps, FolderSync (http://www.robbieduncan.net/cgi-bin/get_file.cgi?file=projects/mac/foldersync/index), written by our own Robbieduncan!

mattsnapper
Apr 28, 2006, 02:40 PM
Mayhaps, FolderSync (http://www.robbieduncan.net/cgi-bin/get_file.cgi?file=projects/mac/foldersync/index), written by our own Robbieduncan!


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

mrichmon
Apr 28, 2006, 03:14 PM
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..?

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.

yellow
Apr 28, 2006, 03:26 PM
I believe Transmit will allow you to create an automated drop box for FTPing items.. http://www.panic.com