Using AppleScript, how does one create a clickable link to a web site in a Numbers cell?

Solution 1:

Applescript and Numbers don't really offer access to text selection, but it looks like you're comfortable using the entire cell for your link. While UI scripting can solve such a problem, most spreadsheets offer functions of their own which applescript can access.

Numbers provides a hyperlink function which lets you create a link in a cell, and optionally, set the displayed text. I have included example code for both options and show the desired function.

The url must include the protocol (e.g. https) and be enclosed in quotes. Because you're setting it in an applescript, the quotes must be escaped.

set myURL to "https://help.apple.com/functions/mac/9.1/#/ffa596a1c7"
set myPageTitle to "Formulas and Functions Help" -- optional

tell application "Numbers"
    tell table 1 of sheet 1 of document 1
        
        set value of cell 1 of row 10 to "=hyperlink(\"" & myURL & "\")"
    --> HYPERLINK("https://help.apple.com/functions/mac/9.1/#/ffa596a1c7")

        set value of cell 1 of row 10 to "=hyperlink(\"" & myURL & "\",\"" & myPageTitle & "\")" -- with optional
    --> HYPERLINK("https://help.apple.com/functions/mac/9.1/#/ffa596a1c7","Formulas and Functions Help") -- contents of cell
        
        set alignment of cell 1 of row 10 to left
        
    end tell
end tell

I'll leave it to you to work out your of adding rows, etc….

Solution 2:

First add an activate command to the tell application "Numbers" block so Numbers is frontmost, then after the tell application "Numbers" block add the following:

Example AppleScript code:

tell application "System Events"
    keystroke return using option down
    keystroke "a" using command down
    keystroke "k" using command down
    keystroke return
end tell

This will set focus into the last cell modified, in this case the one with the URL,and select the text and make it a link.

Notes:

The example AppleScript code, shown below, and the code in the OP was tested in Script Editor under macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

  • 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.

If necessary, add a delay command between the keystroke commands, e.g.: delay 0.1

Another method is to make a handler and use it as in this example:

Example AppleScript code:

set myURL to "https://mywebpage.com"
tell application "Numbers"
    tell table 1 of sheet 1 of document 1
        add row below last row
        tell last row
            set value of first cell to current date
            set value of second cell to myURL
        end tell
    end tell
    activate
    my makeWebpageLink()
end tell

to makeWebpageLink()
    tell application "System Events"
        keystroke return using option down
        keystroke "a" using command down
        keystroke "k" using command down
        keystroke return
    end tell
end makeWebpageLink