How to open list of the URLs in certain specified browser?

215 Views Asked by At

Currently, I am trying to open list of the URLs in the certain specified browser. I tried following without success. If anybody knows how to do it, please help.

-- script: Open list of URls in certain specified browser

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

-- get shared workspace object.
set sharedWorkspace to current application's NSWorkspace's sharedWorkspace()

-- get browser's NSURL
set appPath to POSIX path of (path to application id "com.google.Chrome")
set appURL to current application's |NSURL|'s fileURLWithPath:appPath

-- create array of URLs 
set theURL to current application's |NSURL|'s URLWithString:"https://www.google.gr"
set URLArray to current application's NSMutableArray's new()
(URLArray's addObject:theURL)

-- FOLLOWING THROWS ERROR "unrecognized selector"
-- Trying to open list of URls in certain specified browser
sharedWorkspace's openURLs:URLArray withApplicationAt:appURL configuration:(missing value) completionHandler:(missing value)
1

There are 1 best solutions below

2
Robert Kniazidis On

I got help from the user @Shane Stanley. They helped me correct the syntax on the last code line. For those of you who are curious, the following code allows AsObjC to open multiple URLs in a non-default browser. You can of course indicate the default browser as well.

-- script: Open list of URls in certain specified browser

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

-- get shared workspace object.
set sharedWorkspace to current application's NSWorkspace's sharedWorkspace()

-- get browser's NSURL
set appPath to POSIX path of (path to application id "com.google.Chrome")
set appURL to current application's |NSURL|'s fileURLWithPath:appPath

-- create array of URLs 
set theURL to current application's |NSURL|'s URLWithString:"https://www.google.gr"
set URLArray to current application's NSMutableArray's new()
(URLArray's addObject:theURL)
set theURL to current application's |NSURL|'s URLWithString:"https://www.macscripter.net/viewforum.php?id=2"
(URLArray's addObject:theURL)

-- open list of URls in certain specified browser
sharedWorkspace's openURLs:URLArray withApplicationAtURL:appURL configuration:(missing value) completionHandler:(missing value)

Cleaned version using handler:

-- script: Open list of URls in certain specified browser

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

openURLsWithApplication({"https://www.google.gr", "https://www.macscripter.net/viewforum.php?id=2"}, "com.google.Chrome")

on openURLsWithApplication(urlsList, appID)
    -- get shared workspace object.
    set sharedWorkspace to current application's NSWorkspace's sharedWorkspace()
    -- get browser's NSURL
    set appPath to POSIX path of (path to application id appID)
    set appURL to current application's |NSURL|'s fileURLWithPath:appPath
    -- create array of URLs 
    set URLArray to current application's NSMutableArray's new()
    repeat with textURL in urlsList
        (URLArray's addObject:(current application's |NSURL|'s URLWithString:textURL))
    end repeat
    -- open list of URls in certain specified browser
    sharedWorkspace's openURLs:URLArray withApplicationAtURL:appURL configuration:(missing value) completionHandler:(missing value)
end openURLsWithApplication