MozillaZine

Addressing a specific window in Applescript

Talk about the native Mac OS X browser.

Moderator: Camino Developers

Gena-Left
 
Posts: 10
Joined: July 26th, 2012, 5:00 pm

Post Posted July 31st, 2012, 10:58 am

Is there a way to communicate with a specific browser window by using its window specific id? I've only managed to use the index number, but have written a script that seeks the specified id through all browser windows and returns its index number, but I don't know if this is very stable in the long run.

If you want more info I can post the script or other specs of my application.

cflawson

User avatar
 
Posts: 4721
Joined: December 26th, 2004, 2:54 pm
Location: Flying over your house in a red, white, and blue jet

Post Posted July 31st, 2012, 11:38 am

To see if it's long-term stable, I'd suggest trying this:

Create three windows with three unique URLs and only one tab in each, remembering the order in which you opened them. For the sake of this exercise, call them A, B, and C. Look up the index of B, then open a new window D and close A. See if B's index changes. Look at D's index and then close B and C, and see if D's index changes.

I don't believe windows currently have an AppleScript-accessible ID property, but it's certainly something that *could* be added. Can I ask why you need it, though? A good use-case always helps.

cl

Gena-Left
 
Posts: 10
Joined: July 26th, 2012, 5:00 pm

Post Posted July 31st, 2012, 12:56 pm

Thanks for the info, that's pretty much how I've been testing it so far, by opening, closing, minimizing and rearranging windows, and posting the index number in a Growl message with some other data.

I need to scan for updates on a web page regularly, similar to what the application Changes Meter or Firefox plugin Update Scanner are doing. The page needs to be logged into, so Changes Meter only loads the login page and doesn't report the updates.

Currently the script is a standalone application that reads the URL from the current tab of Camino and opens it in a new browser for checking. It seems to be working quite well with pages that update frequently or on every load, for example with lines such as "Page generated in 0,023 seconds". Alas the page I'm tracking doesn't update very often, so I haven't yet been able to see if the script actually has some practical use for me...

It's my first script, so I'm trying to add as many useful functions as I can to learn about scripting. For instance I'm thinking of making the application run in the menu bar and show notifications in a drop down menu instead of flooding the screen with sticky Growl messages.

I'm still troubleshooting some issues with the core application before moving on, but for now I think the window handling routine is fine and the script reacts if the browser window is closed and reloads the page in a new window to continue checking. For a workaround to opening the page in a new browser I've unchecked "Links opened by other applications" in Tabs preferences, which is not a stable solution if the setting is set on. If there's another way to do this I could develop the script to track multiple tabs in one window and perhaps have one tab show statistics, and create a toolbar script to add a page to be checked.

Uncle Asad
Camino Developer

User avatar
 
Posts: 3957
Joined: July 24th, 2004, 1:38 pm
Location: بين العالمين

Post Posted July 31st, 2012, 1:34 pm

Gena-Left wrote:Is there a way to communicate with a specific browser window by using its window specific id?

Code: Select all
<some-browser-window-property> of browser window id <window-number>
should work. "browser window" inherits every normal property of "window"; if it doesn't work for some "window" property, that's likely a bug.

Gena-Left wrote:For a workaround to opening the page in a new browser I've unchecked "Links opened by other applications" in Tabs preferences, which is not a stable solution if the setting is set on. If there's another way to do this

I assume you're referring to the dreaded bug 395712 problem? Unfortunately, there's no* solution or workaround to that problem yet. We have a number of ideas/possible solutions to the problem, but no-one's had time to dig in to trying to implement them :-(

* If you're not using a toolbar script, you could use GUI scripting to send Cmd-N. Edit: Actually, you can use this in a toolbar script, too, since it's just sending keystrokes, not trying accessing any of Camino's objects. You'd need to delay slightly (to allow the command to be created and processed) before getting the window ID and setting the URL, but if you don't mind enabling GUI scripting, this should be a useable workaround for your use-case.

thom-22 is our AppleScript guru; if she's around, she might think of other options that I've forgotten.
Mac OS X 10.3.9 • PowerBook G4 17" 1.33 GHz | Mac OS X 10.5.x • MacBook Pro 15" 2.2 GHz
Snow7's Camino Forum FAQSearch the Forum  Camino. HelpTroubleshoot Camino

Gena-Left
 
Posts: 10
Joined: July 26th, 2012, 5:00 pm

Post Posted July 31st, 2012, 2:49 pm

So easy... Thanks, it got rid of some thirty lines of code in the script! Although I'm quite proud of coming up with my solution I think I can rest assured this method won't fail under normal circumstances.

I might use GUI scripting to try out how the application could function, thanks for that tip as well. I also have a delay in case the window is closed and needs to be reopened, so I'm wondering if there would be a way to wait until the page has loaded? Actually I checked now that in the script
Code: Select all
reload tab 1 of browser window id <window-number>
set <tab_source> to text of tab 1 of browser window id <window-number>

the text retrieved is still from an earlier load of the page, which is of course not good for my purposes. Is my only chance to add a safety delay to get the latest update?

Uncle Asad
Camino Developer

User avatar
 
Posts: 3957
Joined: July 24th, 2004, 1:38 pm
Location: بين العالمين

Post Posted July 31st, 2012, 8:06 pm

Gena-Left wrote:I'm wondering if there would be a way to wait until the page has loaded? Actually I checked now that in the script
Code: Select all
reload tab 1 of browser window id <window-number>
set <tab_source> to text of tab 1 of browser window id <window-number>

the text retrieved is still from an earlier load of the page, which is of course not good for my purposes. Is my only chance to add a safety delay to get the latest update?

Yeah, a safety delay is your best bet; we don't have any way via AppleScript to tell when a page has loaded—and I don't want to even contemplate what sort of plumbing into Gecko would be required to get that kind of information :shock: :-(
Mac OS X 10.3.9 • PowerBook G4 17" 1.33 GHz | Mac OS X 10.5.x • MacBook Pro 15" 2.2 GHz
Snow7's Camino Forum FAQSearch the Forum  Camino. HelpTroubleshoot Camino

Gena-Left
 
Posts: 10
Joined: July 26th, 2012, 5:00 pm

Post Posted August 22nd, 2012, 4:18 am

Thank you for all the help.

Although the script works fine now as is, I'm still pondering how to develop it further by having multiple pages being tracked in one window.

One solution I was thinking could be to store the pages in a Tab Group bookmark and open that in a new window, but I haven't figured out exactly how Camino's bookmark property works in Applescript. To begin with, is it possible to open a bookmark via Applescript? And further, is it possible to add, remove and edit bookmarks? This sounds like a security issue, though.

I could hardly find anything on this through Google, so any advice would be greatly appreciated!

Another option could be to create a local html with Javascript to open the tabs as popups, but I haven't figured out a way to allow popups for local pages. In Applescript I do this by using a referrer that's allowed to open popups, but I don't think there is a similar Javascript solution?

Uncle Asad
Camino Developer

User avatar
 
Posts: 3957
Joined: July 24th, 2004, 1:38 pm
Location: بين العالمين

Post Posted August 22nd, 2012, 10:34 pm

Gena-Left wrote:To begin with, is it possible to open a bookmark via Applescript?

Not in a way that would help you, no. Opening a bookmark involves getting the bookmark's URL and then using "open location" or setting the URL property of a tab/window to that URL. In recent years I've thought we should really have some sort of "open bookmark" command that behaves similarly to clicking on a bookmark, but I've never had time to see about implementing it :-(

Gena-Left wrote:And further, is it possible to add, remove and edit bookmarks? This sounds like a security issue, though.

Yes, "make"/"delete"/"set" all work properly with bookmarks. (Camino's bookmarks are nice Cocoa objects in relatively sane Cocoa classes, so, unlike lots of other parts of our UI, they script nicely, normally, and easily. Side effect: less need to write documentation about scripting bookmarks.)
Code: Select all
make new bookmark with properties {name: "foo", url: "http://example.com", description: "This bookmark added via AppleScript", shortcut: "foo"} at end of bookmark items of bookmark bar collection

set name of bookmark "foo" of bookmark bar collection to "bar"

delete last bookmark of bookmark bar collection

As far as security goes, if someone has local access to your Mac (needed to add/edit/remove bookmarks by AppleScript), you're already in trouble (and there are a lot worse things they can do).

Tab groups are reflected into AppleScript as normal bookmark folders, though; AppleScript doesn't have access to any of the behind-the-scenes stuff we use to make tab groups act more like bookmarks.

Gena-Left wrote:Another option could be to create a local html with Javascript to open the tabs as popups, but I haven't figured out a way to allow popups for local pages.

Yeah, Gecko broke the ability to set permissions/exceptions for pop-ups, cookies, etc. between Gecko 1.8.1 and Gecko 1.9.0 (aka Camino 1.6.x and Camino 2.x) and they are in no hurry to fix that regression.

So I'm afraid I don't have any good ideas for you this time around :-(
Mac OS X 10.3.9 • PowerBook G4 17" 1.33 GHz | Mac OS X 10.5.x • MacBook Pro 15" 2.2 GHz
Snow7's Camino Forum FAQSearch the Forum  Camino. HelpTroubleshoot Camino

Gena-Left
 
Posts: 10
Joined: July 26th, 2012, 5:00 pm

Post Posted August 23rd, 2012, 3:13 pm

So it seems I'll just have to wait until a script can make new windows and tabs...

I keep brainstorming on the app's functionality when I can spare the sleepless nights and had the idea to write a local file with frames, one for a status bar and one for the page being monitored. However, when I get the text from this page, I seem to receive the text of the local file containing the frame set, which is obviously nothing. If this is indeed so, I suppose it should be fixed, since it would mean that text can not be received from any page with frames! If there is a way to get the text of a specific frame, though, it would be of great help for me.

Another idea I tried to implement includes the same frame set, but with a local copy of the page being monitored. Instead of having a safety delay as discussed earlier, I've tried setting the url to a local file with some text, e.g. "Loading...", then while the actual page is loading from the site the script waits until the text Camino gives from the page is not "Loading..." The source is then saved into a file to have the page in the frame set correspond the one loaded instead of another reload.

It appears to be working with some success, although once in a while the text received is again an empty string. The purpose of this toil is for the time being not much more than to distinguish the browser window from others, so it won't be used for browsing other pages that would result in an unwanted reload when one is due. Partly it's just to have a little eye candy, a happy icon on the side of the page, but since the whole purpose of the app is actually not to keep looking at the page all the time, you might wonder what it's all worth...

I think I'm trying to make Camino's Applescript support do something it's not designed to do, but I hope some of the issues I'm encountering are noteworthy for following upgrades.

Incidentally the apps and plugins I tried before deciding to write my own weren't doing such a superb job either, so I guess I should be content with one that does just what I need it to do rather reliably!

Return to Camino


Who is online

Users browsing this forum: No registered users and 1 guest