Mac shell 脚本隐藏/显示隐藏文件
我对 mac shell 脚本非常陌生,但我写这个是为了在 mac 上切换隐藏/显示隐藏文件。 (然后穿上自动化应用程序)这是一个好的解决方案吗?
#!/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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我正在使用这样的脚本:
我不知道
killall Finder
是否会那么危险。它向 Finder 发送一个 TERM 信号,该信号通常可以被进程捕获以便干净地终止。从 10.8 开始,Finder 不支持突然终止,但如果支持,甚至向其发送 KILL 信号也应该是安全的。I'm using a script like this:
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.如果您想在 Mac 中的终端中快速显示/隐藏隐藏文件,请将以下行添加到主目录中的
.bash_profile
文件中:关闭并打开新的终端窗口以获取新的
alias
命令生效,然后你可以快速输入“hid”-Tab来自动补全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:Close and open a new Terminal window for the new
alias
commands take effect, then you can quickly type "hid"-Tab to auto complete而不是
killall Finder
,这有点极端和危险(你可能会在文件复制或其他 I/O 操作过程中杀死 Finder)。相反,您可以只向 Finder 发送一个 AppleEvent 来告诉它刷新给定的窗口。例如,要刷新最前面的窗口,您可以在 AppleScript 中执行此操作:(来自 http://hints。 macworld.com/article.php?story=2009091413423819)
您可以轻松地调整它以刷新每个打开的 Finder 窗口(如果是)你需要什么。
要从 bash 脚本运行上述 AppleScript 代码,您可以使用 osascript 命令行工具,例如
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:(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.
这个问题很旧,但这里有一个使用您的代码的好解决方案:
它与关闭查找器的方法类似,但比 Paul R 的回答。 Paul,如果您看到此内容并且我遗漏了任何潜在问题,请告诉我。
或者,您可以使用:
This question is old, but here's a good solution using your code:
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:
对于它的价值,我在 .bash_profile 中有以下内容来执行此操作,类似于@SwankyLegg,
因此我可以从终端调用它。 (注意,如果你在从未设置过 AppleShowAllFiles 的机器上运行它,那么你第一次运行它时就会收到投诉,唉:
但一切都会好起来的。我相信它还活着默认情况下位于
NSGlobalDomain
中,但这将其设置在用户的 ) 中。For what it is worth, I have the following in my .bash_profile for doing this, similar to @SwankyLegg
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:but everything's gonna be OK. I believe that it lives in the
NSGlobalDomain
by default, but this sets it in the user's. )