uemtux

Upload Contents of Container Field to FTP (Mac OS / AppleScript technique)

10 posts in this topic

Hi All, 

 

I just thought I'd try to give back to the community that has given so much to me by writing a little tutorial. This technique is one I developed on Macs. Mac OS X has the curl command line tool as part of the default installation. Curl is like a web swiss army knife. You can fetch files, POST data, upload to or download from FTP servers etc... so it brings a lot of functionality to the table which isn't built into FileMaker. I make use of curl through the "do shell script" AppleScript command, which in FileMaker I use through the "Perform AppleScript" script step. Here's a simple 6 line script that lets you take the contents of a container field and upload it to an FTP site. Substitute your container field in the first and fifth lines and your FTP information in the $script variable to make it functional. This script could be extended by actually having variables for the FTP credentials/location too but I've kept it simple here just to demonstrate the technique.

 

 

set variable [$file_name; GetAsText(YourTable::YourContainerField)]

set variable [$path_temp; Get(TemporaryPath) & $file_name]

set variable [$path_unix; Middle($path_temp ; Position($path_temp ; "/" ; 1 ; 2) ; Length($path_temp))]

set variable [$script; "do shell script "curl -T " & $path_unix & " ftp://USERNAME:PASSWORD@FTP_HOST/FTP_DIRECTORY/" & $file_name &"""]

Export Field Contents [YourTable::YourContainerField; "$path_temp"]

Perform AppleScript [$script]

 

Enjoy!

Share this post


Link to post
Share on other sites

This is fantastic, and so simple. It worked for me on the first try. I couldn't believe it. Thank you.

Share this post


Link to post
Share on other sites

NOTE FOR FUTURE VIEWERS OF THIS THREAD:

 

The script as-is may not work since as of FileMaker 12 GetAsText(containerField) returns more than just the filename. You may have to look for and remove additional text such as "remote:"

Share this post


Link to post
Share on other sites

Wondering...Is there a Apple script replacement for Windows?

Share this post


Link to post
Share on other sites

Simple - and did just what it is supposed to do!

 

FMPro 13 allows easier extraction of the container file name, by using the GetContainerAttribute function:

GetContainerAttribute ( GetContainerAttribute ( sourceField ; attributeName ) and substituting "filename" for "attributeName".

Other attributes can easily be read, too, as listed here:

http://help.filemaker.com/app/answers/detail/a_id/11888/~/container-field-enhancements-in-filemaker

 

HOWEVER... I suspect that the "curl-T" command has problems with specific characters in the file name.

I have found that the script just hangs if the file name contains spaces, "&", or other such non-UNIX-friendly characters.

Is there a way to overcome this, and still preserve the original filename?

Share this post


Link to post
Share on other sites

Hi 

I am having trouble entering line 4 in the above script. The value of $script rejects the 'do shell script' as an unrecognised table.

How do we define the script in Filemaker 14

Am I missing something?

Edited by bmcintosh46

Share this post


Link to post
Share on other sites
On 11 February 2013 at 3:44 AM, uemtux said:

"do shell script "curl -T " & $path_unix & " ftp://USERNAME:PASSWORD@FTP_HOST/FTP_DIRECTORY/" & $file_name &"""

But I cannot get this text into the variable value field.

Edited by bmcintosh46

Share this post


Link to post
Share on other sites

I believe it needs to be:

"do shell script \"curl -T " & $path_unix & " ftp://USERNAME:PASSWORD@FTP_HOST/FTP_DIRECTORY/" & $file_name & "\""

Untested.

Share this post


Link to post
Share on other sites

Thanks Comment

It took me a number of hours but I did eventually work this out. Seemed strange to me that other were excited about the script working and yet it wasn't displayed correctly.

Thanks for you time

Share this post


Link to post
Share on other sites
2 minutes ago, bmcintosh46 said:

Seemed strange to me that other were excited about the script working and yet it wasn't displayed correctly.

It probably was displayed correctly back in 2013, but didn't survive all the forum up?grades since.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • By fmgenius
      I am trying to do a basic Insert from URL call with some CURL headers added and I keep getting authentication errors. This is the curl command sent by the provider for me to use. 
       
      curl -X GET --header 'Accept: application/json' --header 'api_key: 1234567890abcdefg' 'https://mydomain.leaddocket.com/api/endpoint' This is the curl I am setting up
      -X GET \ --header 'Accept: application/json' \ --header 'api_key: 1234567890abcdefg' \ -trace $$curlTraceDump FM barks and says "Authentication Failed" but if I do this with the BaseElements plugin, it works just fine. 
      What I found when looking at the Curl Trace was that FM is dropping some of the headers I send and injecting its own. Here's the snippet I am finding from FM's trace:
      GET /api/Leads/P endingExportIds HTTP/1.1 Host: mydomain.leaddocket.com User-Agent: FileMaker/16.0 Accept: */* Accept-Encoding: deflate, gzip Notice: both headers I sent were stripped and replaced. 
      Here is the response from BaseElements trace
      GET /api/endpoint HTTP/1.1 Host: mydomain.leaddocket.com User-Agent: libcurl-agent-base-elements-plugin/3.3.4 Accept: application/json api_key: 1234567890abcdefg BE left my headers intact. 
       
      So what gives? 
    • By 357manch
      With the introduction of cURL options in FM16's Insert from URL script step I've been investigating the possibility of being able to save a copy of my FM solution directly to my Dropbox folder using the https://content.dropboxapi.com/2/files/upload element from Dropbox V2 API.
       
      I've successfully created the access token to pass through as part of this step and the Insert from URL script step successfully creates the stated folder and file in my Dropbox.  However, the file size is always 0 bytes.  I've tried 2 different methods
       
      METHOD 1
      #Export a compacted copy of this file to Temporary Folder
      Set Variable [ $file_path ; Get ( TemporaryPath ) & "Solution.fmp12" ]
      Save a Copy as [ "$file_path" ; compacted ]
       
      #Set my cURL options to variable
      Set Variable [ $curl_header ;
      "curl -X POST https://content.dropboxapi.com/2/files/upload \\" & ¶ &
      "--header \"Authorization: Bearer " & PREFERENCES::Dropbox_AccessToken & "\"  \\" & ¶ &
      "--header \"Dropbox-API-Arg: {\\\"path\\\": \\\"/DB2FM/Solution.fmp12\\\"}\" \\" & ¶ &
      "--header \"Content-Type: application/octet-stream\" \\" & ¶ &
      "--data-binary @" & $file_path ]
       
      #Dropbox URL
      Set Variable [ $dropbox_url ; "https://content.dropboxapi.com/2/files/upload" ]
       
      #Make Request - Set to variable adn then format to JSON field
      Insert from URL [ Verify SSL Certificates ; With dialog: Off ; $json ; $dropbox_url ; cURL options: $curl_header ]
      Set Field [ PREFERENCES:Dropbox_JSON ; JSONFormatElements ( $json ) ]
       
       
       
      METHOD 2
      #Export a compacted copy of this file to Temporary Folder and then import to a container field
      Set Variable [ $file_path ; Get ( TemporaryPath ) & "Solution.fmp12" ]  //note1
      Save a Copy as [ "$file_path" ; compacted ]  //note1
      Insert File [ PREFERENCES::Container ; "$file_path" ]  //note1
      #Set variable following instructions on FileMaker website which states:
      #  -  Direct file access is replaced with a FileMaker variable prefixed by the @ character.
      #  -  To access a file, you can set the variable to a container field, which contains the file, then use that variable as the parameter of the cURL option.
      Set Variable [ $container_path ; PREFERENCES::Container ]
       
      #Set my cURL options to variable
      Set Variable [ $curl_header ; 
      "curl -X POST https://content.dropboxapi.com/2/files/upload \\" & ¶ &
      "--header \"Authorization: Bearer " & PREFERENCES::Dropbox_AccessToken & "\"  \\" & ¶ &
      "--header \"Dropbox-API-Arg: {\\\"path\\\": \\\"/DB2FM/Solution.fmp12\\\"}\" \\" & ¶ &
      "--header \"Content-Type: application/octet-stream\" \\" & ¶ &
      "--data-binary @" & $container_path ]
       
      #Dropbox URL
      Set Variable [ $dropbox_url ; "https://content.dropboxapi.com/2/files/upload" ]
       
      #Make Request - Set to variable adn then format to JSON field
      Insert from URL [ Verify SSL Certificates ; With dialog: Off ; $json ; $dropbox_url ; cURL options: $curl_header ]
      Set Field [ PREFERENCES:Dropbox_JSON ; JSONFormatElements ( $json ) ]
       
       
       
      JSON Response For Both Methods
       
      I get the following JSON Response for both methods
      {
          "client_modified" : "2017-05-16T11:22:47Z",
          "content_hash" : "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
          "id" : "id:heJWQ1SWwJsAAAAAAcMWNw",
          "name" : "Solution.fmp12",
          "path_display" : "/DB2FM/Solution.fmp12",
          "path_lower" : "/db2fm/solution.fmp12",
          "rev" : "3a0412f00f58892",
          "server_modified" : "2017-05-16T11:22:48Z",
          "size" : 0
      }
       
      Any ideas as to why the file is created but with no content.  I've also tried this with a number of different file types (.png, .txt, .pdf) by manually inserting a file into the container and greying out the 3 steps marked //note1 above but the result is always the same.
       
    • By ggt667
      Is there a way in AppleScript to trap if the connection is lost and have an AppleScript act on this?
      At the risk of asking and answering myself( AppleScript below )
      repeat tell application "FileMaker Pro" try database "db" on error do shell script "sudo shutdown -r now" with administrator privileges end try end tell delay 59 -- 59 secs end repeat  
    • By imdarek
      Hi, I am using filemaker 11 to copy customer info from excel into the database and then it would generate special keys in pdf allowing us to send the file to the customer.
      My access to the filemaker is non-admin, and the import function of customer info has been blocked.
      We are keying in the record one at a time, so we are looking at ways to automate this process.
      When I try to create a new record using applescript with just 
      it was not allowed. There are data that require validation like date and auto-indexed record number.
      So, is there a way we could create new record and then using the set function to copy data into the report?