Modified files treated differently under macOS

Started by Joanna Carter, July 24, 2019, 04:06:41 AM

Previous topic - Next topic

Joanna Carter

This is a strange one.

I am writing an app, which will import the URLs of image files (NEF) sent to it via "Open with..."

Once in the app, I change the keywords in the EXIF of some of the files.

If I then select the same files again and send them to my app using "Open with..." again. This time, the URLs are received in two "bunches": the modified files first, followed by those that have not been modified.

It is not a matter of whether the files contain keywords or not, because I can remove the keywords and the difference still exists.

What is ExifTool doing to my files that might cause this? Any ideas?

Joanna Carter

AFAICT, the "command line" that I am constructing in code should be :


exiftool -preserve -ignoreMinorErrors -overwrite_original -Keywords=Test JNA_0031.NEF


For clearing keywords, I use :


exiftool -preserve -ignoreMinorErrors -overwrite_original -Keywords= JNA_0031.NEF


Both operations give me the same problem that files are "imported" into my app in two passes.

If I use Exif Editor to change keywords, which I believe uses ExifTool as well, The problem does not appear. What are they doing differently?

Joanna Carter

Got it !!!  ;D

I should have used :


exiftool -preserve -ignoreMinorErrors -overwrite_original_in_place -Keywords=Test JNA_0031.NEF


Can someone please explain the difference in plain English? And what do the two versions do to the file to cause the problem I had?

Phil Harvey

I don't understand exactly what you are doing, but ExifTool creates a temporary file when it is writing, and renames a temporary file to overwrite the original when -overwrite_original is used.  Changing this to -overwrite_original_in_place instead causes the original file to be modified.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Joanna Carter

Thanks for that Phil. That solved the problem.

I am now in a discussion on the Apple developer forums about why this should make a difference to opening files with an app.

An Apple developer suggested that I tried sending files to Preview (the default Apple image tool) and, lo and behold, it is not just my app that suffers from the same problem. Files modified with the -overwrite_original parameter were opened in one Preview window, whilst unmodified files were opened in another.

Thanks to your explanation, I now understand the difference between the two flags, but it would seem that macOS isn't so understanding  ;)

P.S. Can I humbly suggest that, for the security of the original file, when writing such changes, copying the original to a temporary, modifying the original and then deleting the "security" copy on success? That way, if the write fails, the user only has to delete the failed original and rename the temporary copy. Or is that what you are already doing?  :)

Phil Harvey

I understand now what you mean by "two bunches".  I have found MacOS to be quirky when opening a number of files at the same time in Preview.  I've seen similar behaviour myself with files not edited by ExifTool.

Quote from: Joanna Carter on July 25, 2019, 05:05:40 AM
P.S. Can I humbly suggest that, for the security of the original file, when writing such changes, copying the original to a temporary, modifying the original and then deleting the "security" copy on success? That way, if the write fails, the user only has to delete the failed original and rename the temporary copy. Or is that what you are already doing?  :)

Here is the procedure when -overwrite_original_in_place is used:

1. Rewrite the file to a temporary file, abort if any error occurs.

2. Disable CTRL-C during the following operations to prevent interruption by the user.

3. Open the original file for writing.  On error, erase the temporary file and abort.

4. Copy the temporary file over the original. On error, attempt to rename the temporary file to replace the original.  If the rename fails, leave the temporary file and abort.

5. On success, erase the temporary file.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Joanna Carter

The guy at Apple just got back to me and said :

QuoteIt occurs to me that the difference in the files might be that they have different (though compatible) UTIs, and they're being batched based on UTI rather than receiving app.

You see the behavior as a problem, but it's a genuine question why you would expect the URLs all to be delivered in one batch. I'm not aware of anything that makes that API contract. (Also, keep in mind that the delivery is likely not under control of the Finder, but rather of Launch Services. The semantics of "the user selected and opened these together" might get a bit blurry as the URLs traverse multiple subsystems.)

Although I'm not quite sure I get everything he says yet, I get the feeling it is something that macOS may not handle as smoothly as one would have hoped  ;)

Phil Harvey

Joanna,

This makes sense.  If launch services groups files according to the MacOS file type, then the order will change if the MacOS file attributes are lost when the file is rewritten.  The only way to preserve these attributes when writing with ExifTool is to use the -overwrite_original_in_place option.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).