Jump to content
zuchmir

[SOLVED] Launch Google Chrome bookmark using JXA

Recommended Posts

Hi all! First post here.

 

A workflow of mine involves having a script launch a bookmarklet in Google Chrome. I can, it is true, put the JS code to be executed directly into my script, but that means changing my code in 2 places every time I change something.

 

So with Applescript, opening a bookmark in Chrome is trivial:

tell application "Google Chrome"     
	if not (exists window 1) then reopen
	set coef to URL of bookmark item "Coef" of bookmark folder "Bookmarks Bar"
	open location coef
end tell

 

But when trying this in JXA, everything goes to hell.


I've tried countless variations on the following code,

var app = Application('com.google.Chrome')
var frontWindow = app.windows[0]
var myBM = app.bookmarkFolders.whose({title: "Bookmarks Bar"})
var myBM = app.bookmarkFolders["Bookmarks Bar"]
var myBM = app.bookmarkFolders.title("Bookmarks Bar")
//etc ad nauseam

and cannot for the life of me get a handle on a bookmark folder, let alone a bookmark item. bookmarkFolder vs. bookmarkFolders, nothing helped.

The last line actually got me an array with the names of the two top-level folders, but that's the closest I got to anything.

 

Anybody has an idea how this gets done? Thanks!

 

 

Share this post


Link to post
var app = Application('com.google.Chrome')
var frontWindow = app.windows[0]
var BM = app.bookmarkFolders["Bookmarks Bar"].bookmarkItems["Coef"]
frontWindow.tabs[0].url = BM.url()

 

Edited by CJK

Share this post


Link to post
8 hours ago, CJK said:

var app = Application('com.google.Chrome')
var frontWindow = app.windows[0]
var BM = app.bookmarkFolders["Bookmarks Bar"].bookmarkItems["Coef"]
frontWindow.tabs[0].url = BM.url()

 

 

 

Yep, tried that, doesn't work. Here's what I get:

 

 

image.png.ff3cc41715d40509d37ac0549c1660c4.png

 

 

Splitting the action into 2 - first getting the folder, then the bookmark item - changed little:

 

 

image.png.2651a34080cf27cacc580572ab2ba4e7.png

 

 

Finally, trying to log the title (or id) of either the folder or the item, yielded the same error:

 

 

image.png.92f517269e190125703342bf92f34014.png

 

 

A couple of observations:

1. The script doesn't go off track until the object is accessed, either with console.log or when trying to use the URL property.

2. Changing the name of the folder to gibberish changed nothing.

 

Thanks all again!

Edited by zuchmir

Share this post


Link to post

@zuchmir The JXA code is correct.  I think you have a problem elsewhere.  It appears, for one, as if you might have multiple Google Chromes.


What's the result if you run this code:


Application('com.google.Chrome').properties()


Here's the output you should expect:

 

{"bookmarksBar":Application("Google Chrome").bookmarkFolders.byId(1), "frontmost":false, "otherBookmarks":Application("Google Chrome").bookmarkFolders.byId(2), "pcls":"application", "title":"Google Chrome", "version":"68.0.3440.106"}

 

Edited by CJK

Share this post


Link to post

From this, can you then access the bookmarks bar with Application("Google Chrome").bookmarkFolders.byId(1).bookmarkItems() ?


Otherwise, I don't know what's causing the issue.


Have you tried it in AppleScript instead of JXA ?  Yes, you have, I've just re-read your opening post.

Edited by CJK

Share this post


Link to post

So a couple things.

 

Firstly, even if this had worked, wouldn't be of much help - I'd need to be working on identifying the ID of individual folders and bookmarks. At that point, I may as well hardcode the URL right into the script.

 

In any event, even this didn't work.

Because the AppleScript does work, swimmingly, I could easily find the id of the particular bookmark I am working with, and tried it out in JXA. Didn't get me far:

 

image.png.83c9b4ab4a39b58f98394d7a34c6dfd9.png

 

image.png.6d2614770b1820dea7a9d43768586e7b.png

 

Same when I tried 

var BMI = BMF.bookmarkItem.byId(457)

It's definitely a Chrome issue. Possibly because the name of the bookmark is implemented not as name but as title, which gets no special treatment in JXA as name and id do?

Edited by zuchmir

Share this post


Link to post
44 minutes ago, zuchmir said:

It's definitely a Chrome issue. Possibly because the name of the bookmark is implemented not as name but as title, which gets no special treatment in JXA as name and id do?

 

That would produce a consistent error similar to yours across all systems.  As I receive no such errors, it's more likely it's an issue specific to your system, and not Chrome in general.  It might be something really obvious, such as tweaking your JavaScript settings in Chrome preferences, and making sure it's enabled for automation or Apple events, etc.

Share this post


Link to post
16 minutes ago, CJK said:

 

That would produce a consistent error similar to yours across all systems.  As I receive no such errors, it's more likely it's an issue specific to your system, and not Chrome in general.  It might be something really obvious, such as tweaking your JavaScript settings in Chrome preferences, and making sure it's enabled for automation or Apple events, etc.

 

Do you mean to say that the code you posted before did in fact work by you?

JXA does work, before executing Javascript from an Applescript (or JXA for that matter) I had to authorize it (View > Developer > Allow JavaScript from Apple Events), only the bookmarks makes problems. If that exact code worked for you, that would be truly interesting.

 

This is the closest I've gotten to anything. Invoking the app object's bookmarkFolders.title() yields an array with the names of both top-level bookmark folders.

What to make of that, I'm not sure...

 

image.png.6021e57bde6d72c1dac18335cea9968e.png

Edited by zuchmir

Share this post


Link to post

Yes, everything I've supplied so far works on my system.  You should try to stop using console.log() to evaluate certain object types, because that can be the cause of some of the type conversion errors you saw earlier.  You can evaluate one object's true value and type simply by making that object the last line of the script, e.g. app.bookmarkFolders().

 

image.jpeg.a066f768baf8239283081b10a3769e06.jpegimage.jpeg.02c5544f038bf33fbb318914c5235e28.jpeg

Share this post


Link to post

Bingo! Worked perfect!

I didn't see that console.log had been making problems whenever had worked. What I didn't know was that:

  1. Even though in the dictionary the name property is explicitly defined as "title", byName() still works;
  2. that parentheses are required to access the URL -  bookmark.url( ) . Why should parentheses be necessary?

Anyways, regardless of the "why", works like a charm. Thanks!

Edited by zuchmir

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...