Your best friend for file transfer.

Fetch application logoFetch

Hot Folder to Upload Files using Applescript (26 posts)

  • Started 13 years ago by Josh Smith
  • Latest reply 12 years ago from PeterB
  • 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

    Posted 13 years ago #

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

    In this line:

    display dialog the alert_message buttons {"Yes", "No"} default button 2 with icon 1 giving up after dialog_timeout

    You are referencing the variable "dialog_timeout" but it has not been defined. I would try removing "giving up after dialog_timeout" from that line; so you would have just:

    display dialog the alert_message buttons {"Yes", "No"} default button 2 with icon 1

    and see if that helps.

    Let us know how it goes.

    Thanks,

    Scott McGuire
    Fetch Softworks

    Posted 13 years ago #

  • Josh Smith Member

    Hi Scott

    1.Yes getting rid of the Error code did the trick.

    Fetch duly uploads and mirrors the files.

    2. Is there an applescript command to ask Fetch to "activate in the background " rather than jump to front of the open applications ?.

    3. Also does the mirroring command always need to check the modification date of the files or can this be scripted out.

    Many thanks for your help

    Josh Smith

    ------ Earlier Error --------------------------

    display dialog the alert_message buttons {"Yes", "No"} default button 2 with icon 1 giving up after dialog_timeout

    You are referencing the variable "dialog_timeout" but it has not been defined. I would try removing "giving up after dialog_timeout" from that line; so you would have just:

    display dialog the alert_message buttons {"Yes", "No"} default button 2 with icon 1

    -------

    Posted 13 years ago #

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

    Thanks,

    Scott McGuire
    Fetch Softworks

    Posted 13 years ago #

  • Josh Smith Member

    Hi Scott

    Thanks for the latest info.

    Yes, you are correct, we don't really require a mirroring of files, our goal is the upload of the most recent additions - which is done on a daily basis.

    Please let me know how I might go about this in Fetch

    Many thanks

    Josh

    Posted 13 years ago #

  • Jim Matthews Administrator

    Hi Josh,

    Try replacing the mirror alias line of your script with:

    put into url "ftp://redsnapperphoto:@ftp2.donhost.co.uk/www/Uploads/TestMirrorUpload/" items added_items

    That should upload only the items that were just added to the folder.

    Thanks,

    Jim Matthews
    Fetch Softworks

    Posted 13 years ago #

  • 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

    I know I am been cheeky but I really need your help.

    Many thanks

    Peter

    Posted 12 years ago #

  • Jim Matthews Administrator

    Hi PeterB,

    You can do what you want using Automator; the basic instructions are available here:

    http://fetchsoftworks.com/fetch/help/Contents/Tutorial/SettingUpWatchFolder.html

    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.

    Let me know how it goes!

    Jim Matthews
    Fetch Softworks

    Posted 12 years ago #

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

    Next stage please
    Move completed files to another folder

    Many thanks

    Peter

    Posted 12 years ago #

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

    Let me know how it goes!

    Jim Matthews
    Fetch Softworks

    Posted 12 years ago #

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

    Hope this makes sense.

    Any thoughts?

    Thank you

    Peter

    Posted 12 years ago #

  • 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?

    Thanks,

    Jim Matthews
    Fetch Softworks

    Posted 12 years ago #

  • PeterB Member

    Hi Jim
    Mac OS X
    Filemaker 11.0v4

    Filemaker can trigger scripts but not sure about automator

    Found this http://www.automatedworkflows.com/products/software/automator-actions-for-mac-os-x-10-610-7-snow-leopard-lion/

    Thanks

    Peter

    Posted 12 years ago #

  • 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?

    Thanks,

    Jim Matthews
    Fetch Softworks

    Posted 12 years ago #

  • PeterB Member

    All information for file and folder names are in the database

    Thanks

    Peter

    Posted 12 years ago #

  • Jim Matthews Administrator

    That's great. You should be able to create a Filemaker script with a Perform AppleScript step that runs a script like this:

    tell application "FileMaker Pro"
    set exportedFilePath to cell "ExportedFilePath" of current record
    end tell
    tell application "Fetch"
    put into url "ftp://username:@hostname/path/" item alias exportedFilePath
    end tell
    tell application "Finder"
    move alias exportedFilePath to beginning of folder "HD:Users:username:Documents:Fetch_Uploaded:"
    end tell

    The first step gets the path to the exported file from the database. Then Fetch uploads the file. Finally, the Finder moves the file into the folder that holds uploaded files.

    Does that make sense?

    Thanks,

    Jim Matthews
    Fetch Softworks

    Posted 12 years ago #

  • PeterB Member

    So far:
    the file is uploaded to the FTP site.

    On moving the file I get the following error:

    Finder got an error: Can’t make beginning of folder "Macintosh HD:Users:peter_beverley:Desktop:AdFast_Dump:" into type folder.

    I tried taking out the words 'beginning of' and it moved the file to the folder but it was zero kb

    Thanks

    Peter

    Posted 12 years ago #

  • Jim Matthews Administrator

    Sorry about that -- you don't want the "beginning of" part.

    As for the zero kb -- is that the size of the file that was uploaded? Is it possible that the export is creating an empty file?

    Jim Matthews
    Fetch Softworks

    Posted 12 years ago #

  • PeterB Member

    The uploaded file was correct at 1.4mb
    It left the exported file in the original folder and created the zero kb in the move to folder

    Thanks

    Peter

    Posted 12 years ago #

  • Jim Matthews Administrator

    That's very strange -- I don't think the move command is specified correctly, because it will not leave you with two files, or an empty file.

    Posted 12 years ago #

  • PeterB Member

    Hi Jim
    Once I clicked on the Zero Kb file it updated to show the correct file size. But it copied the file rather than moving it, so I have 2 files.

    Also, how do I replace the URL address and the Move file path to use a Fields from Filemaker?

    Thanks

    Peter

    Posted 12 years ago #

  • PeterB Member

    Hi Jim
    Hmm, moving files is now working perfect.

    I just need to know how to replace the URL address and the Move file path to use a Fields from Filemaker?

    Thanks

    Peter

    Posted 12 years ago #

  • Jim Matthews Administrator

    You would use something like this:

    tell application "FileMaker Pro"
    set uploadURL to cell "UploadURL" of current record
    end tell

    then replace the URL in the "put into" statement with uploadURL.

    Thanks,

    Jim Matthews
    Fetch Softworks

    Posted 12 years ago #

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

    Thanks again Jim

    Peter

    Posted 12 years ago #

  • 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

    Thanks for using Fetch!

    Jim Matthews
    Fetch Softworks

    Posted 12 years ago #

  • PeterB Member

    Of course
    I have changed it

    Thanks again

    Peter

    Posted 12 years ago #

Reply

  • Or nickname, if you prefer.
  • This will be kept confidential.
  • This is to ensure that you’re a person, not a spambot.