How to make a script to show/hide hidden files in Mac OS X?
Asked Answered
E

9

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?

Earnest answered 9/12, 2013 at 11:48 Comment(0)
E
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.

Earnest answered 10/12, 2013 at 12:29 Comment(0)
C
14

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

Curst answered 12/1, 2017 at 19:51 Comment(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.Managing
P
10
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

Pow answered 9/12, 2013 at 12:57 Comment(3)
Thanks for your answer. BTW, why did the command become delete? Will it output the same result? "defaults delete com.apple.finder AppleShowAllFiles"Earnest
defaults write com.apple.finder AppleShowAllFiles -boolean false is not working on 10.9 osxPow
Oh ok. So that's why.Earnest
A
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.

Ansilma answered 10/12, 2013 at 13:0 Comment(0)
T
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.

Tacy answered 11/11, 2016 at 16:52 Comment(0)
Y
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

Yellowweed answered 24/9, 2014 at 21:22 Comment(0)
D
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.

Dayton answered 17/10, 2016 at 17:34 Comment(0)
S
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.

Silicious answered 9/12, 2013 at 15:2 Comment(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)".Silicious
What is the error @DigiMonk? If you tell me the error, I'll adjust the script.Silicious
@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.Silicious
That isn't a problem, @DigiMonkSilicious
E
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.

Earnest answered 10/12, 2013 at 12:29 Comment(0)
D
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
Demosthenes answered 2/9, 2016 at 7:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.