macosshellterminal

Terminal scripting in OS X


I've never created a script before and am looking for a tutorial on writing a script for OS X v10.6 (Snow Leopard). There is a terminal command that can show all hidden files. It's

defaults write com.apple.finder AppleShowAllFiles TRUE
killall Finder

Changing TRUE to FALSE will hide system files. I want to make a script that checks the value of AppleShowAllFiles, and if TRUE, writes FALSE, and if FALSE, writes TRUE.

Is this done in TextEdit and saved as a .sh file? Can a script be something I double-click that just runs, or do I have to start terminal and type a command to execute the script?


Solution

  • Make a file, switchhideshow.command, with the following content:

    #!/bin/sh
    
    show=`defaults read com.apple.Finder AppleShowAllFiles 2>/dev/null`
    
    if [ "$show" == "TRUE" ]; then
      defaults write com.apple.Finder AppleShowAllFiles FALSE
    else # Here we come. If it is FALSE or is empty (the default)
      defaults write com.apple.Finder AppleShowAllFiles TRUE
    fi
    
    killall Finder
    

    Then: chmod a+x switchhideshow.command

    Ready. Unfortunately, you should close the terminal every time you run it. Also, you might want to look at this. It describes how to call the shell script to show hidden files from Automator Actions.