Adrian Zaugg
Adrian Zaugg

Reputation: 328

Open a new Finder Window if there is none (or just an info window) with Apple Script?

No solution in Apple Script I have found to use an existing Finder window or open a new one is fail safe. Mostly it is suggested

tell application "Finder"
    if not (exists window 1) then
        make new Finder window
    end if
end tell

With this, if there is an info window open the code doesn't open a "normal" Finder window. An info window (opened e.g. by "Get Info") is a Finder window as well, and it can't handle calls like the following, which is intended for "normal" Finder windows:

tell application "Finder"
    set the target of the front Finder window to folder thePath
end tell

How do I have to script to open a new "normal" Finder window if there is no "normal" Finder window?

Upvotes: 3

Views: 1368

Answers (1)

Adrian Zaugg
Adrian Zaugg

Reputation: 328

The info window is not a Finder window in Apple Script. The condition "if not (exists window 1)..." should mention you are looking for a Finder window:

if not (exists Finder window 1) then

So you forgot to add "Finder" in front of window. In addition you can open a new Finder window when none is open or all are minimized:

tell application "Finder"
    if not (exists Finder window 1) or (get collapsed of the front Finder window) then
        make new Finder window
    end if
    set thePath to POSIX file /your/path/to/show
    set the target of the front Finder window to folder thePath
    activate
end tell

Upvotes: 2

Related Questions