9 Replies Latest reply: May 1, 2011 10:49 AM by rccharles
mahender negi Level 1 Level 1 (65 points)

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 button 2 with title "/MSN/" with icon 0

 

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

 

 

 

 

copy the result as list to {buttonpressed}

 

try

  if the buttonpressed is "Cancel" then quit -- it will cancel the process/script

  -- below line will hidden files will be hidden

  if the buttonpressed is "Hide Hidden Files" then do shell script ¬

  "defaults write com.apple.finder AppleShowAllFiles OFF"

  -- below line will show hidden files

  if the buttonpressed is "Show Hidden Files" then do shell script ¬

  "defaults write com.apple.finder AppleShowAllFiles ON"

 

end try

 

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)
  • John Maisey Level 5 Level 5 (6,895 points)

    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"

  • mahender negi Level 1 Level 1 (65 points)

    Hi John

    Thanks !!!, it works for me

    Now I got as I was looking for. Just to add in this I want to display Finder icon with startup dialog ( as I know I have three choice icon 1,2 & 3)

     

    Picture 1042.png

  • John Maisey Level 5 Level 5 (6,895 points)

    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

  • mahender negi Level 1 Level 1 (65 points)

    Hi John,

     

    Thanks a ton

    Mahender

  • mahender negi Level 1 Level 1 (65 points)

    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.

     

    Picture 1050.png

     

     

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

     

    tell application "Finder" to quit

     

    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

    delay 1 

    tell application "Finder" to launch

  • John Maisey Level 5 Level 5 (6,895 points)

    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"

  • mahender negi Level 1 Level 1 (65 points)

    Thanks John,

     

    Finally I got that for which I was looking

    you rocks !!

  • John Maisey Level 5 Level 5 (6,895 points)

    Glad it helped.

  • rccharles Level 5 Level 5 (6,265 points)

    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