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
activate
, even the window of the file that is opened.