Script: Selection row focus in new window

Update in 2022-12-17
improved the KM macro, now maybe it’s the fastest way.


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
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

Improved the KM macro.
Focus in new window.kmmacros

3 Likes

Thanks so much for this macro! At first I thought it would be irrelevant to my usage. But I’m testing it out today and find it very useful for a new long Bike document I’m working on. Really appreciate all those who amplify the power of Bike through macros!

2 Likes

Update
With the Bike v1.9 update, the previous macro is not working anymore.

Please change second action (Select Menu Item in Bike) to this:
Window ⇢ Duplicate Tab to New Window

or just download the new macro
Focus in new window 1.1.kmmacros

1 Like