Using a Non-Safari, Non-Chrome Browser

I've got a number of macros that do things in Chrome with the system clipboard: open a URL that I've copied, search Google for the text I've copied, and so on. But I'm considering switching to Brave as my main browser, and I'm having trouble adapting these macros to it. Obviously all the "Set Google Chrome URL" actions have to be replaced with "Open URL" actions -- but I keep getting "invalid URL" errors. Why should the "Open URL" action balk at having

https://www.google.com/search?q="%SystemClipboard%"

in its text field, when this worked just fine in a "Set Google Chrome URL" action? And how can I make this, and similar attempts, work? I have the feeling that if I can figure out the problem in this basic macro, I should be able to adapt the others...there's got to be something obvious that I'm missing?

Thanks for any suggestions!

Chrome and Safari are the only browsers that are directly supported by KM and scripting. You will be facing an uphill battle trying to automate any other browser.

The Open a URL action will open any valid, properly encoded, URL in the default browser. You probably need to encode the Clipboard when you use it in a URL.

Actually, Brave Browser is based on the Chromium web browser that Google Chrome is made from, and is therefore AppleScriptable. I haven't tested it, but it should have the same scripting features as Google Chrome.

Brave Browser AppleScript Dictionary

Brave Browser.app terminology

/Applications/Brave Browser.app

Standard Suite

Common classes and commands for all applications.

Commands

  • save -- Save an
    object.
    • reference -- the object to save, usually a document or window
    • [in file] -- The file in which to save the object.
    • [as text] -- The file type in which to save the data.
      Can be 'only html' or 'complete html', default is 'complete
      html'.
  • open -- Open a
    document.
    • list of file -- The file(s) to be opened.
  • close -- Close
    a window.
    • reference -- the document(s) or window(s) to close.
  • quit -- Quit the
    application.
  • count -- Return
    the number of elements of a particular class within an object.
    • reference -- the object whose elements are to be counted
    • [each type class] -- The class of objects to be counted.
    • Result: integer -- the number of elements
  • delete --
    Delete an object.
    • reference -- the object to delete
  • duplicate
    -- Copy object(s) and put the copies at a new location.
    • reference -- the object(s) to duplicate
    • [to location reference] -- The location for the new
      object(s).
    • [with properties record] -- Properties to be set in the
      new duplicated object(s).
    • Result: reference -- the duplicated object(s)
  • exists --
    Verify if an object exists.
    • anything -- the object in question
    • Result: boolean -- true if it exists, false if not
  • make -- Make a
    new object.
    • new type class -- The class of the new object.
    • [at location reference] -- The location at which to
      insert the object.
    • [with data anything] -- The initial contents of the
      object.
    • [with properties record] -- The initial values for
      properties of the object.
    • Result: reference -- to the new object
  • move -- Move
    object(s) to a new location.
    • reference -- the object(s) to move
    • to location reference -- The new location for the
      object(s).
    • Result: reference -- the moved object(s)
  • print -- Print
    an object.
    • reference -- The file(s) or document(s) to be printed.
  • set -- Set an
    object's data.
    • reference
    • to anything -- The new value.
  • get -- Get the
    data for an object.
    • reference
    • Result: anything

Classes

  • application
    -- The application's top-level scripting object.
    • Plural name:
      • applications
    • Properties:
      • name (r/o) text -- The name of the application.
      • frontmost (r/o) boolean -- Is this the frontmost
        (active) application?
      • version (r/o) text -- The version of the application.
      • bookmarks bar (r/o) bookmark
        folder
        -- The
        bookmarks bar bookmark folder.
      • other bookmarks (r/o) bookmark
        folder
        -- The
        other bookmarks bookmark folder.
    • Elements:
      • window -- by name,
        index, relative, range, test, id
      • bookmark
        folder
        -- by
        name, index, relative, range, test, id
  • window -- A
    window.
    • Plural name:
      • windows
    • Properties:
      • name (r/o) text -- The full title of the window.
      • id (r/o) integer -- The unique identifier of the
        window.
      • index integer -- The index of the window, ordered
        front to back.
      • bounds bounding rectangle -- The bounding rectangle of
        the window.
      • closeable (r/o) boolean -- Whether the window has a
        close box.
      • minimizable (r/o) boolean -- Whether the window can be
        minimized.
      • minimized boolean -- Whether the window is currently
        minimized.
      • resizable (r/o) boolean -- Whether the window can be
        resized.
      • visible boolean -- Whether the window is currently
        visible.
      • zoomable (r/o) boolean -- Whether the window can be
        zoomed.
      • zoomed boolean -- Whether the window is currently
        zoomed.
      • active tab (r/o) tab --
        Returns the currently selected tab
      • mode text -- Represents the mode of the window which
        can be 'normal' or 'incognito', can be set only once during
        creation of the window.
      • active tab index integer -- The index of the active
        tab.
    • Elements:
      • tab -- by name, index,
        relative, range, test, id

Chromium Suite

Common classes and commands for Chrome.

Commands

  • reload --
    Reload a tab.
    • reference -- The tab to execute the command in.
  • go back -- Go
    Back (If Possible).
    • reference -- The tab to execute the command in.
  • go forward
    -- Go Forward (If Possible).
    • reference -- The tab to execute the command in.
  • select all
    -- Select all.
    • reference -- The tab to execute the command in.
  • cut
    selection
    -- Cut selected text (If Possible).
    • reference -- The tab to execute the command in.
  • copy
    selection
    -- Copy text.
    • reference -- The tab to execute the command in.
  • paste
    selection
    -- Paste text (If Possible).
    • reference -- The tab to execute the command in.
  • undo -- Undo the
    last change.
    • reference -- The tab to execute the command in.
  • redo -- Redo the
    last change.
    • reference -- The tab to execute the command in.
  • stop -- Stop the
    current tab from loading.
    • reference -- The tab to execute the command in.
  • view
    source
    -- View the HTML source of the tab.
    • reference -- The tab to execute the command in.
  • execute --
    Execute a piece of javascript.
    • reference -- The tab to execute the command in.
    • javascript text -- The javascript code to execute.
    • Result: anything

Classes

  • tab -- A tab.
    • Plural name:
      • tabs
    • Properties:
      • id (r/o) integer -- Unique ID of the tab.
      • title (r/o) text -- The title of the tab.
      • URL text -- The url visible to the user.
      • loading (r/o) boolean -- Is loading?
  • bookmark
    folder
    -- A bookmarks folder that contains other bookmarks folder
    and bookmark items.
    • Plural name:
      • bookmark folders
    • Properties:
      • id (r/o) number -- Unique ID of the bookmark folder.
      • title text -- The title of the folder.
      • index (r/o) number -- Returns the index with respect
        to its parent bookmark folder
    • Elements:
  • bookmark
    item
    -- An item consists of an URL and the title of a bookmark
    • Plural name:
      • bookmark items
    • Properties:
      • id (r/o) integer -- Unique ID of the bookmark item.
      • title text -- The title of the bookmark item.
      • URL text -- The URL of the bookmark.
      • index (r/o) number -- Returns the index with respect
        to its parent bookmark folder

You probably need to encode the Clipboard when you use it in a URL.

Can you suggest how? Right now I'm filtering it with "Remove Styles," "Trim Whitespace," and "Percent Encode for URL," and yet if I copy a single word from a Word document, run those filters, and pass

https://www.google.com/search?q="%SystemClipboard%"

to the Open a URL action, it fails with "invalid URL" but doesn't give me any way of figuring out what the problem is. If I instead have KM paste the same text string into the address bar and type Return, it works; the search is performed. It makes no sense to me that this action would succeed when the other fails... (ETA: and when it worked fine, even without any filtering or encoding, with a "Set Google Chrome URL" action!)

Hah, got it! I found that it worked if I removed the quotation marks around %SystemClipboard% in the URL. But if I have more than one word, I want to be searching for the phrase as a phrase, so I do want the quotation marks in the URL. So I set a variable to "%SystemClipboard%" (the clipboard wrapped in quotation marks), filtered the variable with Percent Encode for URL, and then passed the Open URL action the text

https://www.google.com/search?q=%Variable%clipboardtext%

and it worked!

Moral: the Set Google Chrome URL action can handle characters in the URL it's passed that the Open URL action can't. I didn't expect that plain-text quotation marks would be a problem, but now I know!

Thanks for sharing that. I was not aware of the Brave Browser.

It looks very interesting.

@peternlewis, any chance that KM might provide direct support for Brave Browser?
I'm particularly interested in the behaviour of the "FrontBrowser" actions and tokens.

I was not aware of the Brave Browser. It looks very interesting.

There are some unpleasant things about it (its optional ad exchange program and monetization thereof look sleazy at a quick view, but it's optional), but boy howdy is it ever fast. So I'm giving it a try, and in the process reworking my browser macros to work with any browser, not just my previous default of Chrome. And learning things along the way :sweat_smile: (It would certainly be great if "Front Browser" actually meant front browser, not just "whichever of Chrome and Safari was last used"!)

Probably not, not unless it becomes more popular. Although you should be able to use the existing preferences for Google browser settings to point at it.

You could also just do it via AppleScript:

tell application "Brave Browser" to open location "https://www.google.com/search?q=\"Mary Had A Little Lamb\""

or, using the clipboard content:

tell application "Brave Browser" to open location "https://www.google.com/search?q=\"" & (the clipboard as text) & "\""

Oh, nice! I'm always glad to learn more Applescript techniques.

Peter, I don't know what you mean by this. Could you please give us an example?

From the Preferences : Other Hidden Preferences user manual section:

You can set the AppleScript name of the Safari application, as well as the name of the Safari browser shown in applications:

defaults write com.stairways.keyboardmaestro.engine AppleScriptSafariBundleID "com.apple.SafariTechnologyPreview"
defaults write com.stairways.keyboardmaestro.engine BrowserSafariName "Safari Tech"

or

defaults write com.stairways.keyboardmaestro.engine AppleScriptSafariName "com.apple.Safari"

Similarly for Google Chrome:

defaults write com.stairways.keyboardmaestro.engine AppleScriptGoogleChromeBundleID "com.google.Chrome.canary"
defaults write com.stairways.keyboardmaestro.engine BrowserGoogleChromeName "Chrome Canary"

The AppleScript name is used in AppleScript's which control the respective browser in the form of tell app “”. Note that the Safari browser must still behave like Safari, and the Chrome browser must behave like Chrome, or the actions will not work. Do not bother trying to set them to Firefox or something like that as it will not work. All this allows you to do is select a specific version of Safari or Chrome respectively.

Peter, thanks for sharing/clarifying.
That at least gives any one who want to ONLY run Brave an option to do so, but at the expense of not having access to Google Chrome.

AFAIK, the scripting model of Brave Browser is identical to Google Chrome. If that is the case, I would hope the effort to change KM to support Brave would be minimal.

Please give this a closer look. There are many who have switched from Safari to Google Chrome because Chrome is faster, better UI, and more extensible (more browser extensions). However, many don't switch to Chrome because of Google's bad reputation on privacy.

Brave is

  • even faster than Google Chrome,
  • has the same scripting model
  • supports most, if not all, of Chrome extensions
  • but has NONE of the privacy issues of Google

Thanks.

What is the bundle ID of Brave?

I hadn’t heard of Brave before; it looks interesting, and if it’s reasonable for KBM to support it, that’d be great.

But if so, what about all the other Chromium-based browsers that folk use? Vivaldi is quite popular, for example (I use it), Edge, and Chromium itself? I think there are a bunch of others, too. And is Opera Chromium-based now? It’d be lovely if all these were supported, but I worry that it might be a time drain on Peter — on-goingly.

"com.brave.Browser"

Peter, when I try to read these defaults, I get an error:

$ defaults read com.stairways.keyboardmaestro.engine AppleScriptGoogleChromeBundleID
2019-05-09 12:45:26.600 defaults[1769:64516] 
The domain/default pair of (com.stairways.keyboardmaestro.engine, AppleScriptGoogleChromeBundleID) does not exist

In fact, when I just do a read without a key to get all defaults, it works, but there is no key for "AppleScriptGoogleChromeBundleID"

So if I set the defaults for Brave Browser, how do I restore to use Google Chrome?

I will add it as a potential match for the front browser tokens - as long as it behaves the same as Google Chrome, it should work. But I cannot test this.

1 Like

Like most preferences, they are unset normally, and when absent they default to their expected normal default behaviour. To delete a key, use:

defaults delete com.stairways.keyboardmaestro.engine AppleScriptGoogleChromeBundleID

Note that like read will read everything if you omit the key, delete will delete everything if you omit the key (I believe), so one should use caution when using defaults delete.

2 Likes

Unfortunately, this has no impact on the FrontBrowser Actions and Tokens.

Is there another hidden pref that will allow Brave Browser to be included in the FrontBrowser Actions and Tokens?

Does this include FrontBrowser Actions, like Javascript in FrontBrowser?

FYI, right after you provided us with the very useful FrontBrowser Actions/Tokens, I converted all of my Macros to take advantage of them, so my Macros work equally well with both Safari and Chrome.

Now, I'd very much like for Brave Browser to be included as a FrontBrowser.

Thanks.