Table of Contents       9. AppleScript


9.7 Power Examples

This chapter contains some extraordinary powerful AppleScript examples you may find very useful.


Use Movie File Name as the EXIF Creation Date
After we had digitized a number of MiniDV video tapes from our old Canon MV30i film camera, using a special Mini-FireWire to Thunderbolt-USB-C connector cable and Apples iMovie software, we ended up with many folders full of separate MOV video files, one for each recorded video snippet from a MiniDV tape.

The name of these video files consists of the date and time of the recording, as the Canon, like most other miniDV digital video cameras, has an internal clock, and had stored the recording data in the video data.

Here is an example of how that looks. Please note that the current EXIF Date column show the data and time of when iMovie had copied the file from the miniDV original tape.

file name list


While that already is useful, it would be so much better if the actual recording date would have been placed in the EXIF Capture Date field, so it could be used for sorting and finding.

With the extensive AppleScript support in NeoFinder, that is actually possible!

This example script looks at all items that are currently selected in NeoFinder.


tell application "NeoFinder"
set myList to selected items
repeat with theItem in myList
set myName to name of theItem
if (myName ends with ".mov") and (length of (myName) > 21) then -- only .mov video files for this example
set myDate to current date -- use that as a base date
set year of myDate to (text 1 thru 4 of myName) as number
set month of myDate to (text 6 thru 7 of myName) as number
set day of myDate to (text 9 thru 10 of myName) as number
set hours of myDate to (text 12 thru 13 of myName) as number
set minutes of myDate to (text 15 thru 16 of myName) as number
set seconds of myDate to (text 18 thru 19 of myName) as number
set exif Capture Date of theItem to myDate -- write back the newly created date into the EXIF section of the movie file
end if
end repeat
end tell


Copy and paste this script into your Script Editor or Script Debugger and give it a try!

This script works through each of the currently selected items in NeoFinder, and for all video files that have ".mov" as the name suffix, and the sufficient file name length, it takes said name, and builds a date from the segments.

Then the script writes that date to the EXIF Capture Date field of the video file. Which will cause NeoFinder to write that date to the EXIF section of your video file.

Let's see how this looks after one file was processed in NeoFinder:

exif date changed


Yep, it did work perfectly. If you check the Inspector, you can see that even the seconds were properly transferred. Nice!

IMPORTANT
This will modify the content of the selected movie files, so:
- You do of course first try this with a single selected video file, so see how it works!
- You do have a full backup of all video files you are running through this script!




Filter XMP Keywords
A client recently told us that she had received a couple of thousand files from a contractor, and each of these files contained a lot of XMP keywords. That was OK, but someone had placed hundreds of these keywords in an hierarchical group named "More Keywords", so the result was confusing, and differed from the regular keywords normally used:

unwanted hGroup


The client asked us if there was a way to get rid of these. We first thought that this would be a lot of manual labor. But no, this can be done with AppleScript!

We wrote this incredibly powerful AppleScript that solves this problem.


tell application "NeoFinder"
set myList to selected items -- get a list of all selected items
repeat with theItem in myList
set originalKeywordString to xmp Keywords of theItem -- one string separated by ", "

-- turn that string into a real list
set AppleScript's text item delimiters to {", "}
set originalKeywordList to (every text item in originalKeywordString) as list
set AppleScript's text item delimiters to ""

-- filter that list
set replacementKeywordList to my filterKeywordList(originalKeywordList, "More Keywords|")

-- turn it back into a string in the proper format
set AppleScript's text item delimiters to {", "}
set newKeywordString to replacementKeywordList as string
set AppleScript's text item delimiters to ""

-- and write it back to the file
set xmp Keywords of theItem to newKeywordString
end repeat
end tell

on filterKeywordList(originalKeywordList, textPrefix)
set replacementKeywordList to {} -- init the result list
set unwantedPrefixLength to (length of textPrefix)
-- go through each keyword in the list
repeat with oneOriginalKeyword in originalKeywordList
-- if the keyword begins with textPrefix, remove that part
if oneOriginalKeyword begins with textPrefix then
set newKeyword to characters (unwantedPrefixLength + 1) thru (length of oneOriginalKeyword) of oneOriginalKeyword as string
set replacementKeywordList to replacementKeywordList & newKeyword
else -- all other keywords are just added to the result list
set replacementKeywordList to replacementKeywordList & oneOriginalKeyword
end if
end repeat
return replacementKeywordList
end filterKeywordList


The script asks NeoFinder for the list of selected items and repeats through that list. It gets the current XMP keywords from each file, filters them, and writes them back to the file. Done.

We have taken the long road here in that sample Script to show how such tasks can be completed. The actual filter process is separated into a procedure, or "handler", and can be given the unwanted keyword prefix as a parameter, so it is easier for you to customize this for your own needs.

As NeoFinder provides the XMP keyword list as one combined string, it is necessary to use the
AppleScript's text item delimiters to separate the keywords for processing.


IMPORTANT
This will modify the content of the selected files, so:
- You do of course first try this with a single selected file, so see how it works!
- You do have a full backup of all files you are running through this script!



Build a Catalog Report in Microsoft Excel
If you wish to create a Microsoft Excel sheet with details about all catalogs in your NeoFinder LIBRARY, you can use this massive AppleScript.

It will first check how many catalogs are contained in your NeoFinder LIBRARY.

Then, it will create a new document in Microsoft Excel, they call them
workbook. The script will set up the columns with the proper width and text formatting, so the values will be shown correctly. The number format property was quite tricky to find, maybe you can use it for other projects as well.

Afterwards, the script walks through each NeoFinder catalog, gets the interesting metadata, like the catalog name, data size, media type, and of course the number of files and folders.

That data is then added to the freshly created Excel workbook.

And here it is:


-- how many catalogs are contained in your NeoFinder LIBRARY?
tell application "NeoFinder"
set numCatalogs to count Catalogues
end tell

tell application "Microsoft Excel"
set myWorkbook to make new workbook
tell myWorkbook
tell sheet 1
set value of cell "A1" to "Name"
set column width of column 1 to 60
set number format of range ("A1:A" & (numCatalogs + 3) as string) to "@" -- plain text

set value of cell "B1" to "Media Type"
set column width of column 2 to 26
set number format of range ("B1:B" & (numCatalogs + 3) as string) to "@"

set value of cell "C1" to "Hardware Serial"
set column width of column 3 to 20
set number format of range ("C1:C" & (numCatalogs + 3) as string) to "@"

set value of cell "D1" to "Number of Files"
set column width of column 4 to 16
set number format of range ("D1:D" & (numCatalogs + 3) as string) to "# ?/?" -- "Fraction"

set value of cell "E1" to "Number of Folders"
set column width of column 5 to 16
set number format of range ("E1:E" & (numCatalogs + 3) as string) to "# ?/?" -- "Fraction"

set value of cell "F1" to "Cataloged Data Size"
set column width of column 6 to 20
set number format of range ("F1:F" & (numCatalogs + 3) as string) to "# ?/?" -- "Fraction"

set value of cell "G1" to "Volume Size"
set column width of column 7 to 20
set number format of range ("G1:G" & (numCatalogs + 3) as string) to "# ?/?" -- "Fraction"
end tell
end tell
end tell


repeat with i from 1 to numCatalogs
-- get the data from NeoFinder
tell application "NeoFinder"
tell Catalogue i
set catName to name
set catMediaType to media type
set catHWSerial to hardware serial
set catNumFiles to file number
set catNumFolders to folder number
set catDataSize to size
set catVolumeSize to volume size
end tell
end tell
-- and place it in the Excel worksheet
set currentRow to (2 + i) as string
tell application "Microsoft Excel"
tell myWorkbook
tell sheet 1
set value of cell ("A" & currentRow) to catName
set value of cell ("B" & currentRow) to catMediaType
set value of cell ("C" & currentRow) to catHWSerial
set value of cell ("D" & currentRow) to catNumFiles
set value of cell ("E" & currentRow) to catNumFolders
set value of cell ("F" & currentRow) to catDataSize
set value of cell ("G" & currentRow) to catVolumeSize
end tell
end tell
end tell
end repeat



A result of running that script may look like this:

applescript_to_excel_sample small

You can then save this spreadsheet like any other Excel documents.




Find and Replace Metadata in all Catalog files
Sometimes it is necessary to find and replace text in metadata fields of all files in a Catalog. For example when your CEO spontaneously decides to change the company name from "
Amazing Inc." to the boring (sic!) but new name "Xyz Inc.", and you must replace the Copyright text in all photos immediately.
This can't simply be done by just overwriting that field in all files with the XMP Editor in the Inspector of NeoFinder, as the Copyright string may be very different for each file, like "Copyright by Amazing Inc. 2021" or "© Amazing Inc. All Rights Reserved" or such.

This AppleScript here walks through all files of a NeoFinder
Catalogue named "TEST", using the Catalog Item property. This list only contains files, no folders!

The Script checks the XMP Copyright field "
xmp Copyright" of each file to see if it contains the old company name.

If that is the case, only the name portion of the copyright string gets replaced with the new company name, and written back.

The replacement is done with a nice string replacement function
findAndReplaceInText found in the Apple archives:
https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ManipulateText.html

To customize this Script to your needs, replace the
originalText and replacementText property of the Script, and the name of the Catalog you wish to use.
Also, if you wish to replace text in other metadata fields, replace the two places of
xmp Copyright with the name of the metadata field you need to edit.


property originalText : "Amazing Inc."
property replacementText : "Xyz Inc."

tell application "NeoFinder"
tell Catalogue "TEST"
set numFiles to (file number)
repeat with i from 1 to numFiles
tell Catalog Item i
set myCopyright to xmp Copyright
if myCopyright contains originalText then
set newCopyright to my findAndReplaceInText(myCopyright, originalText, replacementText)
set xmp Copyright to newCopyright
end if

end tell
end repeat
end tell
end tell

on findAndReplaceInText(theText, theSearchString, theReplacementString)
set AppleScript's text item delimiters to theSearchString
set theTextItems to every text item of theText
set AppleScript's text item delimiters to theReplacementString
set theText to theTextItems as string
set AppleScript's text item delimiters to ""
return theText
end findAndReplaceInText


IMPORTANT
This will modify the content of the files in the catalog, so:
- You do of course first try this with a small catalog, so see how it works!
- You do have a full backup of all files and Catalogs you are running through this script!




9.1 The NeoFinder AppleScript dictionary
9.2 The Scripts menu and folder
9.3 Find by AppleScript
9.4 Change the preferences of NeoFinder
9.5 Cataloging and updating
9.6 Use the Selection