0

My current code is:

let task = Process()
task.executableURL = app.executableURL
task.arguments = windows // windows is an array of urls
try? task.run()

Here I am opening an instance of the safari app.

How could I set the size of the safari app when opening it using Process or similar with swift?

Nighthawk
  • 772
  • 11
  • 21

1 Answers1

1

This is not how you open an app on Mac; you use NSWorkspace to launch other apps. For a web page, you generally would never launch Safari directly though. That would create multiple instances of Safari, which is not generally what users expect.

If you want Safari to open an URL in its own window, bring that window to the foreground, and place that window at a specific location, you'd generally use Apple Events. For example, the following AppleScript will open a 1024x768 window at location 100,100 for example.com:

tell application "Safari"
    open location "https://google.com"
    set bounds of first window to {100, 100, 1024, 768}
end tell

The easiest way to run AppleScript inside an app is with NSAppleScript:

let script = NSAppleScript(source: "...")!
var error: NSDictionary?
script.executeAndReturnError(&error)
if let error = error { ... error handling ... }

AppleScript is a subtle and tricky language, and often poorly documented. You may need to experiment a bit to make sure it's behaving exactly as you want. But it's extremely powerful and the core technology for controling other apps.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • Thank you. How might I pass a value of a swift variable into the apple script string? For example if I had a swift variable that held the value "Safari" how could I pass that variable into apple script? – Nighthawk Mar 20 '22 at 21:53
  • With string substitution. Though, you can also create actual AppleScript script files and read them, and they can accept parameters if things become much more complicated. And eventually you can consider directly sending Apple Events, which is more complex, but allows more control. That said, if you're replacing "Safari," you're in trouble. Not every app accepts the same events. If you *only* want to position the window, you can send messages to System Events: https://stackoverflow.com/questions/12803847/set-position-of-window-with-applescript – Rob Napier Mar 20 '22 at 23:52
  • 2
    (In the most general case, your question is impossible. Applications and windows are separate things. You don't open an application "with a specific size." You can open applications, and between the application and System Events (i.e. the window manager), windows are placed in various spots. You can just make requests. But for most apps, this approach will work.) – Rob Napier Mar 20 '22 at 23:54
  • do you know how I would convert my array of urls to an AppleScript equivalent so I can open them all with safari? – Nighthawk Mar 21 '22 at 00:33
  • I don't know what you mean. Do you want a bunch of Safari windows to open on top of each other? – Rob Napier Mar 21 '22 at 00:37
  • No, I would like to open safari urls in many tabs, but all in one window. – Nighthawk Mar 21 '22 at 00:42
  • Several examples: https://stackoverflow.com/questions/2892622/open-url-in-new-safari-tab-with-applescript, – Rob Napier Mar 21 '22 at 00:47
  • Thank you, but if I have a swift array of urls (e.g., [“apple.com”, “stackoverflow.com”]) how could I iterate over these using apple script to open each of these urls. How could I convert a swift array into a apple script list – Nighthawk Mar 21 '22 at 00:56
  • String interpolation. You'll need to create a Swift String that is the AppleScript code you want to compile. – Rob Napier Mar 21 '22 at 01:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/243143/discussion-between-nighthawk-and-rob-napier). – Nighthawk Mar 21 '22 at 12:37