Change photo date taken to value in date modified - applescript

I came across an old set of photo's taken in the 90's.
These photo's have 2 dates in the exif. A date modified (the correct date) and a date taken (a date somewhere last year). When importing this into my photo database it has the wrong date associated.
I'd like to run an apple automator or applescript to copy the modified date into the taken date.
Anyone willing to help me with this?

Please make a backup first, and then just run the following commands in a directory that is a COPY of your files.
You can do that with exiftool which you can install using homebrew with:
brew install exiftool
I believe the command you want in Terminal is:
exiftool "-EXIF:DateTimeOriginal<FileModifyDate" image.jpg
Just do one file for now, and check before and after with:
exiftool image.jpg
If it all looks good, you can do all the files with:
exiftool "-EXIF:DateTimeOriginal<FileModifyDate" *.jpg

Related

Is there a way to batch copy multiple file's 'Where From' metadata to 'Comments' metadata?

I'm looking for a way to batch copy individual files 'Where From' metadata to 'Comments' metadata.
Only found xattr that can delete metadata, couldn't figure out how xattr can copy the info from 'com.apple.metadata:kMDItemWhereFroms' to 'com.apple.metadata:kMDItemFinderComment'.
I have more than 10,000 files, hoping to find a way to batch process them
I think I understand what you want and I think you can do it with exiftool, but please check on a couple of copies of files in a completely separate directory.
So, to copy the com.apple.metadata:kMDItemWhereFroms to com.apple.metadata:kMDItemFinderComment for a document called file.pdf, you can use:
exiftool '-MDItemWhereFroms>MDItemFinderComment' file.pdf
Then you can check with:
mdls file.pdf
If you have thousands to do, you can specify a directory to start in and -r to recurse, something like:
exiftool '-MDItemWhereFroms>MDItemFinderComment' -r .
You can also limit it to certain files.
On macOS, it is probably simplest to install exiftool with homebrew using:
brew install exiftool

Fill an existing LibreOffice document with values from the command line

I run a bash script which generates a PDF at the end of a billing run. I used to do that with LaTeX but the users ask for a more MS Office like solution. So I'm thinking of using a LibreOffice document and use LibreOffice on the command line to generate the PDF. That works. But I have no idea how to inject the values I need to change (e.g. the address and the billing information) into that document before I can generate a PDF.
Let's assume the example.odt document contains this text:
Dear $fist_name,
you own us $amount USD.
Regards
xyz
Since example.odt is not really easy to edit from a Bash script I'm searching for an other way to inject values for $first_name and $amount.
What is the best way to do this?
The LibreOffice file is a zip archive which can be unzipped with
unzip old.odt -d example
cd example
The content of the file is in the file content.xml. There is can be changed with sed or any other tool. After that the .odt file has to be created again:
zip -r ../new.odt .
After that the PDF can be created with this command (the path is from OS X):
/Applications/LibreOffice.app/Contents/MacOS/soffice --headless
--convert-to pdf:writer_pdf_Export --outdir ~/Desktop/ ~/Desktop/new.odt

How to change Media Created Date in Exiftool?

I have some MP4 videos that don't have Media Created date and time because they were recorded with Instagram camera. I want to set the date and time and I found out that I can do that with Exiftool software. I know that the software works by typing command lines, but I don't know where I should type what. I searched on Google and didn't find any helpful results.
I downloaded the software and got "exiftool(-k).exe" file. What should I do now? I have no idea how it works. I hope somebody can write simple steps just to set that Media Created date.
First you will probably want to rename the exiftool(-k).exe to just exiftool.exe and place it someplace in your PATH (see install exiftool-Windows). There is also Oliver Betz's Alternative Exiftool build for Windows which includes an installer and is a bit more security friendly. See this post on the exiftool forums. For those that use Chocolatey, the Chocolatey exiftool package will add exiftool to the PATH and is a well maintained package
Then you will want to use one of these commands. In the case of your example filename, the filename appears to have been named for the time it was taken i.e YearMonthDay_HourMinutesSeconds. In that case you can simply use this command (see exiftool FAQ #5)
exiftool -api QuickTimeUTC "-CreateDate<Filename" 20181223_000542.mp4
This will work correctly as long as the video was taken in the same time zone as the computer you are currently using. If not, you will have to add the time zone like this:
exiftool -api QuickTimeUTC "-CreateDate<${Filename}-04:00" 20181223_000542.mp4
This is because the CreateDate tag for MP4 files is supposed to be UTC and Windows properties will read it as such. Mac Finder will also correctly adjust from UTC. With the -api QuickTimeUTC option, exiftool will automatically adjust the time to UTC.
If you need to set the time to something different than what the filename is, then you would use this, adding the time zone if needed:
exiftool "-CreateDate=2018:12:23 00:05:42" 20181223_000542.mp4
These commands creates backup files. Add -overwrite_original option to suppress the creation of backup files. If on a Mac, the slower -overwrite_original_in_place option could be used to preserve any MDItem/XAttr data Add the -r (-recurse) option to recurse into subdirectories. If this command is run under Unix/Mac/Powershell, reverse any double/single quotes to avoid shell variable interpretation.
You can process as many files and/or directories as you can put on the command line, so if you wanted to process all the files in c:\Dir1 and C:\Dir2, you would just list both of them at the end of the command c:\Dir1 C:\Dir2

How to convert images from TIFF to JPG preserving the comments and tags

I am using preview (that comes with OS X El Capitan) feature to convert a file form TIFF format into JPG for example. I expected the export process will include the original comments, but it doesn't happen (it applies also for the tag fields).
The generated JPG file has no comment
The compression and change image format work, but the META INFO such as comment or tags are not exported.
Any suggestion or workaround about how to include that information. I need to convert about 500 images so manually copy/paste doesn't work for me.
Updated Answer
In the light of your comments, I think the best way forward is to try and identify how/where the comments are stored for each platform (Windows vs macOS) and then to decide which method you want to use going forward.
macOS Finder/Spotlight comments will not be legible on Windows, so if you want Windows compatibility, you need to standardise on JPEG or EXIF comments.
I recommend using exiftool which you can install with homebrew, using:
brew install exiftool
Then I suggest you try extracting the comments from your files to see how/where they are stored:
exiftool -a image.jpg
will show you all tags in image.jpg. Your comments may be under:
comment - which is the JPEG comment, or
EXIF:UserComment - which is the EXIF comment
If you find your comments in the JPEG or the EXIF section, you can extract just the comments with:
exiftool -comment image.jpg # extract JPEG comment
exiftool -EXIF:UserComment image.jpg # extract EXIF UserComment
Add the option -s3 to suppress the field-names in the above to save having to parse them out.
Likewise, you can set the comments with:
exiftool -comment="FUNKY JPEG COMMENT" image.jpg # set JPEG comment
exiftool -EXIF:UserComment="FUNKY EXIF USER COMMENT" image.jpg # set EXIF UserComment
You can also extract the EXIF user comments to a CSV with:
exiftool -EXIF:UserComment -csv *.jpg
SourceFile,UserComment
a.jpg,FUNKY EXIF:UserComment
b.jpg,b FUNKY EXIF:UserComment
You can also apply comments from a CSV.
You should also be able to extract macOS/Spotlight/Finder comments using the script in my main answer:
$HOME/macOSGetFinderComment "/Users/someone/soneFile.tif"
Original Answer
I would suggest you try the following using ImageMagick.
First, use the Finder, or any other tool you are familiar with, to make a copy of your photos including the entire directory structure to some new place where we cannot damage your existing photos. So, let's say you copy (NOT move) the entire tree of TIFs to a subdirectory called "NEW" inside your HOME directory.
Then start the Terminal and change directory to "NEW":
cd NEW
Easy Method
If all the TIFs are in a single directory or two, just use mogrify:
mogrify -format jpg *.tif
Harder Method
If the TIF files are in multiple directories, you will need to work a bit harder. Inside Terminal copy and paste this:
find NEW -name \*.tif -exec sh -c 'new="${1%.tif}.jpg"; convert "{}" "$new"' _ {} \;
That starts looking in the "NEW" directory for files named "*.tif". When it finds one, it starts a new shell (sh) passing it the filename of the TIF. It then works out the new filename by replacing a trailing "tif" with "jpg" and invokes ImageMagick convert to do the conversion.
As regards the Finder/Spotlight comments, here is a little script to get the Finder comment of a file:
#!/bin/bash
# macOSGetFinderComment
# Pass an absolute path to the file!
file=$1
osascript<<EOF
tell application "Finder" to get comment of item POSIX file "$file"
EOF
And here is one to set the Finder/Spotlight comment:
#!/bin/bash
# macOSSetFinderComment
# Pass an absolute path to the file!
file=$1
comm=$2
osascript<<EOF
tell application "Finder" to set comment of item POSIX file "$file" to "$comm"
EOF
So, I would save those 2 scripts in your HOME directory and then make them executable with:
cd
chmod +x macOS*FinderComment
Then save this file in your HOME directory under $HOME/CopyComments:
#!/bin/bash
shopt -s nullglob
for f in $(pwd)/*.tif; do
comment=$($HOME/macOSGetFinderComment "$f")
new="${f%.tif}.jpg"
echo Setting comment of $new to $comment
$HOME/macOSSetFinderComment "$new" "$comment"
done
and make it executable with:
chmod +x $HOME/CopyComments
and run it with:
cd NEW
$HOME/CopyComments
I have posted this problem also in Apple Community, here is the solution proposed by VikingOSX. It is a big piece of code, so better download it from here or directly from the Apple Community Link mentioned. Here is a description about the solution as described in the original post:
Prompts for a source folder, and a destination folder.
Duplicates folder hierarchy from source to destination folder.
Selects all TIFF images in the folder hierarchy and converts them to JPEG.
For sub-folders and their files, transfers the original Finder comments, color tags and tag name(s) to the destination hierarchy.
The compression level for the JPG file is high, it can be modified for: medium or low in the line: save this_img as JPEG in outfile_name with compression level medium with icon
Limitation: Source folder can only contain one-level of sub-folders. Ignoring this will result in unplanned results.
Additional Comments
Uses a with timeout clause to allow for large number of files. AppleScript does not yet support Finder tag names, so this script uses AppleScript/Objective-C to get and set those tag name(s). Due to this extension, the script now requires AppleScript 2.4 and must be run on OS 10.10 or later.
Due to the AppleScript/Objective-C code, the script cannot be run interactively as a script/script bundle without using the control+command+R keyboard shortcut. A test is made when the script starts, and will warn appropriately. It is best to save the script as an application to avoid this keyboard shortcut altogether.
Usage
Save the script and then copy and paste the file contains into the Script Editor (you can find the application in the folder: Utilities under the name: Script Editor), compile and save the file with the format: Application, then double click on it to run the script application.
I have tested the script under with Mac Air 2010, with OS El Capitan, for a folder with 884 TIFF files with 2.25GB size and it takes about 18 minutes to convert them into JPG files with medium compression level. The generated files will contain the tags and comments from the original equivalent TIFF file.
Disclaimer
Comment and tags generated in one platform for example Windows or mac OS are not visualized in the other platform. Tags created in Windows are treated in mac OS as keywords (Comand+i for visualizing them), but comments generated in Windows are not visualized in mac OS. This is general incompatibility problem that apply for photos in any format (for example TIFF or JPG).
EDIT (updated solution for solving cross-platform problem with comments)
Taking the idea from #MarkSetchell, I adapted the original script to at least solve the cross-platform problem from macOS to Windows, i.e. a comment from macOS can be seen in Windows platform. The idea is to use EXIF metadata. Then the Applescript will invoke the shell script for invoking the exiftool:
set uxFilepath to POSIX path of NewIMG
do shell script "/usr/local/bin/exiftool -overwrite_original -EXIF:UserComment=\"" & cmtstr & "\" " & uxFilepath
Windows processes the UserComment metadata from EXIF as a regular file comment. Now same comment on the TIF file will be on the JPG and also because such comments were copied (copy-paste) into an EXIF metadata the same information will be visualized under Windows. The same idea can be used for other file properties, in case Windows/Mac read it.
The EXIF metadata in macOS can be visualized from command line as suggest #MarkSetchell, but also from Finder: Command+o (to launch preview app), then Command+i (to launch the inspector). Then click on tap: "More Info", then the tab EXIF.
For the opposite process will require an script that does the opposite, i.e., copy EXIF comment using exiftool, into macOS comment. I have verified that in such case the Windows comment will appear under the label: XPComment. The script uses: UserComment, but it works using XPComment as label in both directions.

In Ubuntu, how do I find all photos of the Raw format taken after Jan 1, 2015, and if they total less than 30G, put them on an external drive?

This question has three parts:
How do I find all RAW format photos.
I assume I am going to use the find command. I have used find successfully exactly once:
find . -name creek.jpg
to find creek.jpg in the current folder. How do I search by date? The man page has a list of 'primaries' that allow you to tell find to search by things like date, and it's like reading a foreign language: I can't understand the man page. Also, I assume that find uses the date the photo was uploaded to the system, and not the date in the metadata of the picture itself.
How do I keep a running total of how big these files are?
I have no idea which shell command to use. Should I do it the hacky way and copy the photos into a folder and simply use a shell command to find out how big the folder is? I'm leary of doing that, because I am doing support for a photographer who takes a LOT of raw photos, and I don't want to run her computer out of memory by duplicating raw photos.
I assume putting them on an external drive is as simple as using the cp command.
Thank you for any advice!
You have a bunch of questions to ask and answers to find before you can even get started.
First, you need to know WHERE the files are stored because that is the first parameter for find, e.g.
find $HOME ...
or
find "/mnt/EXTERNAL DRIVE" ...
Second, you need to know the extension of the RAW files the camera creates, it may be .CR2 on a Canon or .TIF or .DNG or .NEF on a Nikon, or .mrw on a Minolta. Then you can get the second parameter for find:
find $HOME -iname "*.NEF"
Good, now you can get a list of the files. But you want to know when they were shot. Unfortunately, Ubuntu isn't going to know that, on its own, though it will know when files were created, so you may find it better to let Ubuntu weed out old files so you don't have to pass them to the next step which will be slower. So, how do we get Ubuntu to only find files newer than 1 Jan 2015 (since files put on disk before then were presumably not shot since January 2015).
Well, one way would be to create a file dated 1 January 2015, and then to tell find to only find newer files. So, let's try that:
# Create file with earliest date we want to find - i.e. 1 Jan 2015
touch -t 201501010000 earliest
Now add an extra parameter to find like this:
find $HOME -iname "*.CR2" -newer earliest
Next, if you want the actual shooting date, you will need to install ImageMagick, or ufraw or dcraw or somesuch to parse your raw files and get the date. Let's say you write a script called CheckIfFileIsNew and make it executable with
chmod +x CheckIfFileIsNew
then you will be able to run that script for all the files:
find $HOME -iname "*.cr2" -exec CheckIfFileIsNew {} \;
Then you need to learn how to grep the date out of a raw file. Then you need to learn how to run stat on a file to find its size. Then you need to learn some awk to total up all the file sizes to see if they exceed 30GB, then you can decide if you want to copy the files to the new place and if you do, you can replace the exec in the original find command with a cp command.
find $HOME -iname "*.cr2" -exec CheckIfFileIsNew {} \; | awk '{total+=$0} END{print $total}'
Your script CXheckIfFileIsNew will look something like:
#!/bin/bash
date=$(identify -verbose "$1" | grep "dng:Timestamp")
# Work out of date is 2015 or 2016
# If date is 2015 or 2016; call `stat` to get the file size, print file size
# Output the size, in bytes, of the filename we were given as parameter
stat -c%s "$1"
Note that the identify command above may be pretty slow if you have a hi-res camera. You could try a command like strings on a couple of your RAW files to see if you can find the date in there that way and it will be miles faster:
strings SomeFile.RAW | grep -i date

Resources