7

I just got my MBP with Retina and i'm really new to the Mac OS X (using PC before). I noticed that the Mac doesn't have a GUI to show/hide hidden files like Windows. I've researched and saw this site Show Hidden Files on your Mac. And yes, it works.

To show hidden files: (Using Terminal) defaults write com.apple.finder AppleShowAllFiles TRUE killall Finder

To hide hidden files: defaults write com.apple.finder AppleShowAllFiles FALSE killall Finder

What i wanted to do is to make an executable script that will perform the above commands when i double-click it so that i don't have to type commands in Terminal in order for me to show/hide hidden files. I saw Applescript but i'm not very familiar with it. I don't know the commands to perform what i want. But i've read some.

Can someone please help me make an executable script that will show/hide hidden files in my Mac?

0

9 Answers 9

14

You don't need script anymore. Open the finder, press + ⇧ Shift + . and it will show/hide your files.

1
  • It should be noted that this method only persists until the next time Finder starts fresh (i.e. from rebooting, signing out, etc.) and will revert back to whatever the default setting is.
    – shbooms
    Dec 31, 2019 at 15:52
9
display dialog "Show all files" buttons {"TRUE", "FALSE"}
set result to button returned of result
if result is equal to "TRUE" then
    do shell script "defaults write com.apple.finder AppleShowAllFiles -boolean true"
else
    do shell script "defaults delete com.apple.finder AppleShowAllFiles"
end if
do shell script "killall Finder"  

enter image description here

Use AppleScript editor and save as application.

enter image description here

3
  • Thanks for your answer. BTW, why did the command become delete? Will it output the same result? "defaults delete com.apple.finder AppleShowAllFiles"
    – daremigio
    Dec 10, 2013 at 11:49
  • defaults write com.apple.finder AppleShowAllFiles -boolean false is not working on 10.9 osx Dec 10, 2013 at 12:28
  • Oh ok. So that's why.
    – daremigio
    Dec 10, 2013 at 12:31
3

You could also use Automator to create a service like this:

do shell script "[[ $(defaults read com.apple.finder AppleShowAllFiles) = 1 ]] && b=false || b=true
defaults write com.apple.finder AppleShowAllFiles -bool $b"
tell application "Finder"
    quit
    delay 0.2 -- without this delay there was a "connection is invalid" error
    reopen -- open a new default window
    activate -- make Finder frontmost
end tell

You can give the service a keyboard shortcut from the Keyboard preference pane.

0
2

I prefer to stick to shell-scripts throughout. Again raised on the answer of @user495470 , the script to toggle hidden files and restart the finder is:

[[ $(defaults read com.apple.finder AppleShowAllFiles) = ON ]] && b=OFF || b=ON
defaults write com.apple.finder AppleShowAllFiles $b
killall Finder

You could store all your scripts in ~/bin. If you save the above code to a ~/bin/toggle_files.command, this makes your script an executable:

chmod +x ~/bin/togglefiles.command

That script is now "clickable". My mom prefers to click her shell scripts, and she stores them in ~/Documents/Scripts instead. I guess it's a matter of taste. In order to evoke your script from the terminal (as I prefer to do) you should create an alias in your profile. I use bash (which I think is the default in OS X) and your bash profile would be at ~/.bash_profile. If you add

alias togglefiles="~/bin/togglefiles.command"

to ~/.bash_profile, all your terminal sessions will treat the command togglefiles as a call to your shell script positioned in your bin-directory.

1

I made simple MAC OS X application to show/hide system files. It is a status bar application. Very lightweight.

Show/Hide System Files on MAC OS X

You can download from here: http://ShowHiddenFilesOnMACOSX.blogspot.com

1

Based on @user495470's answer...

In macOS Sierra (and likely before), you can throw this into the Run Apple Script box in automator instead:

do shell script "[[ $(defaults read com.apple.finder AppleShowAllFiles) = ON ]] && b=OFF || b=ON
defaults write com.apple.finder AppleShowAllFiles $b
killall Finder"

Line breaks matter (or else I would have just commented above).

killall Finder replaces the entire tell block as it will restart on its own when it is ready.

0

Here's a script to toggle the visibility of all invisible files.

set myShell to "defaults read com.apple.Finder AppleShowAllFiles"
set myVisible to (do shell script myShell)
if myVisible = "0" then
    set myShell to "defaults write com.apple.Finder AppleShowAllFiles 1"
else
    set myShell to "defaults write com.apple.Finder AppleShowAllFiles 0"
end if
set myResult to (do shell script myShell)
tell application "Finder"
    quit
    delay 2
end tell
activate application "Finder"
return myVisible

I don't remember why I wrote it the way I did, e.g. why I added the return command at the end. I do know that it works on all versions of OSX since 10.6.8. The difference with the other answer by Parag Bafna is that you're not asked whether to show or hide the files. If the files are hidden, they're shown and if they're visible, they're hidden.

4
  • @DigiMonk I'm really surprised that it matters. The article doesn't explain why. Perhaps this is important only if you have formatted your hard disk as "Mac OS Extended (Case Sensitive)".
    – Mark
    Dec 9, 2013 at 19:13
  • What is the error @DigiMonk? If you tell me the error, I'll adjust the script.
    – Mark
    Dec 9, 2013 at 21:28
  • @DigiMonk I tried this in other versions of OSX and it wasn't a problem, even if the shell returned an error. Have you tried this in OSX 10.9? Can you paste the text of the error message here? It simply doesn't do anything the first time you run the script. If you switch the 0's and 1's in the script, it'll work.
    – Mark
    Dec 9, 2013 at 22:34
  • That isn't a problem, @DigiMonk
    – Mark
    Dec 10, 2013 at 0:11
0

I saw a link which provides options to Show/Hide hidden files.

http://appducate.com/2013/01/how-to-show-hidden-files-folders-on-your-mac/

I used the Automator Script which you can download from the link that i gave.

To make things more efficient for me to Show/Hide hidden files, I saved the script as an Application using Automator.

And then created a keyboard shortcut for the "Show-Hide Hidden Files" application that I created using the steps found here:

https://superuser.com/questions/245711/starting-application-with-custom-keyboard-shortcut

And now i just have to press the keyboard shortcut if i want to show or hide hidden files. I can do this in any application. :)

BTW, thank you all for your answers. This is just another way to show/hide hidden files efficiently.

0

To show or hide files in Mac OSX ElCaptain this is a simple script that uses alias. Create an alias and bind it on a function.So:

#!/bin/bash

#create an alias with function 

a="s" 
b="h" 

function showHideFiles(){
if [[ "$1" = "$a" ]];       #show hidden files
then
  defaults write com.apple.finder AppleShowAllFiles true; killall Finder
elif [[ "$1" = "$b" ]];     #hide hidden files
then      
  defaults write com.apple.finder AppleShowAllFiles false; killall Finder
fi
}

alias files=showHideFiles

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.