How to check if invisible items are visible? How do I check in AppleScript, if invisible objects are visible?
If visible, do: defaults write com.apple.finder AppleShowAllFiles 0
in invisible, do: defaults write com.apple.finder AppleShowAllFiles 1.

That should be one script, so I can change the visible with one click. Is that possible with AppleScript?
An alternative to grgarside's script, this doesn't need you to restart the Finder, just refresh open windows.

Can be saved as an app or in Automator, as a Service.
I have it as a Service, triggered on  Cmd ⌘    H  from System Prefs > Keyboard > Shortcuts > Services - which required me to change the 'Hide Finder' key command in …Shortcuts > App Shortcuts to something other than default.

To add as Service, launch Automator then File menu > New.
Select Service in the dialog then drag Run Applescript to the new Automator window.
Copy/paste the script inside the on run - replacing (* Your script goes here *) &amp; Save. It needs only the default parameters for everything else.



In System Prefs > Keyboard > Shortcuts > Services it will be added as the last line. Set any trigger key command you like. 



If you use  Cmd ⌘   H  the same as me, you'll need to change Hide Finder to something else - I just used a garbage shortcut as I never need that command…



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 " &amp; 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


The script itself reads the current state of the invisible flag (less elegantly than grgarside's version but just as effectively;) then toggles each open window to another view type - list, icon etc then back again.

Edit: Just refreshing the windows no longer works in El Capitan, you now have to restart the Finder.
New version for El Capitan

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 " &amp; newHiddenVisiblesState &amp; "; killall Finder"