Friday, January 28, 2022

#1 2021-10-23 12:00:55 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2208

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.

Applescript:


-- 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)

Last edited by KniazidisR (2021-10-23 05:29:41 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#2 2021-10-23 05:02:47 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6748

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

You've mis-typed the method name. "withApplicationAt:" should be "withApplicationAtURL:".


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2021-10-23 05:29:04 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2208

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

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:

Applescript:


-- 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

Last edited by KniazidisR (2021-10-25 08:15:06 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#4 2021-10-23 05:50:25 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6748

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

KniazidisR wrote:

since there is not a single AsObjC example using it on the Internet.



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).


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2021-10-24 08:58:54 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 929

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

@KniazidisR
Properly not a big deal but you could also do with NSWorkspace Class, maybe not any speed boost.

Applescript:

use framework "Foundation"
use scripting additions

set appID to "com.apple.Safari"
set workspace to current application's NSWorkspace's sharedWorkspace()
set appURL to (workspace's URLForApplicationWithBundleIdentifier:appID) as «class furl»


if you are the expert, who will you call if its not your imagination.

Offline

 

#6 2021-10-25 08:14:09 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2208

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

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.


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#7 2021-10-25 03:13:51 pm

One208
Member
Registered: 2021-03-15
Posts: 55

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

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

Offline

 

#8 2021-10-25 09:54:29 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2208

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

One208 wrote:

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:

Applescript:


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

Last edited by KniazidisR (2021-10-25 09:58:45 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#9 2021-10-26 01:11:15 am

One208
Member
Registered: 2021-03-15
Posts: 55

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

KniazidisR wrote:


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

Applescript:


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

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


Also in Handler

sharedWorkspace's openURLs:URLArray withApplicationAtURL:appURL configuration:(missing value) completionHandler:(missing value)



Here is my script, please help adapt, cheers

Applescript:

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

Offline

 

#10 2021-10-26 09:55:07 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2208

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

One208 wrote:



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



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:

Applescript:


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

Last edited by KniazidisR (2021-10-26 10:02:42 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#11 2021-10-26 12:12:01 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 929

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

Here is a simple script to do it with Safari.

First it use GUI scripting to get a private window and next it open a tab in that window with URL

Applescript:

(**
* Get the private window in Safari
*)


tell application "System Events" to tell application process "Safari"
   set frontmost to true
   tell menu bar 1 to tell menu bar item "File" to tell menu 1
       click menu item "New Private Window"
   end tell
end tell

(**
* Open a new tab with web address
*)

tell application "Safari" to tell window 1
   make new tab with properties {URL:"https://www.apple.com"}
   close tab 1
end tell

Last edited by Fredrik71 (2021-10-26 12:17:49 pm)


if you are the expert, who will you call if its not your imagination.

Offline

 

#12 2021-10-26 12:39:43 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 929

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

Or maybe it possible to pass argument in method @KniazidisR used.
I could see from this page it maybe are possible...
https://developer.apple.com/documentati … guage=objc

And call chrome with the argument: -incognito

I test this in command-line

open -a "Chromium" --args '-incognito'


Its a property so I guess this code maybe will work

Applescript:

set theValue to current application's NSArray's arrayWithArray:{"-incognito"}
set openConfiguration to current application's NSWorkspaceOpenConfiguration's configuration()
openConfiguration's setValue:theValue forKey:"arguments"

I'm still Mojave user so I couldn't test it on my machine.

Last edited by Fredrik71 (2021-10-26 01:08:52 pm)


if you are the expert, who will you call if its not your imagination.

Offline

 

#13 2021-10-27 12:56:45 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2208

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

@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:

Applescript:


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

Last edited by KniazidisR (2021-10-27 01:25:22 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#14 2021-10-27 01:16:35 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6748

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

KniazidisR wrote:

Then, I tried to open the incognito window using completion handler in the following way.



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 Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#15 2021-10-27 01:51:54 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2208

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

@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.

Applescript:


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!")

Last edited by KniazidisR (2021-10-27 02:11:16 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#16 2021-10-27 05:02:05 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6748

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

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#17 2021-10-27 08:22:01 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 929

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

KniazidisR wrote:

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


Test this code...

The code example was not from completionHandler it was from configuration

Applescript:

set theValue to current application's NSArray's arrayWithArray:{"-incognito"}
set openConfiguration to current application's NSWorkspaceOpenConfiguration's configuration()
openConfiguration's setValue:theValue forKey:"arguments"

sharedWorkspace's openURLs:URLArray withApplicationAtURL:appURL configuration:openConfiguration completionHandler:(missing value)

From the property arguments from the NSWorkspaceOpenConfiguration Class, Apple write...

The default value of this property is an empty array. When launching a new instance of an app, use this property to specify any additional launch arguments. The system inserts the app's path as the first element in the array.
If the calling process is sandboxed, the system ignores the value of this property.

Last edited by Fredrik71 (2021-10-27 08:25:33 am)


if you are the expert, who will you call if its not your imagination.

Offline

 

#18 2021-10-27 08:51:35 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2208

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

I tested exactly this way (with openConfiguration), @Fredrik71. Also, I tested with Terminal:

open -a "Google Chrome" --args -incognito

Both open normal window instead of incognito. And, if I understand the openConfiguration documentation correctly, passed to sandboxed application arguments is ignored.

Last edited by KniazidisR (2021-10-27 08:54:23 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#19 2021-10-27 09:18:52 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 929

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

The reason it doesn't work for you is: you type it wrong.

Try this: open -a "Google Chrome" --args '-incognito'

I'm not sure, the code you use was not a configuration parameter you had missing value there.

I will do some test in the weekend when I have access to Catalina.

Last edited by Fredrik71 (2021-10-27 09:24:15 am)


if you are the expert, who will you call if its not your imagination.

Offline

 

#20 2021-10-30 09:42:29 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 929

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

I did some tests in Catalina and Chromium do open 2 windows, (incognito) and (urls)
It means when the method are executed with the URLs it create a new window without incognito

Maybe not the best solution but its works, ps. I use Chromium but that is same for Chrome.

Applescript:

use framework "Foundation"
use framework "AppKit"
use scripting additions

its openURLsWithApplication({""}, "org.chromium.Chromium")

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
   set theValue to current application's NSArray's arrayWithArray:{"-incognito"}
   set openConfiguration to current application's NSWorkspaceOpenConfiguration's configuration()
   openConfiguration's setValue:theValue forKey:"arguments"
   sharedWorkspace's openURLs:URLArray withApplicationAtURL:appURL configuration:openConfiguration completionHandler:(missing value)
end openURLsWithApplication

tell application "Chromium"
   if (count of windows) > 1 then
       close window 1
   end if
end tell

-- This line will open a new tab in incognito window
tell application "Chromium" to tell front window
   make new tab with properties {URL:"https://www.apple.com"}
end tell


if you are the expert, who will you call if its not your imagination.

Offline

 

#21 2021-10-30 11:09:59 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 929

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

Here I other version more vanilla AS

To check if a tab view are in incognito you could point the URL in apple tab or google tab
to this URL:https://jsfiddle.net/2b1dk8oa/

Its should tell you if you are in incognito or not.

Applescript:

tell application "Chromium" to set bundleID to id

its openURLsWithBundleId({"https://www.apple.com", "https://www.google.com"}, bundleID)

on openURLsWithBundleId(theURLs, bundleID)
   do shell script "open -a \"Chromium\" --args '--incognito'"
   tell application id bundleID
       open location theURLs
       if (count of windows) > 1 then
           close window 1
       end if
   end tell
   
   tell application "Chromium" to tell front window
       repeat with anURL in theURLs
           set theURL to contents of anURL
           make new tab with properties {URL:theURL}
       end repeat
       close tab 1
   end tell
end openURLsWithBundleId

Last edited by Fredrik71 (2021-10-30 11:12:06 am)


if you are the expert, who will you call if its not your imagination.

Offline

 

#22 2021-10-30 11:21:46 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 929

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

More test show the problem was the bundleID...

So this is a short version that do not use bundleID

Applescript:

its openURLsInIncognito({"https://www.apple.com", "https://www.google.com"})

on openURLsInIncognito(theURLs)
   do shell script "open -a \"Chromium\" --args '--incognito'"
   tell application "Chromium" to tell front window
       repeat with anURL in theURLs
           make new tab with properties {URL:contents of anURL}
       end repeat
       close tab 1
   end tell
end openURLsInIncognito


if you are the expert, who will you call if its not your imagination.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)