如何制作一个脚本来显示/隐藏Mac OS X中的隐藏文件?

时间:2022-08-10 16:49:24

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.

我刚刚得到了视网膜的MBP,我对Mac OS X(之前使用PC)非常熟悉。我注意到Mac没有GUI来显示/隐藏像Windows这样的隐藏文件。我研究并看到这个网站显示了你Mac上隐藏的文件。

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

要显示隐藏文件:(使用终端)默认写入com.apple。查找器AppleShowAllFiles TRUE killall查找器

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

隐藏隐藏文件:默认值写入com.apple。查找器AppleShowAllFiles FALSE killall查找器

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?

谁能帮我做一个可执行脚本,在我的Mac中显示/隐藏隐藏文件?

9 个解决方案

#1


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"  

如何制作一个脚本来显示/隐藏Mac OS X中的隐藏文件?

Use AppleScript editor and save as application.

使用AppleScript编辑器并将其保存为应用程序。

如何制作一个脚本来显示/隐藏Mac OS X中的隐藏文件?

#2


8  

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

你不再需要脚本了。打开仪,按⌘+⇧Shift +。它会显示/隐藏你的文件。

#3


3  

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

您也可以使用Automator来创建这样的服务:

如何制作一个脚本来显示/隐藏Mac OS X中的隐藏文件?

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.

您可以从键盘首选项窗格中为该服务提供键盘快捷方式。

#4


1  

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

我制作了一个简单的MAC OS X应用程序来显示/隐藏系统文件。它是一个状态栏应用程序。非常轻量级的。

如何制作一个脚本来显示/隐藏Mac OS X中的隐藏文件?

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

您可以从这里下载:http://showhiddenfilesonmacos.blogspot.com

#5


1  

Based on @user495470's answer...

基于@user495470的回答…

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

在macOS Sierra(很可能是在之前),您可以将它放到automator的运行苹果脚本框中:

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.

killall查找器替换了整个tell块,当它准备好时将自动重新启动。

#6


1  

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:

我更喜欢始终坚持shell脚本。在@user495470的答案上再次提出,切换隐藏文件并重新启动查找程序的脚本是:

[[ $(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:

您可以将所有脚本存储在~/bin中。如果您将上述代码保存到~/bin/toggle_files.command,这将使您的脚本成为可执行的:

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

这个脚本现在是“可点击的”。我妈妈更喜欢点击她的shell脚本,而她将它们存储在~/文档/脚本中。我想这是品味的问题。为了从终端调用您的脚本(我喜欢这样做),您应该在配置文件中创建一个别名。我使用bash(我认为它是OS X中的默认值),您的bash配置文件将在~/.bash_profile中。如果你加入

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.

~ /。bash_profile,您的所有终端会话将把命令togglefiles视为对您的shell脚本的调用,该脚本位于您的二进制目录中。

#7


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.

我不记得为什么我这样写,例如为什么我在结尾添加了return命令。我知道它可以在所有版本的OSX上运行,从10.6.8开始。Parag Bafna给出的另一个答案的不同之处在于,您不会被要求显示或隐藏文件。如果文件是隐藏的,它们就会被显示,如果它们是可见的,它们就会被隐藏。

#8


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/

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.

为了更有效地显示/隐藏隐藏隐藏文件,我使用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

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.

顺便说一句,谢谢大家的回答。这只是另一种有效显示/隐藏隐藏隐藏文件的方法。

#9


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:

要在Mac OSX ElCaptain中显示或隐藏文件,这是一个使用别名的简单脚本。创建一个别名并将其绑定到函数上。

#!/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

#1


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"  

如何制作一个脚本来显示/隐藏Mac OS X中的隐藏文件?

Use AppleScript editor and save as application.

使用AppleScript编辑器并将其保存为应用程序。

如何制作一个脚本来显示/隐藏Mac OS X中的隐藏文件?

#2


8  

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

你不再需要脚本了。打开仪,按⌘+⇧Shift +。它会显示/隐藏你的文件。

#3


3  

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

您也可以使用Automator来创建这样的服务:

如何制作一个脚本来显示/隐藏Mac OS X中的隐藏文件?

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.

您可以从键盘首选项窗格中为该服务提供键盘快捷方式。

#4


1  

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

我制作了一个简单的MAC OS X应用程序来显示/隐藏系统文件。它是一个状态栏应用程序。非常轻量级的。

如何制作一个脚本来显示/隐藏Mac OS X中的隐藏文件?

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

您可以从这里下载:http://showhiddenfilesonmacos.blogspot.com

#5


1  

Based on @user495470's answer...

基于@user495470的回答…

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

在macOS Sierra(很可能是在之前),您可以将它放到automator的运行苹果脚本框中:

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.

killall查找器替换了整个tell块,当它准备好时将自动重新启动。

#6


1  

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:

我更喜欢始终坚持shell脚本。在@user495470的答案上再次提出,切换隐藏文件并重新启动查找程序的脚本是:

[[ $(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:

您可以将所有脚本存储在~/bin中。如果您将上述代码保存到~/bin/toggle_files.command,这将使您的脚本成为可执行的:

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

这个脚本现在是“可点击的”。我妈妈更喜欢点击她的shell脚本,而她将它们存储在~/文档/脚本中。我想这是品味的问题。为了从终端调用您的脚本(我喜欢这样做),您应该在配置文件中创建一个别名。我使用bash(我认为它是OS X中的默认值),您的bash配置文件将在~/.bash_profile中。如果你加入

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.

~ /。bash_profile,您的所有终端会话将把命令togglefiles视为对您的shell脚本的调用,该脚本位于您的二进制目录中。

#7


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.

我不记得为什么我这样写,例如为什么我在结尾添加了return命令。我知道它可以在所有版本的OSX上运行,从10.6.8开始。Parag Bafna给出的另一个答案的不同之处在于,您不会被要求显示或隐藏文件。如果文件是隐藏的,它们就会被显示,如果它们是可见的,它们就会被隐藏。

#8


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/

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.

为了更有效地显示/隐藏隐藏隐藏文件,我使用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

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.

顺便说一句,谢谢大家的回答。这只是另一种有效显示/隐藏隐藏隐藏文件的方法。

#9


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:

要在Mac OSX ElCaptain中显示或隐藏文件,这是一个使用别名的简单脚本。创建一个别名并将其绑定到函数上。

#!/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