我是mac shell脚本的新手,但是我已经写过这个来切换隐藏/显示mac上的隐藏文件。 (然后戴上automator应用程序)这是一个很好的解决方案吗?
#!/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
答案 0 :(得分:2)
我正在使用这样的脚本:
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
我不知道killall Finder
是否也会那么危险。它向Finder发送一个TERM信号,该信号通常可以被一个进程捕获,以便干净地终止。截至10.8,Finder不支持突然终止,但如果确实如此,即使发送KILL信号也应该是安全的。
答案 1 :(得分:2)
如果您想快速显示/隐藏Mac中终端的隐藏文件,请将以下行添加到您主目录中的.bash_profile
文件中:
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";
关闭并打开一个新的终端窗口,使新的alias
命令生效,然后您可以快速输入“hid”-Tab自动完成
$ hidden-files-show
$ hidden-files-hide
答案 2 :(得分:1)
而不是killall Finder
,这有点极端和危险(你可能会在文件复制或其他I / O操作中杀死Finder)。相反,您可以将AppleEvent发送到Finder,告诉它刷新给定的窗口。例如。要刷新最前面的窗口,您可以在AppleScript中执行此操作:
tell application "Finder"
tell front window
update every item with necessity
end tell
end tell
(来自http://hints.macworld.com/article.php?story=2009091413423819)
如果您需要,可以轻松调整此选项以刷新每个打开的Finder窗口。
要从bash脚本运行上述AppleScript代码,您可以使用osascript命令行工具,例如
osascript <<EOF
tell application "Finder"
tell front window
update every item with necessity
end tell
end tell
EOF
答案 3 :(得分:1)
这个问题已经过时了,但使用您的代码是一个很好的解决方案:
osascript -e 'tell app "Finder" to quit'
这是关闭查找器的类似方法,但比Paul R的答案更简洁。保罗,如果你看到了这个,我错过了任何潜在的问题,请让我知道。
或者,您可以使用:
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'
答案 4 :(得分:1)
对于它的价值,我在.bash_profile中有以下内容用于执行此操作,类似于@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"'
}
所以我可以从终端呼叫它。 (注意,如果您在从未设置过AppleShowAllFiles
的计算机上运行它,那么您第一次运行它时会收到投诉,ala:
XXXXXXXXX defaults[2228:124111]
The domain/default pair of (/Users/xxx/Library/Preferences/com.apple.finder, AppleShowAllFiles) does not exist
但是一切都会好起来的。我相信它默认存在于NSGlobalDomain
,但这会将其设置在用户的手中。 )