You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

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

Hiding file extensions in Automator

I download a lot of PDF's and rename them with pertinent details about the file. Most will automatically remove the file extension when I rename them, but some do not and I usually remove it manually so that the exceptions are less distracting in column view. I searched and found a great automator quick action by VikingOSX that will do the hiding of the file extensions, however what I'm finding is that ones which need this done on also need to have their permissions changed from 'read only' to 'read and write' before the quick action will work. Is there a way to add the required permission change to this workflow so that it can be a one step operation to accomplish the desired action?


Thanks


Original Thread - How to batch hide extensions using Automa… - Apple Community


iMac 27″, macOS 10.13

Posted on Feb 12, 2022 7:56 PM

Reply
Question marked as Top-ranking reply

Posted on Feb 14, 2022 7:39 AM

Let's start over.


I have revised and tested an Automator solution on macOS Mojave 10.14.6 that will hide the extension of all files in the hierarchy of a selected directory in the Finder, or individually selected (⌘) files instead. I have added an initial dialog that with appropriate button selection, will then allow the Quick Action to hide or unhide the file extensions in the selection.



Just press return to enable Hide processing.


Build the Quick Action


  1. Launch /Applications/Automator.app. In its dialog, select New Document, the Quick Action category, and then click the Choose button.
  2. At the top of the empty Quick Action, set the following:
    1. Workflow receives current [ files or folders ↕︎ ] in [ Finder ]
    2. Change nothing else in this Quick Action header
  3. On the left are the action libraries.
    1. Select Utilities, locate Run AppleScript, and then click and drag that action onto the large workflow window.
    2. Remove all boilerplate content in that Run AppleScript action.
    3. Replace the content of the now-empty Run AppleScript with the code below via copy/paste.
    4. Once the paste is complete, click the hammer icon in that Run AppleScript to syntax check and compile that AppleScript.
  4. Save the Quick Action and give it a meaningful name (.e.g. Hidden Extensions).
  5. Quit Automator
  6. Right-click on a Folder, or selected files, and from the Finder's secondary menu, choose Quick Actions > Hidden Extensions.
  7. The Quick Action will run, produce the initial dialog, change the files, and quit.


Code (to go into the Run AppleScript action)


use scripting additions
property hidden_status : true as boolean

on run {input, parameters}
	
	-- allow user to choose whether to hide or unhide file extensions
	try
		set ret to button returned of (display alert "Hide or Unhide File extensions?" buttons {"Cancel", "Unhide", "Hide"} default button "Hide" cancel button "Cancel" as informational)
		if ret is "Unhide" then set hidden_status to false
	on error number -128
		quit
	end try
	
	-- recursively hide file extensions in a selected folder, or on 
	-- individually selected files in the Finder
	tell application "Finder"
		repeat with anItem in input
			if kind of anItem is "Folder" then
				set theFiles to (every file in entire contents of folder anItem)
				repeat with afile in theFiles
					my hide_extension(afile, hidden_status)
				end repeat
			else
				my hide_extension(anItem, hidden_status)
			end if
		end repeat
	end tell
	return
end run

on hide_extension(sfile, abool)
	tell application "Finder"
		set owner privileges of sfile to read write
		set extension hidden of sfile to abool
		set owner privileges of sfile to read only
	end tell
	return
end hide_extension

on quit
	display alert "User Canceled and quitting…"
	continue quit
	# Automator will throw a dialog about encountering an error. Ignore it.
end quit


Here is what the finished Quick Action will look like in Automator:


12 replies
Question marked as Top-ranking reply

Feb 14, 2022 7:39 AM in response to conrad93

Let's start over.


I have revised and tested an Automator solution on macOS Mojave 10.14.6 that will hide the extension of all files in the hierarchy of a selected directory in the Finder, or individually selected (⌘) files instead. I have added an initial dialog that with appropriate button selection, will then allow the Quick Action to hide or unhide the file extensions in the selection.



Just press return to enable Hide processing.


Build the Quick Action


  1. Launch /Applications/Automator.app. In its dialog, select New Document, the Quick Action category, and then click the Choose button.
  2. At the top of the empty Quick Action, set the following:
    1. Workflow receives current [ files or folders ↕︎ ] in [ Finder ]
    2. Change nothing else in this Quick Action header
  3. On the left are the action libraries.
    1. Select Utilities, locate Run AppleScript, and then click and drag that action onto the large workflow window.
    2. Remove all boilerplate content in that Run AppleScript action.
    3. Replace the content of the now-empty Run AppleScript with the code below via copy/paste.
    4. Once the paste is complete, click the hammer icon in that Run AppleScript to syntax check and compile that AppleScript.
  4. Save the Quick Action and give it a meaningful name (.e.g. Hidden Extensions).
  5. Quit Automator
  6. Right-click on a Folder, or selected files, and from the Finder's secondary menu, choose Quick Actions > Hidden Extensions.
  7. The Quick Action will run, produce the initial dialog, change the files, and quit.


Code (to go into the Run AppleScript action)


use scripting additions
property hidden_status : true as boolean

on run {input, parameters}
	
	-- allow user to choose whether to hide or unhide file extensions
	try
		set ret to button returned of (display alert "Hide or Unhide File extensions?" buttons {"Cancel", "Unhide", "Hide"} default button "Hide" cancel button "Cancel" as informational)
		if ret is "Unhide" then set hidden_status to false
	on error number -128
		quit
	end try
	
	-- recursively hide file extensions in a selected folder, or on 
	-- individually selected files in the Finder
	tell application "Finder"
		repeat with anItem in input
			if kind of anItem is "Folder" then
				set theFiles to (every file in entire contents of folder anItem)
				repeat with afile in theFiles
					my hide_extension(afile, hidden_status)
				end repeat
			else
				my hide_extension(anItem, hidden_status)
			end if
		end repeat
	end tell
	return
end run

on hide_extension(sfile, abool)
	tell application "Finder"
		set owner privileges of sfile to read write
		set extension hidden of sfile to abool
		set owner privileges of sfile to read only
	end tell
	return
end hide_extension

on quit
	display alert "User Canceled and quitting…"
	continue quit
	# Automator will throw a dialog about encountering an error. Ignore it.
end quit


Here is what the finished Quick Action will look like in Automator:


Feb 13, 2022 6:11 AM in response to VikingOSX

Thanks for responding! I am only trying to hide them as you say. And the quick action you wrote works great, it just hides it. What I'm saying is the quick action doesn't work unless I first manually select the file, "Get Info", and change the permissions from 'read only' to 'read and write'. If I do that first, THEN run the automation, it hides, not deletes, the extension perfectly. So what I'm wondering is if there is a way to build in this permission changing process to the automation?

Feb 13, 2022 9:37 AM in response to conrad93

Here is the replacement script for the Automator Quick Action's Run Shell Script. It will change the file to read/write before hiding the extension, and then setting the owner permission back to read only. If you don't want that last bit, you can place a comment # before that line, or remove it entirely. I also changed the shell in the script to Zsh.


Tested: macOS Monterey 12.2.1.


#!/bin/zsh

# make absolute path to folder
STARTDIR="${1:a:s/\~\//}"

function hide_ext () {
    # usage: $1 is folder
    #        $2 is true to hide extensions, or false to unhide them
    /usr/bin/osascript <<AppleScript
        use scripting additions

        set aFolder to (POSIX file "${1}") as alias
        tell application "Finder"
            set theFiles to (every file in entire contents of folder aFolder)
            repeat with anItem in theFiles
                set owner privileges of anItem to read write
                set extension hidden of anItem to ${2}
                set owner privileges of anItem to read only
            end repeat
        end tell
        return
AppleScript
    return
}

hide_ext "${STARTDIR}" "${2}"
exit 0





Feb 14, 2022 8:14 AM in response to conrad93

You are welcome. Something I also thought about after I posted this morning…


Ideally, the code should save the current owner privileges set on the file to a variable. If that result turns out to already be read write, then just hide (or unhide) the extension. If it is not read write, then change the owner privileges to read write and afterward, set the original owner permissions back on the file.


I now have the code to do this if you need it, and it can be integrated into the existing AppleScript.

Feb 14, 2022 8:38 AM in response to VikingOSX

Interesting you should mention that. I just used in on some files that already had their permissions set to read/write, and it toggled them to read only. When I open them with preview, it says they are locked and I need to deselect the locked checkbox in order to make changes to the name of the file. Ideally it would always set the permissions to read/write and leave them there, regardless of what the permission was set to at the outset, rather than just toggle the setting to the opposite of what it was at the beginning.


That said, it's no big deal because typically I only need to run this process on files that are already set to read only. As background, my homemade, and likely 'bad practice' practice, is to use preview to rename docs with gibberish titles as they come in, using identifying data from the doc that I can see while it's open. For example, if I download an electric bill that's named 12kleudg673ksuyds5tvg85f5.pdf, I'll open it with preview, look at the bill, and rename it something like "Electric 1/12/22 2/1/22 $105" which tells me the posting date, due date, and amount without opening it again, and also enables it to be automatically sorted chronologically when I place it in a folder of bills from that same company. When doing this renaming, the .pdf is deleted, but Preview knows not to actually delete it, instead it just hides it. If, after renaming, the .pdf is still there, I know that the permissions on that doc must be read only, in which case I will now zap it with the quick action.

Hiding file extensions in Automator

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