Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Show/ Hide hidden files in Finder

Hi All,


I am creating a script with which I can hide and unhide hidden files, I want to make a setup like if hidden files are hidden then script will start with a dialog

"Show all hidden files in this Mac" & if hidden files were unhide then start with a dialog "Hide all hidden files in this Mac"


rest of the script is as below



display dialog"Show all hidden files in this Mac ?"buttons{"Show Hidden Files","Cancel"}default button2with title"/MSN/"with icon0


display dialog"Hide all hidden files in this Mac ?"buttons{"Hide Hidden Files","Cancel"}default button2with title"/MSN/"with icon0





copytheresultaslistto{buttonpressed}


try


ifthebuttonpressedis"Cancel"thenquit-- it will cancel the process/script


-- below line will hidden files will be hidden


ifthebuttonpressedis"Hide Hidden Files"thendo shell script¬


"defaults write com.apple.finder AppleShowAllFiles OFF"


-- below line will show hidden files


ifthebuttonpressedis"Show Hidden Files"thendo shell script¬


"defaults write com.apple.finder AppleShowAllFiles ON"


endtry


do shell script"killall Finder"

Mac Book Pro 2.66 GHz,I-Mac Intel, Mac Pro DP,PowerMac G5 DP 2.0GHz,E-mac 1Ghz, Mac OS X (10.5.8)

Posted on Apr 28, 2011 5:27 AM

Reply
Question marked as Best reply

Posted on Apr 28, 2011 8:07 AM

Hi,


You'd need to check the preference and then select the dialog accordingly.


do shell script "defaults read com.apple.finder AppleShowAllFiles"


For the above returns "TRUE" or "FALSE" (or may not return anything if it has not been set). You could then set an if/else block on the result.


Best wishes

John M


As a matter of interest here is a script I use to do the same.


display dialog "Show or hide invisible files?" buttons {"Show", "Hide"} default button 2

if button returned of the result is "Hide" then

set myShow to "FALSE"

else

set myShow to "TRUE"

end if

do shell script "defaults write com.apple.finder AppleShowAllFiles " & myShow & " ; killall Finder"

9 replies
Question marked as Best reply

Apr 28, 2011 8:07 AM in response to mahender negi

Hi,


You'd need to check the preference and then select the dialog accordingly.


do shell script "defaults read com.apple.finder AppleShowAllFiles"


For the above returns "TRUE" or "FALSE" (or may not return anything if it has not been set). You could then set an if/else block on the result.


Best wishes

John M


As a matter of interest here is a script I use to do the same.


display dialog "Show or hide invisible files?" buttons {"Show", "Hide"} default button 2

if button returned of the result is "Hide" then

set myShow to "FALSE"

else

set myShow to "TRUE"

end if

do shell script "defaults write com.apple.finder AppleShowAllFiles " & myShow & " ; killall Finder"

Apr 29, 2011 1:56 AM in response to mahender negi

Hi Mahender,


I'm glad you found it helpful.


To show the finder icon you need to enclose the display dialog commands with a tell Finder block. Like this:


tell application "Finder"

display dialog "Shows application icon" with icon 1

display dialog "Shows alert icon with application icon" with icon 2

display dialog "Shows warning icon" with icon 0

end tell

Best wishes


John M

Apr 29, 2011 5:07 AM in response to John Maisey

Hi John,


I modified my script and now I am getting error when I run it first time and when I run it again then it run fine.


User uploaded file



setthedate_stampto((thecurrent date)asstring)


tellapplication"Finder"toquit


setOnOfftodo shell script"defaults read com.apple.finder AppleShowAllFiles"

ifOnOff="NO"orOnOff="OFF"then


setOnOffto"ON"



tellapplication"Finder"


display dialog"Show all hidden files in this Mac ?"&return&return&date_stampbuttons{"Show Hidden Files","Cancel"}default button2with title"/MSN/"with icon1


endtell

else


setOnOffto"OFF"



tellapplication"Finder"


display dialog"Hide all hidden files in this Mac ?"&return&return&date_stampbuttons{"Hide Hidden Files","Cancel"}default button2with title"/MSN/"with icon1


endtell


endif


do shell script"defaults write com.apple.finder AppleShowAllFiles "&OnOff

delay1

tellapplication"Finder"tolaunch

Apr 29, 2011 5:48 AM in response to mahender negi

The display dialog is not working because the Finder has not had time to launch. If you stop the Finder in the do shell script command you don't need to quit and launch the Finder.


set the date_stamp to ((the current date) as string)


set OnOff to do shell script "defaults read com.apple.finder AppleShowAllFiles"

if OnOff = "NO" or OnOff = "OFF" then

set OnOff to "ON"


tell application "Finder"

display dialog "Show all hidden files in this Mac ?" & return & return & date_stamp buttons {"Show Hidden Files", "Cancel"} default button 2 with title "/MSN/" with icon 1

end tell

else

set OnOff to "OFF"


tell application "Finder"

display dialog "Hide all hidden files in this Mac ?" & return & return & date_stamp buttons {"Hide Hidden Files", "Cancel"} default button 2 with title "/MSN/" with icon 1

end tell


end if


do shell script "defaults write com.apple.finder AppleShowAllFiles " & OnOff & " ; killall Finder"

May 1, 2011 10:49 AM in response to mahender negi

Here is my two cents worth. I toggle the hidden atribute. What else do you want to do anyway when you invoke the script?


I have the applescript attached to option + f12 via a keystroke mapping program. With option + f12 it's unlikely that I will invoke the program accidently.


(*
    Name: toggle-hidden


    Author: rccharles


    Copyright 2010 rccharles
    GNU General Public License 

    *)

on run
    -- Write a message into the event log.
    log "  --- Starting on " & ((current date) as string) & " --- "

    try
        set results to do shell script "defaults read com.apple.finder AppleShowAllFiles"
        log "results = " & results
    on error errorMsg
        -- Most likely, the variable AppleShowAllFiles does not exit.  
        -- AppleShowAllFiles isn't defined in a newly created account. 
        -- The write command below will create the AppleShowAllFiles variable.
        log "Error ..." & errorMsg
        set results to "FALSE"
    end try

    if results = "FALSE" then
        display dialog "Displaying all files.  Finder will restart." giving up after 2
        set the_rc to do shell script "defaults write com.apple.finder AppleShowAllFiles TRUE  ;killall Finder"

    else
        display dialog "Hiding hidden files.  Finder will restart.." giving up after 2

        set the_rc to do shell script "defaults write com.apple.finder AppleShowAllFiles FALSE  ;killall Finder"

    end if

end run

Show/ Hide hidden files in Finder

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.