Hot Folder to Upload Files using Applescript (26 posts)

Josh Smith Member

Hi
I am trying to create an Applescript Folder Action which will upload a jpg file to the remote server using Fetch's mirror command.
I have managed to hack the Add Item Alert Folder Action script that came with Tiger OS and tried to add in the
Fetch command to mirror the folder, after the new item has been added.

The Finder alter works but Fetch does not activate and upload the jpg file.

Any advice would be greatly appreciated.

Many thanks

Josh

This Folder Action handler is triggered whenever items are added to the attached folder. The script will display an alert

--------SCRIPT--------------------
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 have "
else
set alert_message to alert_message & "One new item has "
end if
set alert_message to alert_message & "been placed in folder " & «data utxt201C» & the folder_name & «data utxt201D» & "."
set the alert_message to (the alert_message & return & return & "Would you like to view the added items?")

display dialog the alert_message buttons {"Yes", "No"} default button 2 with icon 1 giving up after dialog_timeout
set the user_choice to the button returned of the result

if user_choice is "Yes" then
tell application "Fetch"
activate
mirror alias "Macintosh HD:Users:joshsmith:Desktop:LocalUpload:" to url "ftp://redsnapperphoto:@ftp2.donhost.co.uk/www/Uploads/TestMirrorUpload/" format Automatic without delete strays
end tell
end if
end try
end adding folder items to

Scott McGuire Administrator

Hi,

First, one important thing to know is that the try/end try block prevents the AppleScript from displaying an error if one occurs; the script will just fail silently. That may be what is happening, because your script does have an error in it.

Scott McGuire Administrator

Hi,

1) Great, good to hear the script is now working.

2) The way to prevent Fetch from jumping to the foreground is to remove the AppleScript lines that include the "activate" command. The "activate" AppleScript command means "bring the application to the foreground" - it is not required to send AppleScript commands to Fetch. Fetch will still upload just fine if you remove that line, it just won't come to the foreground - which is what you want.

3) The function of the mirror command is to only upload new and changed files in a folder, and not upload the other files in the folder if they are identical to the copies of the files on the server. In order to determine what is new and changed, Fetch has to check the modification dates of all the files and folders in the server. (For more information about the Mirror command, see the Mirror Window introduction help topic.)

If your goal is just to upload only the file you just added to the folder, instead of mirroring the whole folder, then probably you want to use a different command to upload the file instead of mirror. Let us know if you'd like information about that.

PeterB Member

Hi, I can't write apple script so I need help please. This is what I need to happen:

A folder on desktop called 'Fetch_Upload' needs to monitor once a new file has been added. then it needs to upload the file to an ftp site (let's call the site ftp://sitename.co.uk/uploads/). Once the file has successfully uploaded it needs to move the file to a folder called 'Upload_Complete'. (it must upload the files in the order they were added to the folder). I hope this makes sense.

I have no chance of writing the script but I will be able to edit the folder and directory names.

I know you talented people write this kind of stuff in your sleep, but my lack of ability is causing me a lack of sleep lol

Jim Matthews Administrator

That Fetch Help describes an Automator folder action that automatically uploads files when they are placed into a local folder (like your Fetch_Upload folder). It does not discuss then moving the files to a different local folder, but if you can get the first part working I can then help with that step.

PeterB Member

Hi Jim
Brilliant.
I managed to create the folder action and files dropped into it are uploaded to the ftp.
I can't tell if they are uploaded by file name or by creation time added to the folder (there is only a split second between each file been added)

Jim Matthews Administrator

Hi PeterB,

To have your Folder Action Automator workflow also move the new files to a different folder, after uploading them with Fetch, we have to add 3 more actions to the workflow. When you open the current workflow in Automator you should see one action:

Upload Files

When you're done, there should be four actions:

Set Value of Variable
Upload Files
Get Value of Variable
Move Finder Items

Here are the steps:

1) Find the Set Value of Variable action and drag it into the workflow, before Upload Files. Click on the "Variable:" pop-up menu to set the variable name (the default name, "Storage", is fine)
2) Find the Get Value of Variable action and drag it into the workflow, after Upload Files. The "Variable:" pop-up menu should be set to the variable you created in step 1 (e.g. "Storage")
3) Find the Move Finder Items action and drag it into the workflow, after Get Value of Variable. Click on the "To:" pop-up menu and choose "Other...", then choose the "Upload_Complete" folder where you want the files moved.

PeterB Member

Thanks Jim
It's nearly working!
I use Filemaker Pro to save down files from a found set directly into the upload hot folder. Each record exports a text file and a PDF file.

Filemaker Pro exports the files into the hot folder but the hot folder is only uploading via Fetch a selection of them (4 or 5 out of 10.) All files that are successfully uploaded are also copied into the sent folder. All unsuccessful files remain in the hot folder.

If I drag and drop files into the hot folder, it successfully uploads all files and moves them to the sent folder. it is just when exporting direct from Filemaker into the hot folder when it fails.

I have tried using the same Mac to export from Filemaker into the hot folder and send. And export from Filemaker into a hot folder on a separate Mac on the Network both with the same results.

Jim Matthews Administrator

Hi PeterB,

It sounds like the Finder is not triggering the Finder Action workflow for every Filemaker Pro export. I'm not sure why that would be. What version of Mac OS X are you running?

You might have Filemaker trigger the upload process directly, rather than using the Folder Action mechanism. What version of Filemaker are you running? Do you know if it supports running Automator workflows?

Jim Matthews Administrator

It looks like the Filemaker script that exports the text and PDF files could then run an AppleScript. The AppleScript could either tell Fetch to upload the files, or could run an Automator workflow (which would then tell Fetch to upload the files).

The trick would be getting the locations (folder and file names) of the exported files to the AppleScript. Is that information stored in fields in the database?

PeterB Member

Hi Jim
Result.

Thank you for your patience and your knowledge
My finished script is:

tell application "FileMaker Pro"
set exportedFilePath to cell "PDF Path LHP" of current record
end tell
tell application "FileMaker Pro"
set exportedMovePath to cell "AdFast_Dump" of current record
end tell
tell application "FileMaker Pro"
set uploadURL to cell "UploadURL" of current record
end tell
tell application "Fetch"
put into url uploadURL item alias exportedFilePath
end tell
tell application "Finder"
move alias exportedFilePath to folder exportedMovePath
end tell

It may be simple for you but without your help I would be totally lost. People like you make this world a better place.

Jim Matthews Administrator

Glad you got it working! You could combined the tell Filemaker blocks and shorten it a bit to this:

tell application "FileMaker Pro"
set exportedFilePath to cell "PDF Path LHP" of current record
set exportedMovePath to cell "AdFast_Dump" of current record
set uploadURL to cell "UploadURL" of current record
end tell
tell application "Fetch"
put into url uploadURL item alias exportedFilePath
end tell
tell application "Finder"
move alias exportedFilePath to folder exportedMovePath
end tell