Improve applescript to open New Private Window with Safari
Try the following:

tell application "Safari"
    activate
    delay 0.5
end tell
tell application "System Events"
    open location "http://localhost:38"
    delay 0.5
    keystroke "w" using command down
    keystroke "n" using {shift down, command down}
end tell




Now here is the code I'd choose to use as it checks to see if Safari is already open and if yes, just opens a new Private window. If not then it works as I believe you intended, which is to close the opening window and then open a new Private window.

on is_running(appName)
    tell application "System Events" to (name of processes) contains appName
end is_running

set isRunning to is_running("Safari")

if isRunning then
    tell application "Safari" to activate
    delay 0.5
    tell application "System Events" to keystroke "n" using {shift down, command down}
else
    tell application "Safari" to activate
    delay 1
    tell application "System Events"
        keystroke "w" using command down
        keystroke "n" using {shift down, command down}
    end tell
end if



Note: The value of the delay may need to be adjusted for the speed of your system.  As coded the present values of delay work fine on my system.  The value is in seconds or fractions thereof as a decimal value.


By the way, I see no need to open a new Tab to "a URL that cannot be found" as I believe my method herein, in the block of code directly above, achieves your intended goal, both quickly and without as you put it, "see that the Safari window is launched in private mode without so much "macro" like appearance.".



By the way, the code in your question could be reduced to the example below and would actually run a bit faster then the way you presently have it coded.

tell application "Safari"
    activate
end tell
tell application "System Events"
    open location "http://localhost:38"
    click menu item "New Private Window" of menu "File" of menu bar 1 of process "Safari"
    click menu item "Failed to open page" of menu "Window" of menu bar 1 of process "Safari"
    click menu item "Close Window" of menu "File" of menu bar 1 of process "Safari"
end tell


However my first (and second) example, at the top of this answer, acts directly without seeing the menus open and close as it's not using click events but key presses.  Also while the condensed example of your code is just that, I wouldn't use it without appropriate delay statements where applicable.

In the end, I'd choose to use the second block of code shown in this answer.