Fix bug #6542 (Exception thrown on save if file not visible in project tree)

Fix bug #6542 - move initialization of `wasOpen` after the check that early-exits when `$directoryNode` is null