1
votes

I'm trying to write my first OS X script using JavaScript instead of AppleScript, and am running into some issues. The first part of this is to get the URL from a tab in Chrome, so that I can do some more work on it (e.g., create a new reading list item in safari). I've tried the following code:

var safari = Application('Safari');
var chrome = Application('Google Chrome');
chrome.activate();
var url = chrome.windows[0].tabs[0].get('url'); // This returns an ObjectSpecifier
safari.addReadingListItem(url, {andPreviewText: 'Added from Chrome'});

This fails, with error: "Error -1700: Can't convert types." I need a string, but this is still an Object specifier. So I guess I don't get a string back from .get('url')

I haven't had any luck finding examples of pulling values FROM the scripting environment, but I've seen a few about putting new tabs into a browser. Any help with this is appreciated!

2

2 Answers

3
votes

To get the URL of the active tab, do:

Application('Chrome').windows[0].activeTab.url()
-1
votes

After more digging, I tracked down what you're supposed to do. Documented here: Apple Documentation

Properties are returned as object specifiers, to get a value from an object specifier, you call it like a function:

chrome.windows[0].tabs[0].url(); // the string value of the URL

Settable properties can be set using the =. This does not apply to read-only values (which are marked as such in the Library Panel in script editor:

chrome.windows[0].tabs[0].url = "http://example.com";