Your best friend for file transfer.

Fetch application logoFetch

Applescript question (4 posts)

  • Started 2 months ago by Nico
  • Latest reply 2 months ago from Jim Matthews
  • Nico Member

    Hello Jim (and others),

    I have a brief question about the Applescript "Put into" command.
    When uploading files, I want them to have unique names if a file with the same name already exists on the target.
    As I understood from the Fetch Applescript Dictionary, and some examples I found on the internet, the correct statement is:

    on open doc
    set fetchconnection1 to fetchconnect(theServerAddress, thePort, theUserName, thePassword, theDirectory)
    -- this is a seperate subroutine setting up the connection

    set fetchconnectionstatus1 to fetchconnectionstatus()
    -- this is a seperate subroutine asking Fetch status of window 1

    if fetchconnectionstatus1 = "Verbonden." or fetchconnectionstatus1 = "Connected." then
    repeat with oneDoc in doc
    tell application "Finder"
    set oneDocName to name of item (contents of oneDoc)
    end tell
    with timeout of 60000 seconds
    try
    tell application "Fetch"
    try
    put into transfer window 1 item oneDoc with uniquename
    on error the_errormessage
    set logdata to ((current date) as string) & " *TRANSF* FAILED uploading " & oneDocName & " to Local NAS." & the_errormessage & return
    write_to_log(logdata, logfile, true)
    end try
    end tell
    set logdata to ((current date) as string) & " *TRANSF* Uploaded " & oneDocName & " to Local NAS." & return
    write_to_log(logdata, logfile, true)
    on error the_errormessage
    set logdata to ((current date) as string) & " *TRANSF* FAILED uploading " & oneDocName & " to Local NAS." & the_errormessage & return
    write_to_log(logdata, logfile, true)
    end try
    end timeout
    end repeat
    end open doc

    However: the command "put into transfer window 1 item oneDoc with uniquename" does not create a new file if a file with the same name exists on target.
    What am I doing wrong?

    Thanks in advance!

    Posted 2 months ago #

  • Jim Matthews Administrator

    Hi,

    Thanks for asking -- I'm always happy to hear from people using Fetch's AppleScript support.

    Could you try uploading a file with this script, and then choose "Fetch Transcript" from the Window menu and send me the contents of that window, along with the name of the file you tried to upload? I'd like to see what commands Fetch is sending.

    You can post the transcript here, or submit it at http://fetchsoftworks.com/fetch/feedback and I'll respond by email.

    Thanks,

    Jim Matthews
    Fetch Softworks

    Posted 2 months ago #

  • Nico Member

    Hello Jim,

    As it turns out, according to Ben Spink - owner of CrushFTP, FTP Unique is no part of the SFTP protocol. This is his response on my question:
    ---
    FTP "Unique" mode is a feature of the FTP protocol. It uses "STOU" to upload (STOR) with Unique (U).

    SFTP has no such concept as it was a foolish thing to have put into the FTP protocol. SFTP expects the client to specify a unique name as SFTP provides the ability to check if a file exists, where FTP didn't provide this ability, and later provided it in many none standard ways.
    ---

    Posted 2 months ago #

  • Jim Matthews Administrator

    SFTP does provide a way to say "create a new file with this name, and return an error if the file already exists." That's the feature that CrushFTP is not implementing (and it's better than checking if a file exists and then creating it, since that can lead to a race condition). Could you ask Ben to check out the description of SSH_FXF_EXCL in section 6.3 of the SFTP spec?

    http://tools.ietf.org/search/draft-ietf-secsh-filexfer-04

    In the meantime, you should change your script to check if a file exists. Here's an example of how you might do it:

    on splitFilename(filename)
    set savedTILs to AppleScript's text item delimiters
    set AppleScript's text item delimiters to "."

    set parts to every text item of filename
    if (count of parts) > 2 then
    set basename to (items 1 thru -2 of parts) as string
    set parts to {basename, item -1 of parts}
    end if

    set AppleScript's text item delimiters to savedTILs

    return parts
    end splitFilename

    on addIndexToFilename(filename, theIndex)
    set nameParts to splitFilename(filename) of me
    if (count of nameParts) < 2 then
    return filename & " " & (theIndex as string)
    else
    return (item 1 of nameParts) & " " & (theIndex as string) & "." & (item 2 of nameParts)
    end if
    end addIndexToFilename

    on pickUniqueName(defaultName, namesInUse)
    set nameToUse to defaultName
    set nameIndex to 1
    repeat while namesInUse contains nameToUse
    set nameToUse to addIndexToFilename(defaultName, nameIndex) of me
    set nameIndex to nameIndex + 1
    end repeat
    return nameToUse
    end pickUniqueName

    on uploadUnique(f)
    set finfo to info for f
    set fname to name of finfo
    tell application "Fetch"
    set allNames to name of every remote item of transfer window 1
    end tell
    set nameToUse to pickUniqueName(fname, allNames) of me
    tell application "Finder"
    set name of file f to nameToUse
    end tell
    tell application "Fetch"
    put into transfer window 1 item f
    end tell
    tell application "Finder"
    set name of file f to fname
    end tell
    end uploadUnique

    set f to choose file
    uploadUnique(f) of me

    Thanks,

    Jim Matthews
    Fetch Softworks

    Posted 2 months ago #

Reply

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