Reputation: 8202
I tried to write a small AppleScript which will open a new Safari Window. This window should show a specific URL and shall be opened in a given size.
The additional constraints are:
What I've come up with is this:
-- Explanations in Comments.
on run
tell application "Safari"
set myURL to "https://…long url for The Moth FM…"
activate
-- activate will open a new empty window when Safari is not running.
-- It will bring any other window to the front when it's running.
try
set oldBounds to bounds of front window
-- I try to remember the bounds of the (new) window.
if "favorites://" = (URL of document of front window) then
-- if the window was an empty one, it was most probably a new window.
close front window
-- try to close it.
end if
on error errStr number errNum
-- all the above might fail.
-- So we do not know the original window size at the moment.
make new document with properties {URL:"favorites://"}
-- I open a brand new window,
set oldBounds to bounds of front window
-- so that I get the bounds of new windows.
close front window
-- Then I directly close it again.
end try
-- All the above is just preparation to rememver Safari's
-- default Window size (and position).
make new document with properties {URL:myURL}
-- Now I create the window I want
set bounds of front window to {21, 46, 621, 191}
-- and set its size.
-- This size has now become Safari's new default size. :(
make new document with properties {URL:"favorites://"}
-- So I create another window
set bounds of front window to oldBounds
-- and set its size to the default size retrieved above
close front window
-- and close it, as I don't need it.
end tell
end run
This seems a bit cumbersome and maybe someone here has an idea how to do this more smoothly without all the window-"flickery"?
Upvotes: 1
Views: 1404
Reputation: 572
Here's the code I played around with, based on your last comment. Feel free to update to your needs, not really a proper answer but I hope it helps you accomplish your goal, good luck!
global DEFAULT_BOUNDS, CUSTOM_BOUNDS
-- set DEFAULT_BOUNDS to {x:missing value, y:missing value, w:missing value, h:missing value}
set DEFAULT_BOUNDS to {x:210, y:146, w:621, h:491}
set CUSTOM_BOUNDS to {x:21, y:46, w:621, h:191}
-- part1() -- un/commen as needed
part2() -- un/comment as needed
The new window should have your resized dimensions.
to part1()
activate application "Safari"
tell application "System Events" to tell process "Safari" to set theWindow to first window
resize(theWindow, CUSTOM_BOUNDS)
tell application "Safari" to quit
repeat while running of application "Safari" is true
delay 1
end repeat
activate application "Safari"
end part1
Now try this:
The new window should have your new default dimensions.
to part2()
set theFirstWindow to newSafariWindow("https://www.example.com")
resize(theFirstWindow, CUSTOM_BOUNDS)
set theNewWindow to newSafariWindow("https://www.yahoo.com")
resize(theNewWindow, DEFAULT_BOUNDS)
tell application "Safari" to close first window
tell application "Safari" to quit
repeat while running of application "Safari" is true
delay 1
end repeat
activate application "Safari"
end part2
Some auxilliary handlers
on saveCurrentBounds(referenceWindow)
tell application "System Events"
set thePosition to position of referenceWindow
set x of DEFAULT_BOUNDS to first item of thePosition
set y of DEFAULT_BOUNDS to second item of thePosition
set theSize to size of referenceWindow
set w of DEFAULT_BOUNDS to first item of theSize
set h of DEFAULT_BOUNDS to second item of theSize
end tell
end saveCurrentBounds
on newSafariWindow(theUrl as text)
if running of application "Safari" is false then
do shell script ("open -a Safari " & theUrl)
script WindowWaiter
if (count of (windows of application "Safari")) is not 0 then return true
end script
wait on WindowWaiter
else
tell application "Safari" to make new document with properties {URL:theUrl}
end if
delay 1
tell application "System Events" to tell process "Safari" to return front window
end newSafariWindow
to resize(theWindow, newBounds)
tell application "System Events"
set position of the theWindow to {x of newBounds, y of newBounds}
set size of the theWindow to {w of newBounds, h of newBounds}
end tell
end resize
to wait on scriptObj by sleep : 1 for ntimes : 1000
repeat ntimes times
try
set handlerResult to run of scriptObj
if handlerResult is not missing value then return handlerResult
end try
delay sleep
end repeat
return missing value
end exec
Upvotes: 1