3

I'm pretty new here, and I looked for an answer to this question, and found some answers but none seemed to work for me.

I want to be able to run an applescript that toggles hidden files and immediately updates all the open finder windows.

Using the scripts in these threads:

https://stackoverflow.com/questions/29135878/what-is-the-quickest-way-to-toggle-hide-show-hidden-files-on-a-mac-os-x-yosemite

https://stackoverflow.com/questions/21788558/automator-command-to-refresh-all-finder-all-finder-windows

https://stackoverflow.com/questions/20469894/how-to-make-a-script-to-show-hide-hidden-files-in-mac-os-x

I've come up with something that looks like this:

try
    set state to (do shell script "defaults read com.apple.finder AppleShowAllFiles") as boolean
on error
    set state to false
end try

do shell script "defaults write com.apple.finder AppleShowAllFiles " & (not state)

tell application "Finder"
    set theWindows to every window
    repeat with i from 1 to number of items in theWindows
        set this_item to item i of theWindows
        set theView to current view of this_item
        if theView is list view then
            set current view of this_item to icon view
        else
            set current view of this_item to list view
        end if
        set current view of this_item to theView
    end repeat
end tell

However, as of my version of OS X (10.11.5), this doesn't actually toggle the hidden files. I need to relaunch finder in order to see the changes. I know how to do this in the script with: do shell script "killall Finder", but I don't know how to get all my current windows back and repositioned to where they were.

TL;DR: Is there a nice way to force the Finder windows to refresh without having to relaunch Finder? Or if there isn't, how can I keep my windows after a restart?

1 Answer 1

2

The trick of switching views worked well in Yosemite but no longer in El Capitan.

I've had to go back to the simple, yet annoying

set newHiddenVisiblesState to "YES"
try
    set oldHiddenVisiblesState to do shell script "defaults read com.apple.finder AppleShowAllFiles"
    if oldHiddenVisiblesState is in {"1", "YES"} then
        set newHiddenVisiblesState to "NO"
    end if
end try
do shell script "defaults write com.apple.finder AppleShowAllFiles " & newHiddenVisiblesState & "; killall Finder"

Rather than the much nicer version which worked before

set newHiddenVisiblesState to "YES"
try
    set oldHiddenVisiblesState to do shell script "defaults read com.apple.finder AppleShowAllFiles"
    if oldHiddenVisiblesState is in {"1", "YES"} then
        set newHiddenVisiblesState to "NO"
    end if
end try
do shell script "defaults write com.apple.finder AppleShowAllFiles " & newHiddenVisiblesState    

tell application "Finder"
    set theWindows to every Finder window
    repeat with i from 1 to number of items in theWindows
        set this_item to item i of theWindows
        set theView to current view of this_item
        if theView is list view then
            set current view of this_item to icon view
        else
            set current view of this_item to list view

        end if
        set current view of this_item to theView
    end repeat
end tell

Honestly, I'd love this answer to be wrong

BTW, Windows ought to reopen exactly where they were - but not if you have them spread over more than one Space; that's not possible.

1
  • so if i understand correctly, pre-OS 10.11 with Tetsujin's first script, you COULD show/hide the hidden files withOUT the Finder relaunching, but you cannot post-OS 10.10? that seems ridiculous (IMO). how come apps like Panic's Transmit FTP program can show/hide without relaunching it & the Finder can't? would LOVE to find an app to simply toggle this in ONE Finder window, not all. Aug 1, 2016 at 20:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .