Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automator Change File Extension

I have a number of files that have the incorrect File Extension.


I would like to see if I could use Automator to check the file extension and change to the correct extension.


if I use the file -I command in Terminal I can see the files that have a incorrect extension, I would like to get Automator to look into my folders, subfolders and where it needs to change the extension.


Paul

Posted on Oct 2, 2019 2:06 PM

Reply

Similar questions

55 replies

Oct 21, 2019 2:56 PM in response to Paul1762

The only accurate way to make the script work was to use the free, third-party exiftool and combine that with checking the first byte of the image file, because exiftool lies when it encounters a tiff image with a .CR2 extension.


If you have Xcode (Mac App Store) or preferably the far smaller Apple's Command Line Tools for Xcode 11 (requires free developer account) installed, then the homebrew package manager (also required) will automatically compile and install exiftool in /usr/local/bin. Normally, I do not post code that requires other installation conditions, but this is the sole exception.


To get homebrew package manager installed, you can copy/paste the following into an open Terminal window with a return:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"


Once that is done, you can do the following, also in the Terminal:

brew update
brew upgrade
brew info exiftool
brew install exiftool
brew cleanup # after you have updated exiftool once, this will purge the prevous version


You will need /usr/local/bin in your shell PATH, so either in your ~/.bash_profile, or ~/.zshrc, you would do the following:

export PATH=".:/usr/local/bin:$PATH"
# gag Google analytics tracking
export HOMEBREW_NO_ANALYTICS=1


and for the current Terminal session to put this into effect:

source ~/.bash_profile or source ~/.zshrc


You would run the script from the Terminal with the only argument as the full or tilde (~) path to the parent directory of your images:

# make the script executable
chmod +x ./imgmv.zsh
./imgmv.zsh ~/Desktop/Test3


There are instructions in the script for also generating a report of images to be renamed. The fact that there are 35 lines of comments in the script likely means that the hosting software will not allow me to post the full 112 lines, so I will try to do that in a follow up post as it also counts the lines in this post as well.

Nov 5, 2019 1:25 PM in response to Paul1762

The following files do not exist unless you have already created them in your home directory:

  1. ~/.bash_profile
  2. ~/.zprofile
  3. ~/.zshrc


The tilde (or $HOME) is shorthand for /Users/paul directory location. The first two files are read by the default SHELL login process when the Terminal is first launched. You want to change the default PATH environment variable order so that the exiftool application is found.


If you do not have a plain text editor such as BBEdit or another programmer's editor, then you can use TextEdit in plain text mode (format menu) to create or edit these files. Choose Open, and then press shift+command+. to toggle these hidden files visibility in the TextEdit File chooser. They must be in your home directory.


You simply enter the following in each of these files. The # character is a comment line.


export PATH=".:/usr/local/bin:$PATH"
# gag Google analytics tracking
export HOMEBREW_NO_ANALYTICS=1


To set this information for the current Terminal session:

# if Bash is your default shell
source ~/.bash_profile
# if Zsh is your default shell in Catalina
source ~/.zprofile


Nov 5, 2019 1:49 PM in response to VikingOSX

From Dock : Launchpad : Other, click on the Automator icon. Select your Desktop and new document, choosing Application.


From an Automator application perspective, you need two actions:

  1. Files & Folder Library > Ask for Finder Items
    1. Start at: Desktop
    2. Type: Folders [ ] Allow Multple Selection
  2. Utilities Library > Run Shell Script
    1. Shell: /bin/zsh
    2. Pass input: as arguments
    3. Replace any boilerplate text in the action with the entire copied contents of the downloaded (and unzipped) img5.zsh.zip from my Google Drive account.
  3. Save with an appropriate name to your Desktop. This is now a double-clicked application that will initially prompt you for the starting folder that contains your images and sub-folders of images. This folder path is passed into the Run Shell Script, and the Zsh script runs like a wildman to completion.


I just ran this script again on three sub-folders in a starting folder on my Desktop against a variety of 23 images with a variety of extensions — some correct for the internal image type, and others different. When the script was done, all images had the correct extensions for their internal image type.

Oct 4, 2019 12:19 PM in response to Paul1762

Ok. Here is a table of image type responses from different tools on Mojave, just to make things interesting. The exiftool is not standard on macOS and was installed by homebrew package manager.



Apple is deprecating Python, Ruby, and Perl runtimes on Catalina, and the Fall 2020 release of macOS.next may exclude them altogether. I cannot write a tool now, that depends on users having to install scripting languages on future releases of macOS. So the solution that I come up with here will likely be shell script based, and something that you can use in Automator.

Nov 6, 2019 11:28 AM in response to VikingOSX

Viking,

firstly I must appoligise asking these question... so thanks very much for your help...


I have ensure the files are present and contain the the following:

export PATH=".:/usr/local/bin:$PATH"

# gag Google analytics tracking

export HOMEBREW_NO_ANALYTICS=1


however when I go to run the command lines

source ~/.bash_profile

and

source ~/.zprofile


I get another error:

/Users/Paul/.bash_profile:2: command not found: \ufeff#

/Users/Paul/.bash_profile:3: command not found: \ufeff\ufeffexport


stuck again..


Nov 19, 2019 11:41 AM in response to Paul1762

Sure.


Just add a final Utilities Library : Run AppleScript action to the Automator application with this content:


use scripting additions

on run {input, parameters}
	display alert "Change File Extension" message "Completed" as informational
	return input
end run


From Dock : Launchpad : Other, launch Automator. Cancel the File dialog, and from Automator : File menu : Open Recent -> your Automator Application name. Drag and drop the Run AppleScript action to the end of the workflow, add the content from above, save, and you have your completion alert panel.


This was simpler than attempting to add comparable code to the end of the Zsh script.


See what patience can accomplish. 😎

Oct 2, 2019 2:11 PM in response to Paul1762

What are these wrong extensions, and what should the new extensions become. You realize that changing the extension does not convert the internal document content?


This can be done without the need for Automator, by using just AppleScript, a shell script, or even higher level languages which are more efficient at folder hierarchy recursion.

Oct 3, 2019 2:32 PM in response to Paul1762

Paul,


I can write the script, and I felt that the Zsh shell can provide the directory recursion, and other processing strengths for more efficiency and speed than AppleScript. The latter can certainly recurse down a directory structure, but it slows like molasses the deeper it goes, and the more files that it processes.


This is why I wanted a list of file extensions that need to be searched. I can make the Zsh script prompt you with a normal AppleScript choose folder dialog so you can aim it at the starting folder name.

Oct 4, 2019 7:23 PM in response to Paul1762

Wait for me to write the Zsh script, which I will eventually post here after testing. Then you test it yourself on a test directory of your own where you have valid image files, and some that have the wrong extension for their image type. I am still making progress on the coding this evening, so probably won't be ready for you until Monday. 10/7.

Oct 7, 2019 6:07 AM in response to Paul1762

It took about two yours last evening to configure the test folder hierarchy with a GB of test raw, jpg, and tiff files with different extensions, etc. I also had to write up the test plan so that when the script runs, I can inspect what the given file was originally, and what its extension was altered to be, and if it the script logic correctly understood the image format and changed any wrong extensions correctly.


Initial tests suggest that part of the script is working fine for DNG and RAF, but tiff images renamed as CR2 are being elusive. I may have to write another function to just handle these irregularities, and the net is that I don't believe this solution will be ready today as planned. Right now I am testing on just one sub-folder, and I have three to test before I am satisfied that it is working correctly. I may have to resort to exiftool after all.

Oct 9, 2019 7:11 AM in response to Paul1762

I determined that Image Events in AppleScript is able to discern that a TIFF or JPEG image is just that — regardless of the extension, or the misleading MIME type returned by other tools. So I have a Zsh function that calls AppleScript and returns the proper MIME type (e.g. public.jpeg, public.tiff).


Need to walk through my code again to determine why it is ignoring file renaming. Probably something so simple, it is invisible.





Oct 16, 2019 6:15 AM in response to Paul1762

Still working on this with interrupts due to house sitting, remodeling, and contractor issues. Testing has shown me that this is not as straightforward as originally thought, and that it will require slower AppleScript code (function), and the free, third-party exiftool for reliable image format detection. I have some pesky syntax errors and logic to resolve yet too.

Automator Change File Extension

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.