MacOS – An automator service that opens a file with the same name but different extension of the selected item

applescriptautomatorfindermacos

My movie collection has this setup:

  • a folder "Movies/Covers" with the images of the covers (jpg files)
  • a folder "Movies/Videos" with the corresponding video files (mostly mp4 files)

The files in both folders have the same base name.

I would like to be able to right click an image in the covers folder, select the service "open video" and open the corresponding video file in the videos folder with the VLC player.

I'm very new to Automator and initially I tried to do this for files in the same folder with this workflow:

But it doesn't work because the file renamed from xxx.jpg to xxx.mp4 opened by VLC is still an image.

Anyone could help me?

PS: In the past I used to add the cover to the video files as icons and choose the movie in the finder grid view, but this has led to problems after updating the operating system on several occasions.

Best Answer

You don't want to rename the file, just use a new folder path in order to look up a matching file. Automator doesn't include default actions for stuff like that, so you are looking at using a third party action or a script, or doing that part yourself.

For an AppleScript solution, create a new service/quick action workflow that receives files or folders in the Finder, and add a Run AppleScript action, replacing the default script with the following:

on run {input, parameters} -- match name of an input item to a videoFolder item
    set videoFolder to ((path to home folder) as text) & "Movies:Videos:" -- the HFS folder path containing matching video files
    repeat with anItem in the input -- return the first match
        set {theName, theExtension} to getNamePieces from anItem
        tell application "Finder"
            set matches to (files of folder videoFolder whose name starts with theName) -- match any extension
            if matches is not {} then return (first item of matches) as alias
        end tell
    end repeat
    return missing value -- no match
end run

to getNamePieces from someItem -- get name and extension from a file item
    tell application "System Events" to tell disk item (someItem as text)
        set {theName, theExtension} to {name, name extension}
    end tell
    if theExtension is not "" then
        set theName to text 1 thru -((count theExtension) + 2) of theName -- the name part
        set theExtension to "." & theExtension
    end if
    return {theName, theExtension}
end getNamePieces

...then finish up by adding an Open Finder Items action, and save the workflow.