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:
This is what I want my screen to look like after the AppleScript is triggered:
This is what my screen currently looks like after the AppleScript is triggered:
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:
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:
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.