Reputation: 341
I'm very new to mac shell scripting, but I've written this to toggle hide/show hidden files on mac. (Then put on automator application) Is this a good solution?
#!/bin/sh
view=$(defaults read com.apple.finder AppleShowAllFiles)
if [ "$view" = "1" ]
then
defaults write com.apple.finder AppleShowAllFiles -bool false
else
defaults write com.apple.finder AppleShowAllFiles -bool true
fi
killall Finder
Upvotes: 2
Views: 3358
Reputation: 9090
If you want a fast way to show/hide hidden files from Terminal in Mac, add the lines below to your .bash_profile
file in your home directory:
alias hidden-files-show="defaults write com.apple.finder AppleShowAllFiles YES; killall Finder";
alias hidden-files-hide="defaults write com.apple.finder AppleShowAllFiles NO; killall Finder";
Close and open a new Terminal window for the new alias
commands take effect, then you can quickly type "hid"-Tab to auto complete
$ hidden-files-show
$ hidden-files-hide
Upvotes: 2
Reputation: 235
For what it is worth, I have the following in my .bash_profile for doing this, similar to @SwankyLegg
togglehidden() {
STATUS=`defaults read com.apple.finder AppleShowAllFiles`
if [ $STATUS == TRUE ];
then
defaults write com.apple.finder AppleShowAllFiles FALSE
else
defaults write com.apple.finder AppleShowAllFiles TRUE
fi
osascript -e 'tell app "Finder" to quit'
sleep 1
osascript -e 'launch app "Finder"'
}
so I can call it from the Terminal. (NB, if you run it on a machine where AppleShowAllFiles
has never been set, you'll get a complaint the first time you run it,ala:
XXXXXXXXX defaults[2228:124111]
The domain/default pair of (/Users/xxx/Library/Preferences/com.apple.finder, AppleShowAllFiles) does not exist
but everything's gonna be OK. I believe that it lives in the NSGlobalDomain
by default, but this sets it in the user's. )
Upvotes: 1
Reputation: 27633
I'm using a script like this:
do shell script "x=$(defaults read com.apple.finder AppleShowAllFiles)
[ $x = 1 ] && b=false || b=true
defaults write com.apple.finder AppleShowAllFiles -bool $b"
tell application "Finder"
quit
delay 0.1 -- without this there was a "connection is invalid" error
launch -- without this Finder was not made frontmost
activate -- make Finder frontmost
reopen -- open a default window
end tell
I don't know if killall Finder
would be that dangerous either. It sends Finder a TERM signal, which can usually be caught by a process in order to terminate cleanly. Finder doesn't support sudden termination as of 10.8, but if it did, it should be safe to even send it a KILL signal.
Upvotes: 2
Reputation: 473
This question is old, but here's a good solution using your code:
osascript -e 'tell app "Finder" to quit'
It's similar method for closing finder, but is more concise than Paul R's answer. Paul, if you see this and I'm missing any potential issues, please let me know.
Alternatively, you could use:
STATUS=`defaults read com.apple.finder AppleShowAllFiles`
if [ $STATUS == TRUE ];
then
defaults write com.apple.finder AppleShowAllFiles FALSE
else
defaults write com.apple.finder AppleShowAllFiles TRUE
fi
osascript -e 'tell app "Finder" to quit'
Upvotes: 1
Reputation: 213120
Instead of killall Finder
, which is somewhat extreme and dangerous (you may kill the Finder in the middle of file copying or other I/O operations). Instead you could just send an AppleEvent to the Finder to tell it to refresh a given window. E.g. to refresh the frontmost window you can do this in AppleScript:
tell application "Finder"
tell front window
update every item with necessity
end tell
end tell
(from http://hints.macworld.com/article.php?story=2009091413423819)
You can easily adapt this to refresh every open Finder window if that's what you need.
To run AppleScript code such as the above from a bash script you can use the osascript command line tool, e.g.
osascript <<EOF
tell application "Finder"
tell front window
update every item with necessity
end tell
end tell
EOF
Upvotes: 1