Jump to content

Compress file imported into container


This topic is 6627 days old. Please don't post here. Open a new topic instead.

Recommended Posts

I would like to be able to compress (zip, is probably prefered) the files that I import into my FMP7 database. Obviously I could do this manually by first compressing the file, then importing it, but I was hoping that there was a more automated way to do this. Specifically, I would like to have a button trigger a script that would compress the file on the computer, then import it into the container field. If there's a way for FMP7 to do this on its own, then that's great, but I'm not thinking that this is possible. Because of this I was thinking that I could use AppleScript (which I don't know much about) to access the OS's built-in archiving abilities.

Thanks in advance for any help y'all can offer!

Link to comment
Share on other sites

I found a routine which uses a shell script to do this:

-- I'm choosing a file, but it could be a full Mac file or alias reference

set macPath to choose file

set itemPath to quoted form of POSIX path of macPath

--this saves the zip with the file extension (name.jpg.zip)

set savePath to itemPath & ".zip"

do shell script ("ditto -c -k -rsrc " & itemPath & " " & savePath)

-- -k is for a Windows-compatible zip file, -rsrc is for Mac resource forks

The original file still exists afterwards, but you could easily delete it, by adding:

tell application "Finder"

delete macPath

end tell

Link to comment
Share on other sites

This is much the same, but it saves the file without its original extension showing (the unzipped file would still have it). You might want to show the extension in FileMaker however, unless you've already got it some other way. There's also a test to see if it even has an extension, so it gets ".zip" instead of just "zip"

-- this saves the zip without file extension (name.zip)

tell application "Finder"

set macPath to choose file

set sourcePath to quoted form of POSIX path of macPath

set theFolder to container of macPath as string

set fileName to name of macPath

set fileExt to name extension of macPath

if fileExt is not "" then

set extCount to (count of fileExt) + 1

set savePath to quoted form of POSIX path of (theFolder & text items -extCount thru 1 of fileName & "zip")


set savePath to quoted form of POSIX path of (theFolder & fileName & ".zip")

end if

do shell script ("ditto -c -k -rsrc " & sourcePath & " " & savePath)

-- delete macPath

end tell

Link to comment
Share on other sites

Thanks Fenton. I'll take a look at this tomorrow and see if I can understand and implement it. You always take a lot of time to help folks on the forums it seems. I certainly appreciate it and have learned quite a bit.

Link to comment
Share on other sites

So I typed in your AppleScripts and both of the ones that you wrote above did what they were supposed to (compressing the files that were chosen with the appropriate extensions).

How do I integrate these into a FileMaker script that would insert the compressed file into the appropriate container field?

Basically, I have a script right now that is:

Go to Field [container]

Perform AppleScript [Native AppleScript text]

But this doesn't actually insert the compressed file in the chosen field. How do I complete this?


Link to comment
Share on other sites

No, you have to use the FileMaker Insert File step. However, this step can ONLY use a FIXED file reference. And you cannot do this with AppleScript; it cannot insert a "file as a file" into a container field, though it can insert a "image from a file."

You need a FileMaker script step to Insert File from a fixed location/name. You can set it up with a dummy file, it doesn't matter what; but I think it should be a zipped file (with ".zip").

You use AppleScript (or a plug-in) to take your original file (or the zipped version of the same), then rename and move it (with replacing) to be what the fixed file is.

Then a FileMaker step Insert File gets it.

You can just leave the zipped file there afterwards, as the fixed file. The next rename/move operation will overwrite it. Or you can delete it.


1. Perform AppleScript to zip the original, rename/move it to the fixed location

2. At the end of above, call FileMaker script ->

3. Insert File

The syntax to call a FileMaker script from AppleScript is:

--tell application "FileMaker Pro"

tell window 1

do script FileMaker script "script name"

end tell

--end tell

The --commented steps are not needed at the top level of a Perform AppleScript step's text; but are needed in Script Editor.

Link to comment
Share on other sites

OK, that seems clear.

I'm a little disappointed, though, if I understand correctly, that all the files that I import will have to have the same fixed names. My database has a window in which I display the container field, the date that the file was inserted, a text field that I can make notes on the nature of the file, and a GetAsText [container] field that displays the title of the file. With all the files required to have the same name, the title field will be useless. I suppose that I could enter it manually, but that partly defeats the purpose of automating things as much as possible.

A couple follow-up questions:

* What code do I need to add to the AppleScript in order for it to move & rename the zipped file to a standard location?

* If that standard location (a folder named, say, "FileMakerZip") doesn't exist at the time that the script is run (like if I'm operating the database on a new or different computer), can I have the script automatically generate it?

Link to comment
Share on other sites

I just realized another glitch. Because we had to rename the zip file BEFORE Inserting as File, we've lost the original name. Which means it won't show up on the icon in the container field. There's no way to alter that name.

But, since the AppleScript can get the original name at the beginning of the operation, it can set it into a FileMaker field. So just show that instead of the name under the icon (which is more or less meaningless now, a .zip icon with a generic name.

Or you could just zip the file, via an AppleScript or manually, then Insert it manually, via the dialog. But where's the fun in that? And it's 2 steps of looking for the file (since they'd be using a different navigation mechanism).

Link to comment
Share on other sites

I see we're more or less on the same page, with the name glitch, etc.

>* What code do I need to add to the AppleScript in order for it to move & rename the zipped file to a standard location?

This is the guts of the AppleScript:

do shell script ("ditto -c -k -rsrc " & sourcePath & " " & savePath)

There is no reason why "savePath" above could not be the fixed file name/location.

I forgot to include how AppleScript would know where the file is; see the answer below.

>can I have the script automatically generate it (the folder)?

Yes. FileMaker has to know the "relative" location, in relation to the database file, to Insert File; whereas AppleScript needs to know the "absolute" location on the hard drive, to move files.

There is a way to tell AppleScript the absolute location of the database file; by creating a "myFolder" calculation field (unstored) in FileMaker:

Substitute( Left(Get(FilePath); Position(Get(FilePath); "/"; 1; PatternCount(Get(FilePath); "/"))); ["/"; ":"]; ["file::"; ""] )

Add the folder and/or file name for the fixed file, then include that in the 1st Perform AppleScript step.

Then use the "exists" test, and create the folder if needed. But you don't really need a folder, since it's only 1 file, and will be recreated each time.

Link to comment
Share on other sites

OK, so let me see if I understand this. (I'm going to go step-by-step so sorry that this is a bit long.)

1. The first FileMaker script will simply run an AppleScript (one of the two that you have provided above that I have typed into the "Perform AppleScript" step)

2. The AppleScript will ask for the user to locate the file to be compressed

3. The AppleScript will copy the name of the file to be compressed into a field in the FileMaker database

Question: How do we have the AppleScript do this?

4. The AppleScript will compress the selected file

5. The AppleScript will rename and move the compressed file to a specific location (the same location each time)

Question: How do I designate the absolute path in #5 above to which AppleScript will move the renamed file (especially if I will be using this database on different machines)? Do I simply have it save the file to a location that all machines will have like the user's Desktop? If that's the case, how do I designate a path to the Desktop when the name of the machine and the name of the user changes depending on which machine the database is located on and which user is logged in and using it? (Something like ~/Desktop ?)

6. The AppleScript will then tell the FileMaker database (the path to which it acquires via the calculated "myFolder" field) to run a script that will insert the compressed file into a container field specified in the FM script

Question: I've created the "myFolder" field in the database, but how do I integrate this into the AppleScript?

7. The FileMaker "Insert File" script step knows the location of the compressed file via a relative path that I have typed into the "Specify File" dialogue box that is part of the "Insert File" script step

Question: Similar to the question above on designating an absolute path for AppleScript, how do I designate the relative path in #7 for the FileMaker script (if I will be using this database on different machines)?

Question: Can we have the Applescript automatically delete the compressed file from the computer once it is done (as opposed to having it just be overwritten the next time)?

Link to comment
Share on other sites

I'll answer questions by the numbers. Otherwise the steps are correct, and the questions are expected.

You should open the included script files, in Script Editor, and read them. FileMaker is actually running the AppleScripts, in Perform AppleScript steps. But that is not where you either write them or read them; and there is no sytax coloring or checking. That's why I included the separate AppleScript files.

The only difference is that the external files have an extra line(s):

tell application "FileMaker Pro" (or Developer)

--and its corresponding

end tell

These lines are not needed (or wanted) in the FileMaker Perform AppleScript step; but they're needed by Script Editor (who otherwise doesn't know you're talking to FileMaker). You can also just --comment them out in the FileMaker Perform AppleScript text.

3. AppleScript can easily get a file's name and set a field (cell) in FileMaker's current record

5. Yes, this is a problem. FileMaker cannot use the Desktop as a file reference, because it has the short user name in it. It doesn't get the ~/Desktop shortcut (at least I can't make it work).

I thought you were just on your own machine. For networked solutions a different location must be used. The Shared folder is a good one. I'll rewrite the scripts to do that.

The file path must be, for FileMaker:

Macintosh HD/Users/Shared/Filename.zip

or, in AppleScript syntax:

Macintosh HD:Users:Shared:Filename.zip

If you want the file to be deleted, you can use the temporary items folder:

Macintosh HD/private/tmp/501/TemporaryItems/Filename.zip

That's probably better than deleting with AppleScript, 'cause it doesn't make the Trash bulge suspiciously.

I've actually got the opposite process done also. Which is to Export, decompress and move the file to the desktop. AppleScript can always find the user's desktop. It's FileMaker who can't dynamically set file references.

Link to comment
Share on other sites

Great! The import script seems to work well. Thanks.

You very accurately anticipated my follow-up question about exporting! Running the export script, though, I get an error:

"ditto: /private/tmp/501/TemporaryItems/FileExported.zip: No such file or directory"

I compared this path to the path specified in the Insert File script and they are identical, so I'm not sure why one would be able to find this directory and the other script wouldn't.

Any idea why this might be happening?

Link to comment
Share on other sites

This topic is 6627 days old. Please don't post here. Open a new topic instead.

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

Important Information

By using this site, you agree to our Terms of Use.