Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How can one extract a URL from a hyperlink pasted from the web?

I have a list of hyperlinks, but there is no direct way to extract the URLs from them. I want to take a column of hyperlinks and output a column of URLs.


There's a way to do it in Excel using VBA Script (https://spreadsheetplanet.com/extract-url-from-hyperlinks-excel/).


I saw this older discussion: https://discussions.apple.com/thread/7151156 but it points to a script that is no longer available and includes another script at the bottom that seems to have some typos.


As it's been more that five years since that thread anyways, I was wondering if there was an easier way to do this, or if not, if someone could point me towards a script I can use.


Thanks.

Posted on Oct 23, 2021 11:41 AM

Reply
Question marked as Best reply

Posted on Oct 24, 2021 8:51 AM

Here's an updated version of my "short" script that got mangled by the forum software in the older thread:


-- select cells, command-c, run script, click "upper left" destination cell 1x, command-v to paste urls
set the clipboard to (the clipboard as «class RTF »)
set theHTML to do shell script "pbpaste -Prefer rtf | textutil -convert html -stdin -stdout"
set htmlParts to splitText(theHTML, "<a href=\"http://")

set pasteStr to ""
repeat with i from 2 to (htmlParts's length)
	set pasteStr to pasteStr & "https://" & htmlParts's item i's first word & return
end repeat

set the clipboard to pasteStr's text 1 thru -2
return pasteStr

to splitText(t, d)
	set text item delimiters to d
	t's text items
end splitText


Make sure Script Editor is listed and checked at System Preferences > Security & Privacy > Privacy > Accessibility.


To use:

  1. Copy-paste the script above into Script Editor (in Applications > Utilities)
  2. In Numbers select the cells with the links (A2:A4 in the example below)
  3. Type command-c to copy their contents to the clipboard
  4. Click the triangle <run> button in Script Editor
  5. Click once in the top destination cell in Numbers (B2 in the example below)
  6. Command-v to paste.


Result:

If you do this often you can consider putting the script in the script menu.



SG

3 replies
Question marked as Best reply

Oct 24, 2021 8:51 AM in response to Philip Trauring

Here's an updated version of my "short" script that got mangled by the forum software in the older thread:


-- select cells, command-c, run script, click "upper left" destination cell 1x, command-v to paste urls
set the clipboard to (the clipboard as «class RTF »)
set theHTML to do shell script "pbpaste -Prefer rtf | textutil -convert html -stdin -stdout"
set htmlParts to splitText(theHTML, "<a href=\"http://")

set pasteStr to ""
repeat with i from 2 to (htmlParts's length)
	set pasteStr to pasteStr & "https://" & htmlParts's item i's first word & return
end repeat

set the clipboard to pasteStr's text 1 thru -2
return pasteStr

to splitText(t, d)
	set text item delimiters to d
	t's text items
end splitText


Make sure Script Editor is listed and checked at System Preferences > Security & Privacy > Privacy > Accessibility.


To use:

  1. Copy-paste the script above into Script Editor (in Applications > Utilities)
  2. In Numbers select the cells with the links (A2:A4 in the example below)
  3. Type command-c to copy their contents to the clipboard
  4. Click the triangle <run> button in Script Editor
  5. Click once in the top destination cell in Numbers (B2 in the example below)
  6. Command-v to paste.


Result:

If you do this often you can consider putting the script in the script menu.



SG

Oct 23, 2021 4:29 PM in response to Philip Trauring

I suspect there are slightly less convoluted ways of doing this, but there's currently a bug in the AppleScript implementation of 'selection range' which makes things a bit more cumbersome, and it's late on a Saturday night...


Here's a column of hyperlinks, first formula displayed:



Select all the hyperlinks:



Run this AppleScript:


use scripting additions -- and why not?
set AppleScript's text item delimiters to "" -- clear existing tids
tell application "Numbers"
	tell document 1 to tell active sheet to set activeTable to first table whose selection range's class is range -- define the working area
	tell activeTable -- get information from the active table
		set theCells to cells of selection range -- get a list of the selected cells
		set theColumn to column of selection range
		set nuColumn to (add column after item 1 of theColumn) -- create a new column in which to place the URLS
		set nuColumnName to name of nuColumn
	end tell
end tell
repeat with eachCell in theCells
	tell application "Numbers" to set theFormula to formula of eachCell -- get the formula as a string, parse the string to extract the URL
	set AppleScript's text item delimiters to "=HYPERLINK(\"" -- front chunk
	set trimmedHyperlink1 to text item 2 of theFormula -- chop off the front chunk
	set AppleScript's text item delimiters to "\"," -- first characters after clean URL
	set trimmedHyperlink2 to text item 1 of trimmedHyperlink1 -- chop off the back, result is the clean URL
	tell application "Numbers"
		tell activeTable --find address of cell to right of original cell
			set cellName to (name of eachCell)
			set nuCellRow to character 2 of cellName
			set nuCellAddress to nuColumnName & nuCellRow
set value of cell nuCellAddress to trimmedHyperlink2 --place trimmed URL in cell to right
		end tell
	end tell
end repeat


This gives me this:



It will almost certainly throw errors if you select cells without hyperlinks, or select multiple columns. It will probably throw other errors that I can't envisage. But give it a try, and post back with screen shots if you have problems.


EDIT: minor code simplification



Oct 24, 2021 12:39 AM in response to Philip Trauring

ADD: If the hyperlinks are just pasted text, rather than HYPERLINK() formulae, it gets a bit simpler:


use scripting additions
tell application "Numbers"
	activate
	tell document 1 to tell active sheet to set activeTable to first table whose selection range's class is range -- define our working area
	tell activeTable
		set theCells to cells of selection range -- get a list of the selected cells
		set theColumn to column of selection range
		set nuColumn to (add column after item 1 of theColumn) -- create a new column in which to place the URLS
		set nuColumnName to name of nuColumn
	end tell
	repeat with eachCell in theCells
		set nuCellContent to "" --non-matching and empty cells are skipped
		set cellContent to value of eachCell -- get the cell value as a string, parse the string to extract the URL
		if cellContent begins with "https:" then set nuCellContent to text 9 thru -1 of cellContent
		if cellContent begins with "http:" then set nuCellContent to text 8 thru -1 of cellContent
		tell activeTable --find address of cell to right of original cell
			set cellName to (name of eachCell)
			set nuCellRow to character 2 of cellName
			set nuCellAddress to nuColumnName & nuCellRow
			set value of cell nuCellAddress to nuCellContent --place trimmed URL in cell to right
		end tell
	end repeat
end tell


Again select the column containing the hyperlinks before running the script. This deals with secure and non-secure URLs, anything else leaves an empty cell in the new column.

How can one extract a URL from a hyperlink pasted from the web?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.