Electron Clipboard not working with simulated Copy (command+c) event


#1

Hi everyone :slight_smile:

I am building an app that uses clipboard functionality.

One of the functionalities of the clipboard, is that when the user copies text from within the browser, I want to fetch the url of the page the text was copied from.

I do this by using robotjs, and simulating the
Command+L and then Command+C

This successfully copies the URL to the OS keyboard (I have checked with Command+V), but when I try to read it via the electron clipboard, I get a blank string, as if nothing was copied.

** I have tried this with removing the clipboard.clear(), and still get the same result.

I have been stuck on this for several days, and would be glad if someone could point me in the right direction.

Thanks! :slight_smile:


#2

It would probably be much easier for you to use JXA / AppleScript to get the URL.

var {Application} = require('jxa')
var safari = Application('Safari')
var curLoc = safari.windows[0].currentTab.URL

This also gives you the capability of using google chrome:

var {Application} = require('jxa')
var chrome = Application('Google Chrome')
var curLoc = chrome.windows[0].currentTab.url //untested but should work

Using keyboard simulation should always be avoided if possible :). You will need to install a JXA Package of course. In general though, if you are looking for automation of Mac applications, use JXA / AppleScript. You will save yourself a hell of a lot of time.


#3

Hey @sancarn

First off, thanks for taking the time to post an answer :slight_smile:

The general direction you’re pointing me in sounds a lot cleaner than the key simulation hack I was trying earlier.

I have installed jxa and tried running the above code, but it doesn’t work.

The documentation is sparse (nearly nothing on communicating with Chrome :frowning: )

Do you have any resources or examples of using this API successfully?

Tried digging in the result but couldn’t make much out of it.

Appreciate the help!


#4

I wouldn’t be surprised if the Chrome example doesn’t work, that was mostly guess work :stuck_out_tongue:.

Some things that would probably help:

In terminal execute the command osascript -l JavaScript -i. This will enable the JXA REPL. From here you can do some exploring…

The supporting documentation for Google Chrome and Safari can be found in "Script Editor.app">File>"Open Dictionary...">Safari/Google Chrome. Make sure to change Applescript Dropdown to JavaScript. Here you can see all the properties and methods of both the applications.

Here’s a simple script that works directly in the REPL:

var safari = Application("Safari")
console.log(safari.windows[0].currentTab().url())

One thing to note with JXA is it’s got some weird differences to javascript. For example, to retrieve the url property of the tab object you would need to do tab.url(). So for example:

var safari = Application("Safari")
console.log(safari.windows[0].currentTab().url)

returns the Object Specifier for the URL Object rather than it’s value. Ultimately properties have to be called like methods.

Using JXA from node maybe different. I have no experience with that.


#5

@sancarn - This was super helpful!

Thanks so much for taking the time. It’s working now :slight_smile: !


#6

Great! :slight_smile:

Some other info which may be useful to you:

Application('System Events') is very useful for getting things about windows in general, though this gets deeper into Mac’s UI Automation. The advantage is, however, that you can access almost anything in any window with mac’s UI Automation framework.

var system = Application('System Events')
system.processes() //returns an array of processes
system.processes[0].name() //returns name of first process
var se_safari = system.processes['Safari'] // returns safari process
var frontwindow = se_safari.windows[0] //returns the most recently active safari window
var UIElements = frontwindow.entireContents() // returns all UI elements of last active safari window.
var wndAttribs = frontwindow.attributes() //get all attributes for UI Element
var wndFrame = frontwindow.attributes["AXFrame"].value() //get bounding box of window element
var wndActions = frontwindow.actions() //get all actions for UI Element
frontwindow.actions["AXRaise"].perform() //perform AXRaise action

To use the above commands it is important to note that you will need to allow accessibility access for the application you are running the commands from. To Do this go to “System Preferences”>“Security & Privacy”>Privacy>Accessibility. In “Allow the apps below to control your computer”, check Terminal (I suppose NodeJS will have to be checked/added?)


#7

Once again, thank you for your detailed response. Much appreciated :slight_smile: