Hi,
On my mac, an external screen is connected.
How to write an applescript in such a way that each new opened window of a specific chosen application opens in the main screen or in the external screen?
thanks
Hi,
On my mac, an external screen is connected.
How to write an applescript in such a way that each new opened window of a specific chosen application opens in the main screen or in the external screen?
thanks
You don’t, at least directly.
The application decides where new windows are opened.
The only alternatives would be to:
have a script tell the app to create a new document/window, and then move that window to your preferred location.
have a background script constantly poll the application to (attempt to) detect a new window, and then move the window accordingly.
Both are prone to problems.
In the first case you need to switch out of your app to create new documents. You’d have to run the script rather than simply File → New in the app. Potentially you could write a keyboard shortcut that overrides Cmd-N to do this for you, but it’s a little hokey.
In the second case you’d have to have your script running in the background, periodically polling the application in question to find its windows, and react when a new window is added. This has other problems such as race conditions (for example, there will be a delay between the new window opening and your script reacting, and knowing what to do if the user has already moved the window), and may be complicated by the application in question and how it handles windows.
In short, there’s no silver bullet for this question.
Just for grins, I took a run at this using my second model - a background app that polls for new windows and moves them.
Change the properties at the beginning to match your needs (I targeted Safari, but you may want a different app, and it’s likely my screen coordinates don’t match yours)
Note that different apps may define bounds differently. AppleScript itself states:
There are various standards for using four numbers to designate a rectangle. The old way is to specify the x and y coordinates of the origin corner, and the x and y coordinates of the opposite corner, of the rectangle. But the origin corner might be the top left (traditional) or the bottom left (newer), and the Cocoa standard is to use the third and fourth numbers for the width and height of the rectangle.
so you may need to experiment with your target app.
Save the script as a Stay Open application, then any time it’s running it will periodically poll the app (based on delayTime to check for new windows. Any new window is automatically moved to the coordinates.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
property appName : "Safari"
property delayTime : 10 -- how often to check.. decrease for more frequent checks
property newWinCoordinates : {1859, 81, 3587, 1075} -- where should the new window appear - adjust as needed
global windowIDList
on run
set windowIDList to {}
end run
on idle
-- first check if the app is running - no need to do anything if it isn't
tell application "System Events"
if not (exists application process appName) then
return delayTime
end if
end tell
-- if we get here, the app is running
-- first check if we have a list of windows
-- if it's empty this is our first run, so populate it with the current windows
if windowIDList = {} then
tell application appName
-- grab just the ids, since we don't need to worry about the rest of the windows' data
set windowIDList to id of every window
end tell
else
-- if we get here, we had some windows on the last run, so check the current window list
tell application appName
set curWindowIDs to id of every window
if windowIDList ≠ curWindowIDs then
-- the window list has changed
-- find any new window(s)
repeat with wID in curWindowIDs
if wID is not in windowIDList then
-- new window, so set its position
set bounds of window id wID to newWinCoordinates
end if
end repeat
set windowIDList to curWindowIDs
end if
end tell
end if
return delayTime
end idle
FWIW, I wrote a script that takes the above approach. It must be run in the background–for example by the AppleScript Script Menu. Also, the active app must support the command+N keyboard shortcut to open a new window. I tested this script on my Sonoma computer with Safari, TextEdit, Script Editor, Script Debugger, and a Finder window. I don’t have an external display, so I used position coordinates on my main display.
--this script must be run in the background
--the app that has the focus must open a new window with the command+N keyboard shortcut
tell application "System Events"
set activeApp to name of first process whose frontmost is true
tell process activeApp
set frontmost to true --probably not necessary
keystroke "n" using command down --the active app must support this keyboard shortcut
delay 0.3 --test different values
set position of window 1 to {100, 35} --set to desired position on external display
end tell
end tell
BTW, I’m not certain that I understand what the OP wants to do, but this is my best guess.