2

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
5
  • My original post contained an error. Please see the revised post. No TextEdit windows are brought forward without activate, even the window of the file that is opened. Mar 15, 2017 at 2:26
  • So, what is your preferred solution here? Do you want your script to run and then hide all the other TextEdit windows, or do you want it to mnimise all the other TextEdit windows instead? Or doesn't it matter? Or something else?
    – Monomeeth
    Mar 15, 2017 at 3:37
  • @Monomeeth Not minimized. My preferred solution is that this one file comes to the foreground and the other TextEdit windows (that are currently in the background when the script is triggered) remain where they are in the background. If the other windows must be brought to the front, then, afterwards, I would like them sent to the background. Mar 15, 2017 at 3:59
  • I'm not quite getting what the issue is here - maybe I need another coffee! :) Please bear with me, but, if your script opens the file you want in TextEdit and activates it so that it's brought to the foreground, doesn't that by default mean all the other TextEdit windows are in the background? Or are you wanting to actually change the way applications behave in macOS (e.g. when you activate the Finder, all Finder windows are visible but only one is in the foreground, likewise with all other apps). So, unless you want the windows closed, minimised or hidden, they will be visible.
    – Monomeeth
    Mar 15, 2017 at 4:26
  • @Monomeeth I may not be using the correct terminology. I want the other windows to be hidden. These windows may be technically "in the background" when my Service is triggered, but they are no longer hidden. I think that using screenshots is the best way to show what I'm describing. Please see the screenshots that I've added to my post. Mar 15, 2017 at 5:03

2 Answers 2

2

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.

4
  • "TextEdit was running, but it was hidden (therefore all windows are hidden)." Are you saying that I clicked the "Hide" button that is presented when one right-clicks on the TextEdit icon in the Dock? That is not the case. The reason that no TextEdit windows are visible in my first screenshot is because a different application (i.e., Preview.app) was opened on top of the TextEdit windows. Also, I use Mission Control, so it is entirely possible for me to have, for example, two TextEdit windows in front of a window of a different app, and two TextEdit windows hidden behind that window. Mar 15, 2017 at 8:17
  • That's not exactly what I'm saying, but the Dock example you've used is one way it could be hidden. Another is using the Hide Others option from the application menu of other apps. And yet another is exactly what you've just described in your comment - that it was hidden because other applications and their windows (Preview.app in your screenshot example) were in the foreground. Regardless of how TextEdit was hidden from view, whenever it is brought to the foreground again, all its windows become visible (unless they're minimised), and that's what is happening when your script runs.
    – Monomeeth
    Mar 15, 2017 at 8:37
  • Basically, what I was trying to do with my answer was explain why your script resulted in the behaviour you were experiencing and why that was to be expected. My minor addition to your code deals wth this fact, but of course there may be better ways to do what you want. I haven't tested the answer by @Mockman, but it may also be a good solution. The reason I opted for my approach was that I've noticed you're asking a lot of questions about AppleScript and I thought slightly modifying your existing code was a better approach for you. But only you can decide which one, if any, suits you.
    – Monomeeth
    Mar 15, 2017 at 8:37
  • I understand. I appreciate your answer. It was just that you said, "TextEdit was running, but it was hidden (therefore all windows are hidden)." which kind of confused me. Mar 15, 2017 at 8:47
1

How about this?

tell application "TextEdit"

    set fWin to front window -- Since you're already using 'front window'
    repeat with w in every window
        set visible of w to false
    end repeat

    -- set visible of window id 189 to true (frontmost on my mac now)
    set visible of fWin to true
    activate
end tell

I'm sure that there are other methods to identify the windows. You could also do something like this… where a list of windows from the second to the last is used.

tell application "TextEdit"
    -- Assuming that front window should be visible…
    set winList to (windows 2 thru -1)
    repeat with w in winList
        set visible of w to false
    end repeat

    activate
end tell

The key term here is 'visible'.

5
  • I can't get either of these solutions to work correctly. Should my open targetFilepath line come before or after your code? I put this line before your code. I notice no difference with your first solution; the hidden TextEdit windows still become visible. Your second solution closes out of the hidden windows completely, instead of simply making them hidden. Mar 15, 2017 at 22:44
  • Interesting. Regarding the second script, it's not closed. But it is really invisible. If you open up a script and run commands like 'visible of window id xxx' or 'id of third window', etc… then you'll get different results than you would with a 'closed' window. This is how the dictionary defines the window property… visible (boolean) : Whether the window is currently visible.
    – Mockman
    Mar 16, 2017 at 7:53
  • Regarding the first script… yes. I had started from the point at which all of your documents were already open.
    – Mockman
    Mar 16, 2017 at 7:54
  • Regarding your first comment, how do I make these invisible windows unhidden then (without using AppleScript to do so)? If I minimize all windows of all applications, these invisible windows still do not appear. Right-clicking on the TextEdit icon in the Dock and selecting "Show All Windows" doesn't bring the invisible windows forward either. Mar 16, 2017 at 8:02
  • Could be that you can't (without applescript, that is). Seems a little weird though.
    – Mockman
    Mar 16, 2017 at 8:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .