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

Hi, all.

Currently, I am trying to open list of the URLs in the specified certain 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 withApplicationAtURL:appURL configuration:(missing value) completionHandler:(missing value)

You’ve mis-typed the method name. “withApplicationAt:” should be “withApplicationAtURL:”.

Thanks, Shane.

Probably, without your help, this method would have remained a mystery to everyone, since there is not a single AsObjC example using it on the Internet. Meanwhile, this method allows the AsObjC code to open multiple URLs in the specified browser instead of the default browser. I have corrected the error you pointed out. Thanks.

Here is cleaned version using the 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 appURL to sharedWorkspace's URLForApplicationWithBundleIdentifier:appID
	-- 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

That’s probably due, at least in part, to the fact that it was only introduced in macOS 10.15. It essentially replaces openURLs:withApplicationAtURL:options:configuration:error: (which is deprecated as of macOS 11, but will probably continue to work for some time).

Thanks, @Fredrik71.

You correctly noted that there is such a method to get the NSURL of an application by the ID. I have updated my last script according to your comment.

Hi, I hope you do not mind me asking how do I adapt the script to open window “Incognito” mode
Cheers

One208,

For the Google Chrome you can add following command before opening urls code line:


tell application id "com.google.Chrome" to make new window with properties {mode:"incognito"}

Hi KniazidisR
used above code before → Result is opens new incognito window, but URl’s open in normal window

Also in Handler

Here is my script, please help adapt, cheers

on run {}
	ChrAction(UrlChoice() of me) of me
end run
on UrlChoice()
	----News------------------------------------------------------------
	set TeleG to "https://www.telegraph.co.uk/"
	set DailyM to "https://www.dailymail.co.uk/home/index.html"
	----HeatingOil------------------------------------------------------------
	set Site1Fuel to "https://homefuelsdirect.co.uk/"
	set Site2Fuel to "https://theheatingoilclub.co.uk/"
	set Site3Fuel to "https://tanktopper.co.uk/tt-web-enquiry/"
	set Site4Fuel to "https://https://www.boilerjuice.com/heating-oil/"
	set Site5Fuel to "http://www.heatingoilshop.com/price.php"
	----------------------------------------------------------------
	set mList to {"HeatingOil", "News"}
	set chosenOption to choose from list of mList with prompt "what you want to do, please select:" default items "AppSearch"
	if chosenOption is false then
		error number -128 (* user cancelled *)
	else
		set chosenOption to chosenOption's item 1 (* extract choice from list *)
	end if
	-- display dialog "You chose → " & chosenOption & "!"
	
	-- "mode" is last in list, parsed in handler to determine incognito or otherwise 
	if chosenOption is "HeatingOil" then
		-- Last on the List is first tab
		set mode to "InCog"
		set theUrls to {Site1Fuel, Site2Fuel, Site3Fuel, Site4Fuel, Site5Fuel, mode}
	else if chosenOption is "News" then
		set mode to "Cog"
		set theUrls to {TeleG, DailyM, mode}	
	end if
	return theUrls -- Returns out as list, last item is InCog or Cog (InCog=incognito, Cog=Normal Mode) 
end UrlChoice
--================================================================
--» HANDLER 
--> theUrls = List of urls
--> Mode = "Cog" or "InCog"
--> Usage = ChrAction(theUrls, "InCog") for InCognito mode
--================================================================
on ChrAction(theUrls)
	set mode to the last item of theUrls
	set theUrls to items 1 thru -2 of theUrls
	if mode is "Cog" then
		tell application id "com.google.chrome" to tell (make new window)
			repeat with theURL in theUrls
				set newTab to make new tab with properties {URL:theURL}
			end repeat
			tell tab 1 to close
		end tell
	else if mode is "InCog" then
		tell application id "com.google.chrome" to tell (make new window with properties {mode:"incognito"})
			repeat with theURL in theUrls
				set newTab to make new tab with properties {URL:theURL}
			end repeat
			tell tab 1 to close
		end tell
	end if
end ChrAction

Indeed, as I checked, the AsObjC opens additional none incognito window instead of making tabs in the already existing incognito window. So, I suggest you to not use AsObjC version. Here, I fixed your plain AppleScript version:


set News to {"https://www.telegraph.co.uk/", "https://www.dailymail.co.uk/home/index.html"}
set HeatingOil to {"https://homefuelsdirect.co.uk/", "https://theheatingoilclub.co.uk/"}

set choisenGroup to choose from list {"News", "HeatingOil"} with prompt "Please, select sites group from:"
if choisenGroup is false then return

if (item 1 of choisenGroup) is "News" then
	set theURls to News
	tell application id "com.google.Chrome" to set aWindow to make new window with properties {mode:"normal"}
else
	set theURls to HeatingOil
	tell application id "com.google.Chrome" to set aWindow to make new window with properties {mode:"incognito"}
end if

tell application id "com.google.chrome" to tell aWindow
	repeat with theURL in theURls
		make new tab with properties {URL:theURL}
	end repeat
	close tab 1
end tell

@Fredrik71,

I tested your suggestion. It opens the URLs in the normal window instead of incognito.

Then, I tried to open the incognito window using completion handler in the following way. It opens incognito window, but immediately after it fails to add tabs… and throws error “Can’t make window id 149 of application “Google Chrome” into the expected type”. So, I guess, this AsObjC method expects only normal mode window. My conclusion is: this method works only with windows in the normal mode, unfortunately.

Following fails to add tabs to the incognito window:


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

set News to {"https://www.telegraph.co.uk/", "https://www.dailymail.co.uk/home/index.html"}
set HeatingOil to {"https://homefuelsdirect.co.uk/", "https://theheatingoilclub.co.uk/"}

set choisenGroup to choose from list {"News", "HeatingOil"} with prompt "Please, select sites group from:"
if choisenGroup is false then return

if (item 1 of choisenGroup) is "News" then
	openURLsWithApplication(News, "com.google.Chrome", missing value)
else
	openURLsWithApplication(HeatingOil, "com.google.Chrome", my openIncognito())
end if

on openURLsWithApplication(urlsList, appID, myCompletionHandler)
	-- get shared workspace object.
	set sharedWorkspace to current application's NSWorkspace's sharedWorkspace()
	-- get browser's NSURL
	set appURL to sharedWorkspace's URLForApplicationWithBundleIdentifier:appID
	-- 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:myCompletionHandler
end openURLsWithApplication

on openIncognito()
	tell application id "com.google.Chrome" to make new window with properties {mode:"incognito"}
end openIncognito

I’m surprised that doesn’t crash immediately – the completionHandler: parameter takes a block (effectively a chunk of code to be executed at some later stage) as its argument, and blocks aren’t supported in AppleScriptObjC. Such methods usually only accept missing value when called from AppleScript.

@Shane,

Here is working example I wrote one month ago with completion handler in this form. Test it as application to ensure I am not lying.


use framework "Cocoa"
use scripting additions

on show(title as string)
	set alert to current application's NSAlert's new()
	tell alert
		its setMessageText:title
		its beginSheetModalForWindow:(current application's NSApp's mainWindow()) completionHandler:(my completionHandler())
		its runModal()
	end tell
end show

on completionHandler()
	set aList to {"FIVE !", "FOUR !", "TREE !", "TWO !", "ONE !"}
	repeat with anItem in aList
		display alert anItem buttons {"ok"} giving up after 1
		delay 1
	end repeat
end completionHandler

show("START!")

I don’t doubt you; I just suspect what you’re seeing is a bit of a fluke. If AS could pass completion handlers, it would still need to match the number of arguments, which you’re not doing here.

I tested exactly this way (with openConfiguration), @Fredrik71. Also, I tested with Terminal:
[format]open -a “Google Chrome” --args -incognito[/format]
Both open normal window instead of incognito. And, if I understand the openConfiguration documentation correctly, passed to sandboxed application arguments is ignored.