MacOS – AppleScript: How to open a file in TextEdit, without bringing the other TextEdit windows to front

applescriptmacostextedit

I have the following AppleScript code that opens a specific file in TextEdit in a window with predetermined bounds:

set targetFilepath to "/Users/Me/Desktop/My Fruit Log.txt"
tell application "TextEdit"
    activate
    open targetFilepath
    set bounds of front window to {279, 111, 1180, 719}
end tell

The problem with this code is that, if I have any other TextEdit files open, the code will also bring the other documents to the front as well. I would like, however, that only this one, specific text file is brought to the front.

The code will not bring a minimized TextEdit window to the front, but it will bring all hidden, non-minimized, TextEdit windows to the front.

When I remove the activate line from the code:

tell application "TextEdit"
    open targetFilepath
    set bounds of front window to {279, 111, 1180, 719}
end tell

the file is opened, but its window is not brought to the foreground.

OS X El Capitan, version 10.11.6.


Here is a visual representation of what I've described above. I had zero windows minimized on my computer when these screenshots were taken.

This is what my screen looks like before the AppleScript is triggered:

Before

This is what I want my screen to look like after the AppleScript is triggered:

After - Desired

This is what my screen currently looks like after the AppleScript is triggered:

After - Actual


In the interest of full disclosure, my code also moves the TextEdit cursor location after opening the file. I don't know that this would affect a potential solution, but here is the additional code that follows the above passage (x is an integer that is assigned beforehand):

tell application "TextEdit" to activate
    tell application "System Events" to tell process "TextEdit"
        repeat x times
            key code 125
        end repeat
    end tell
end tell

Best Answer

I think the problem you're having is that you've misunderstood what is actually going on. Let me try to explain...

The first question to answer is why were the windows missing from your first screenshot not visible? Obviously, the My Fruit Log.txt file was not visible because it wasn't open yet (i.e. your script hadn't run yet). But what about the other windows? There's only three possibilities:

  1. TextEdit wasn't actually running (therefore no windows exist). This would be obvious - so no more discussion needed.
  2. TextEdit was running, but it was hidden (therefore all windows are hidden). This just means you hid TextEdit from view. Running your script simply makes it visible again (along with all its windows except for any that may be minimised).
  3. TextEdit was running but all windows were minimised. This just means TextEdit is running but no windows are visible because they've been miminimed. Running your script makes the app itself visible and in the foreground again, but all minimised windows remain minimised.

In your scenario, it's almost certain that Option 2 applies. So, while it's not what you want to hear, your script is actually working the way it's supposed to in macOS. The only way I can think of at present to achieve what you want in a practical way is to ensure that your script minimises all other windows except for the one it's just opened. For example, you could achieve this with the following code:

set targetFilepath to "/Users/Me/Desktop/My Fruit Log.txt"
tell application "TextEdit"
    activate
    open targetFilepath
    tell (windows whose id is not (get id of front window) and visible is true)
        set miniaturized to true
    end tell
    set bounds of front window to {279, 111, 1180, 719}
end tell

As you can see, I've just added two lines of code. I think they're fairly self-explanatory - They just tell TextEdit to minimise any window that meets these two conditions (1. it's not the front window and 2. it is visible).

Hopefully this works for you - but if not maybe someone else comes up with a better solution.