Toggle Hidden Files without Relaunching Finder

applescriptfinder

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?

Best Answer

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.