Script: Selection row focus in new window

AppleScript

global bikeurl

tell front document of application "Bike"
	set bikeurl to URL of selection row
end tell

tell application "System Events"
	tell process "Bike"
		click menu item "New Window" of menu "File" of menu bar 1
	end tell
end tell

tell front document of application "Bike"
	open location bikeurl
end tell

Keyboard Maestro

2 Likes

Not sure if it’s just my system (macOS 11.7) but the focus seems to be opening in the existing (rather than new) window.

Perhaps a variant approach (to side-step any saving / versioning issues associated with a new document window) might be to create a new tab for the focus instead ?

Possibly something like:

Expand disclosure triangle to view JS source
(() => {
    "use strict";

    // New Bike app tab focused on selected row.

    // Rob Trew @2022
    // Ver 0.01

    // main :: IO ()
    const main = () => {
        const
            bike = Application("Bike"),
            doc = bike.documents.at(0);

        return doc.exists() ? (() => {
            const
                rows = doc.rows.where({
                    _and: [
                        {selected: true},
                        {_not: [{
                            name: ""
                        }]}
                    ]
                });

            return 0 < rows.length ? (() => {
                const selectedRowID = rows.at(0).id();

                return (
                    bike.activate(),
                    bundleMenuItemClicked(
                        "com.hogbaysoftware.Bike"
                    )(["File", "New Tab"]) ? (() => {
                            const
                                tabDoc = bike.windows.at(0)
                                .document();

                            return Boolean(
                                tabDoc.focusedRow = tabDoc
                                .rows.byId(selectedRowID)
                            );
                        })() : "Menu item not clicked."
                );
            })() : "No line of text selected.";
        })() : "No documents open in Bike";
    };

    // --------------------- GENERIC ---------------------

    // bundleMenuItemClicked :: String -> [String] -> IO Bool
    const bundleMenuItemClicked = bundleID =>
        menuPath => {
        // True if an item at the specified menu path
        // has been successfully clicked in an
        // application with the given bundle identifier.
            const intMenuPath = menuPath.length;

            return intMenuPath > 1 ? (() => {
                const
                    appProcs = Application("System Events")
                    .processes.where({
                        bundleIdentifier: bundleID
                    });

                return 0 < appProcs.length ? (
                    Application(bundleID)
                    .activate(),
                    menuPath.slice(1, -1).reduce(
                        (a, x) => a.menuItems[x].menus[x],
                        appProcs[0].menuBars[0].menus
                        .byName(menuPath[0])
                    )
                    .menuItems[menuPath[intMenuPath - 1]]
                    .click(),
                    true
                ) : false;
            })() : false;
        };

    // MAIN ---
    return main();
})();
2 Likes

I’m not sure what’s going on, it’s works well here. <macOS 12.6, Bike 1.5(87)>

2022-10-25 18.54.36.2022-10-25 18_56_32

Could be that I have just misunderstood the goal – you end up with the same document open in two different windows ?

( Perhaps more likely to be a timing issue here )

OK, works here if we add a brief delay to ensure that front document is already bound to the document of the new window.

tell application "Bike"
    activate
    set bikeurl to URL of selection row of front document
    
    tell application "System Events"
        tell process "Bike" to click menu item ¬
            "New Window" of menu "File" of menu bar 1
    end tell
    
    delay 0.1
    
    tell front document to open location bikeurl
end tell
2 Likes

Yes, it’s just timing issue, perhaps.

When I have a document like Moby Dick, and when I want to focus in Chapter 130 paragraph 4 in new window, it will save my time.

thank you!

2 Likes