Within KM get image file H & W, and get video file duration?

Is there a direct and/or easy way, within Keyboard Maestro or using a short script, to get:

  • an image file’s height and width in pixels?
  • a video file’s duration?

I use Name Mangler to do this (I am putting the numbers in the file name), but I am trying to simplify/compress/future-proof some workflows by condensing a workflow that might go “KMacro➞NameMangler multi-step action➞KMacro” to a single continuous Keyboard Maestro macro.

I have used a simple script (kindly provided by @peternlewis in this thread) to get video duration.

Somewhere in the KM forum is a way to get image dimension from a file that included copying the file to a clipboard, but I’d like to avoid that because I handle tens of thousands of images, a few of which are several GBs in size.

I have come across the “sips” Terminal command, but I don’t know how to use it, nor how to get the files to Terminal and the results back to Keyboard Maestro.

The three data I want to get are readily displayed in Path Finder as “Spotlight Metadata”. Can I get that from Keyboard Maestro? It seems to be direct and likely quick.

Hey Kirby,

Select an image file in the Finder, and run this script to see the height and width.

----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2018/03/28 23:08
# dMod: 2018/03/28 23:08 
# Appl: Finder, the shell
# Task: Extract the pixel-height and pixel-width of the selected image in the Finder.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @Extract, @Pixel, @Height, @Width, @Selected, @Image, @Finder
----------------------------------------------------------------

tell application "Finder"
   set finderSelectionList to selection as alias list
   if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
   set theItem to item 1 of finderSelectionList
end tell

set itemPath to quoted form of (POSIX path of theItem)
set shCMD to "mdls -name kMDItemPixelHeight -name kMDItemPixelWidth " & itemPath
set imageDimensions to do shell script shCMD

----------------------------------------------------------------

To see all of the available meta-data for a file do this:

Type mdls<space> into the Terminal.app.

Drag and drop an file to the Terminal (which will emplace the file path).

Hit <Return>.

You’ll see all the tag names and values available for that particular file type (or folder).

The tag name you’re looking for for the vid duration is kMDItemDurationSeconds.

-Chris

1 Like
sips -g pixelHeight -g pixelWidth /Users/me/myfile.jpg

will produce:

/Users/me/myfile.jpg
  pixelHeight: 3456
  pixelWidth: 5184
1 Like

Fun^2 :blush: . Thank you both.

I ended up using mdls because it seemed easier, was the first I tried, and worked.

I "discovered" that the "Execute Shell Script" Action can be used to run a Terminal command. I did not know this.

Here is how I did it. Comments, improvements welcome.

  • The "FilePath" variable is set previously in the macro.
  • I'm a little worried about the regex used to extract the data I want for the variables. They work, but assume, afaict, line endings. It seems unwise to make that assumption.
  • The format for the duration, "MM'ss" is what I use in file names (it avoids the problematic colon character)
  • mdls reports the correct H & W for video files.
    (Edit: Please see post below for downloadable macro file.)

Hey Kirby,

Just to be clear the Execute a Shell Script action ONLY runs “Terminal” commands – e.g. items that can run from a Unix command shell.

:sunglasses:

-Chris

1 Like

Here is the Group of Actions saved as a macro (as requested via PM; I should have posted it this way in the first place):

Keyboard Maestro 8.2 “Get Duration & W & H of video file to variables” Macro

Get Duration & W & H of video file to variables.kmmacros (9.4 KB)

1 Like

Hey Kirby,

Thanks.

Just for fun I did this with AppleScript and the Satimage.osax (AppleScript Extension).

Select a movie file in the Finder, and run the script from the Script Editor.app.

-Chris

----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2018/03/30 21:52
# dMod: 2018/03/30 22:07
# Appl: Satimage.osax, AppleScript
# Task: Extract the duration  in minutes'seconds, width, and height from a movie file for use in a file name.
# Libs: None
# Osax: Satimage.osax
# Tags: @ccstone, @Applescript, @Script, @MDLS, @Finder, @Extract, @Duration, @Movie, @File, @Minutes, @Seconds, @Width, @Height
----------------------------------------------------------------
# REQUIRES!!!! the Satimage.osax
----------------------------------------------------------------

tell application "Finder"
   set finderSelectionList to selection as alias list
   if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
   set theItem to item 1 of finderSelectionList
end tell

set itemPath to quoted form of (POSIX path of theItem)

set shCMD to "mdls -name kMDItemDurationSeconds -name kMDItemPixelHeight -name kMDItemPixelWidth" & space & itemPath
set mdlsResults to do shell script shCMD

set valueList to find text "[\\d.]+" in mdlsResults with regexp, all occurrences and string result

set {durationSeconds, pixelHeight, pixelWidth} to valueList
set dMinutes to format (durationSeconds div 60) into "00"
set dSeconds to format (round (durationSeconds - (dMinutes * 60))) into "00"
set dFormatted to (dMinutes as text) & "'" & dSeconds as text

set textOutput to linefeed & ¬
   "Duration:" & tab & dFormatted & linefeed & ¬
   "Width:" & tab & tab & pixelWidth & linefeed & ¬
   "Height:" & tab & tab & pixelHeight & linefeed

----------------------------------------------------------------

You could also have a look at Noodlesofts’s Hazel (http://www.noodlesoft.com/). It is basically a workflow tool for files and folders and does exactly what you want. Rename (or tag, move, copy, delete, sort, sync, etc.) files based on their (or their folders) attributes.

It is one of my favorite tools after KM & BTT (all 3 work together) and will make your life easier. E.g. I use KM to tag, download, manipulate files and then Hazel sorts all that stuff afterwards.

Sorry for the pitch. Not affiliated in any way. I just love it almost as much as KM.

Julian

2 Likes

Thanks Kirby for sharing, saves me a lot of time today.

1 Like