0

I am trying to create a service in automator that helps me make a file/folder hidden or unhidden.

I am a very new beginner and do not know anything about automator and I have only tried putting different services together but I don't even know how they work.

I have tried the following and it is not working:

Get Selected Finder Items

Run Shell Script:

STATUS = chflags

for f in "$@"

  if [$STATUS = unhidden]

  then

    chflags hidden

  else

    chflags unhidden

  fi

    killall Finder

1 Answers1

1

You’re not all that far off: your main difficulty is how to test for whether or not the file is currently hidden. (Though your bash syntax is wrong, and a Finder-based service to toggle visibility only half makes sense -- how are you going to select a file that’s invisible?) That said, this script will work [1], given that you’re running it with bash and passing the input as arguments:

for f in "$@"
do
    if ( ls -lO "$f" | grep -wq hidden )
    then
        chflags nohidden "$f"
    else
        chflags hidden "$f"
    fi
done

Killing the Finder afterwards is not necessary; it will notice changes to “hidden” automatically. If you create an Automator “service” workflow, you don’t need the “Get Selected Finder Items” action at the beginning; your service will be passed the selected items as input.

[1] Mostly. It won’t work correctly on files that have “hidden” in their name. Fixing this, perhaps by using stat(1), is left as an exercise for the reader.

Chris N
  • 905
  • 5
  • 10
  • Thanks. It mostly works but I view hidden files with another service, select the hidden file or folder and activate the service it doesn't make it unhidden. Do you know how to fix this? – user2235435 Apr 03 '13 at 05:39
  • That depends on how the files were hidden in the first place. This script works by twiddling the file’s “hidden” flag, but Finder has several different rules for hiding files, and the flag is only one of them. It will also hide any item whose name begins with “.”, and it has a fixed list of items to hide, such as “Trash”. You can’t make those files visible without using the “AppleShowAllFiles” default, which I’m guessing you already know about. (You could rename a dot-file, but then you risk other programs not being able to find it.) – Chris N Apr 04 '13 at 01:23