• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
re: Trying To Upload To FTP Using Fetch From Filemaker
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

re: Trying To Upload To FTP Using Fetch From Filemaker


  • Subject: re: Trying To Upload To FTP Using Fetch From Filemaker
  • From: David Crowe <email@hidden>
  • Date: Wed, 11 Apr 2007 09:31:15 -0600

Title: re: Trying To Upload To FTP Using Fetch From Filemaker
Dave;

I use Fetch extensively to do uploads etc. from within FMP. The script I paste in below actually even stores and retrieves the password from within the keychain (using the fast keychain functions). The FTP information is stored within FileMaker. It should be easy to adapt it to your circumstance. The first time the script runs for a site it will prompt you for the password which gets stored in the keychain for next time. It uses a static name for the file exported from FileMaker but then renames it correctly. This works somewhat easier with FMP although now it is possible to name an exported file using variables.

- David Crowe



property debug : false

property logfile : (path to temporary items as string) & "Upload.txt"
property fd : 0

property FetchTimeout : 240 -- seconds

try
    
set fd to open for access logfile with write permission
set eof fd to 0
on error errmsg number errnum
   
set fd to 0
end try

trace("UploadExportFile: starting.")

tell application "Website Manager"
       
tell layout "g" to set {ftpaccount, ftpdomain, ftppath, ftpport, ftpprotocol, ftpkeynumber, ftpfilename, WebsiteManagerFile} to {cell "gFTPaccount", cell "gFTPdomain", cell "gFTPpath", cell "gFTPport", cell "gFTPprotocol", cell "gFTPKeyChainIndex", cell "gFTPFileName", cell "gWebsiteManagerFile"} of current record
end tell

try
 
set WebsiteManagerPath to POSIX file WebsiteManagerFile as alias
       
tell application "Finder" to set ExportFile to (((container of WebsiteManagerPath) as string) & "ExportTemp2.htm") as alias
on error errmsg number errnum
       
ReportError(errmsg, errnum)
    
try
            
close access fd
end try
return
end try
trace(("ExportFile=" & ExportFile as string) & ".")

if ftpprotocol = "afp" then
    
trace("Transferring " & ftpfilename & ".")
     
TransferFile(ftppath, ftpfilename, ExportFile)
 
tell application "Website Manager" to tell layout "g" to set cell "gAppleScriptReturnedString" to "Done File Transfer!"
try
            
close access fd
end try
return
end if

if ftpaccount = "" or ftpdomain = "" or ftpfilename = "" or WebsiteManagerFile = "" then
   
activate
       
error "Invalid data from FileMaker: ftpaccount=" & ftpaccount & ", ftpdomain=" & ftpdomain & ", ftppath=" & ftppath & ", ftpprotocol=" & ftpprotocol & ", ftpkeynumber=" & ftpkeynumber & ", ftpfilename=" & ftpfilename & ", WebsiteManagerFile=" & WebsiteManagerFile & "."
end if

trace("Uploading " & ftpfilename & ".")


tell application "Finder" to set ExportFile to (((container of WebsiteManagerPath) as string) & "ExportTemp2.htm") as alias



UploadExportFile(ftpprotocol, ftpdomain, ftpport, ftpaccount, ftppath, ftpkeynumber, ftpfilename, ExportFile)

tell application "Website Manager" to tell layout "g" to set cell "gAppleScriptReturnedString" to "Done FTP Transfer!"
try
    
close access fd
end try
--
----------------------------------------------------------------------------

on ReportError(errmsg, errnum)
     
trace("Report Error: " & errmsg & " (" & errnum & ")")
 
tell application "Website Manager" to tell layout "g" to set {cell "gAppleScriptErrorString", cell "gAppleScriptErrorNumber"} to {errmsg, errnum}
end ReportError

on UploadExportFile(theProtocol, theServer, thePort, theAccount, thePath, KeyNumber, theFileName, ExportFile)
  
       
if thePort = "" then set thePort to "21" -- Default ftp port
   
set thePort to thePort as integer
      
       
trace("Get password for account: " & theAccount)
       
set {thePassword, errmsg, errnum} to getFTPPassword(theServer, thePort, theAccount, thePath, theProtocol, KeyNumber as integer)
if thePassword = "" then
               
activate
               
try
                    
display dialog "Password could not be obtained from the Keychain, please enter it directly" default answer "" with hidden answer
               
on error
                       
ReportError(errmsg, errnum)
                    
return
         
end try
       
set thePassword to text returned of the result
 
end if
 
trace("Got password, " & (count thePassword & " characters."))
 
       
set {worked, errmsg, errnum} to UploadFile(theProtocol, theServer, thePort, theAccount, thePath, KeyNumber, thePassword, theFileName, ExportFile)
        if worked then
         
tell application "Website Manager" to tell layout "g" to set {cell "gAppleScriptReturnedString"} to {"Completed"}
      
else
           
ReportError(errmsg, errnum)
    
end if
end UploadExportFile


on getFTPPassword(theServer, thePort, theAccount, thePath, theProtocol, KeyNumber)
   
my trace("getFTPPassword(" & theServer & "," & thePort & "," & theAccount & "," & thePath & "," & theProtocol & "," & KeyNumber & ")")
 
tell application "Finder"
              
set keychainname to name of (path to home folder)
      
end tell
       
       
tell application "Usable Keychain Scripting"
           
if theProtocol = "ftp" then
                    
tell application "Fetch" to set ProtocolType to FTP
            
else if theProtocol = "sftp" then
                      
tell application "Fetch" to set ProtocolType to SFTP
           
else
                   
my trace("Invalid protocol: " & theProtocol)
                   
return {"", "Invalid protocol: " & theProtocol, -1}
            
end if
         
               
try
                    
current keychain
               
on error errmsg number errnum
                  
my LastWords("Keychain does not Exist", "Your personal keychain (" & keychainname & ") does not exist. FTP operations are not possible.")
                      
return {"", errmsg, errnum}
            
end try
       
               
tell current keychain
                  
try -- to get password from keychain, if not we'll ask user
                            
set x to first internet password whose server = theServer and path = thePath and account = theAccount ¬
                                --
and  (port = thePort or (thePort = 21 and port = 0)) and protocol = ProtocolType
                            
set thePassword to password of x
                               
return {thePassword, "", 0}
                             (*
on error errmsg number errnum                                set s to "Cannot get password on key for ftp://" & theServer & "/" & thePath & ":" & thePort                            display dialog s giving up after 20 *)
                 
end try
       
end tell
       
end tell
       
        --
We searched and searched and didn't find the PIN
    
tell me
       
beep
           
activate
               
try
                    
display dialog "The password for '" & theAccount & "' is not yet stored in the keychain. Please enter now." default answer "" with hidden answer
                       
set thePassword to text returned of the result
                 
tell application "Keychain Scripting"
                          
try
                                    
set theKey to make new Internet key with properties {server:theServer, path:thePath, port:thePort, protocol:FTP, account:theAccount, password:thePassword}
                                     
my trace("Stored new password for account '" & theAccount & "'.")
                              
on error errmsg number errnum
                                  
my LastWords("Password Write Failure", "Cannot store password for ftp site. " & errmsg)
                               
return {thePassword, "", 0}
                            
end try
                       
                               
try -- to store keychain. If we can't it probably means it is already there (this can happen when the user doesn't unlock the keychain before a timeout occurs)
                               
my StoreKeyIndex(KeyNumber)
                                     (*
on error errmsg number errnum                                        my LastWords("Database Update Failure", "Cannot store keychain index in database. " & errmsg)                                   return {thePassword, "", 0} *)
                         
end try
                       
                               
return {thePassword, "", 0}
                    
end tell
               
on error
                       
return {"", errmsg, errnum} -- User cancelled probably
         
end try
end tell
       
return {"", errmsg, errnum}
end getFTPPassword

on StoreKeyIndex(KeyIndex)
tell application "Website Manager" to tell layout "g" to tell current record to set cell "gFTPKeyChainIndex" to KeyIndex
end StoreKeyIndex

on UploadFile(theProtocol, theServer, thePort, theAccount, thePath, KeyNumber, thePassword, theFileName, ExportFile)
   --
Build complete URL
  
set theURL to theProtocol & "://"
      
if theAccount ‚ "" then set theURL to theURL & theAccount & ":" & thePassword & "@"
    
set theURL to theURL & theServer
       
if thePort ‚ 21 then set theURL to theURL & ":" & thePort
      
if last character of theURL ‚ "/" then set theURL to theURL & "/"
      
set theURL to theURL & thePath
  --
if last character of theURL ‚ "/" then set theURL to theURL & "/"
   
        --
OpenTransferWindow(theProtocol, theAccount, thePassword, theServer, thePort, thePath)
       
       
tell application "Finder"
              
set ExportFolder to (container of ExportFile) as string
       
set ExportFileName to (name of ExportFile) as string
           
               
try
                    
delete file (ExportFolder & theFileName)
               
end try
       
try
                    
set name of file (ExportFolder & ExportFileName) to theFileName
       
on error errmsg number errnum
                  
my LastWords("Rename Failure", "Cannot rename export file (" & ExportFileName & ") to " & theFileName & ". " & errmsg)
                        return {false, errmsg, errnum}
         
end try
       
set theFile to (ExportFolder & theFileName) as alias
   
end tell
       
       
       
my trace(("put into url " & theURL as string) & " item " & theFile as string)
  
with timeout of FetchTimeout seconds
           
tell application "Fetch"
                       
try
                            
put into url theURL item theFile
                       
on error errmsg number errnum
                          
my LastWords("Cannot upload file", "Cannot upload export file with URL" & return & theURL & return & errmsg)
                           
return {false, errmsg, errnum}
                 
end try
       
end tell
       
end timeout
    
       
try
            
tell application "Finder" to delete theFile
    
on error errmsg number errnum
          
my LastWords("Deletion Failure", "Cannot delete export file. " & errmsg)
               
return {false, errmsg, errnum}
 
end try

       
return {true, "", 0}
end UploadFile


on trace(msg)
if debug then tell application "Smile" to postit msg as string
 
if fd ‚ 0 then tell me to write msg & return to fd
end trace

on TransferFile(ftppath, ftpfilename, ExportFile)
   
tell application "Finder"
              
               
try
                    
delete file (ftppath & ftpfilename) as alias
           
end try
       
               
set theMovedExportFile to duplicate ExportFile to folder ftppath with replacing
       
               
set name of theMovedExportFile to ftpfilename
          
               
try
                    
delete ExportFile -- will fail if the file is being moved within the same volume.
              
end try
       
       
end tell
end TransferFile

on OpenTransferWindow(theProtocol, theAccount, thePassword, theServer, thePort, thePath)
       
set theURL to theProtocol & "://"
      
if theAccount ‚ "" then set theURL to theURL & theAccount & ":@"
       
set theURL to theURL & theServer
       
if thePort ‚ 21 then set theURL to theURL & ":" & thePort
      
if last character of theURL ‚ "/" then set theURL to theURL & "/"
      
       
tell application "Fetch"
               
set theWindowList to {hostname, username} of every transfer window
             
repeat with aWindow in theWindowList
                   
if item 1 of aWindow = theServer and item 2 of aWindow = theAccount then return ("OK")
         
end repeat
             
try
                    
make new transfer window at beginning with properties {hostname:theServer, username:theAccount, password:thePassword, initial folder:thePath}
          
on error
                       
my trace("Error making new transfer window for <" & theURL & ">")
              
end try
end tell
       
end OpenTransferWindow


on LastWords(hdr, msg)
   
tell me
       
activate
               
beep
           
set LastErrorMessage to "Error: " & msg
       
display alert hdr message LastErrorMessage as critical buttons {"OK"} default button "OK" giving up after 10
   
end tell
end LastWords
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Prev by Date: Re: Trying To Upload To FTP Using Fetch From Filemaker
  • Next by Date: Re: Sort a List with Integers & Letters ( Reverse Speed Results When Using TEXT)
  • Previous by thread: Re: Trying To Upload To FTP Using Fetch From Filemaker
  • Next by thread: Finder - List - Question
  • Index(es):
    • Date
    • Thread