What makes the text in a topic a hyperlink?

I’m trying to control the use of hyperlinked text in an OO4 document, hopefully using applescript

.

If I drag a link from a browser into OO4, I will get something that looks like this below (in blue), but when I click on it, the appropriate page in Wikipedia will load in my browser. (Row 1)

~Flat cap - Wikipedia, the free encyclopedia~

If I select this text and then in a different row, Paste and Match Style, then the same text will be pasted but without the linking functionality (and be black)(Row 2).

Other than by dragging, it is possible to create this kind of link?

It is possible to edit an already existing link, by right-clicking and selecting Edit Link. However, this command is unavailable until you select already linked text.

Oddly, if you select the linked text and Copy Style (row 5), and then select some black text and Paste Style (row 6), you will apply the link to the black text. Unfortunately, it applies the link as is… meaning that if that the text won’t match the actual link.

Ultimately, my goal is to be able to be able to create links using applescript, based on open tabs in a browser. I’m able to put a string containing both text and URL into a row but I’m unable to join them into a single element (row 8).
Does anyone understand how this works and how it could be done?

I’ve attached an outline with what I’ve laid out above.

You can type a URL to have OmniOutliner automatically convert it to a hyperlink, but I do think it would be nice if we offered a menu item for creating a link.

OmniOutliner scans for URLs when certain keys are pressed (Return, space, etc.), so that would explain why inserting text via AppleScript isn’t having the intended effect.

Internally, hyperlinks are stored as style attributes (which is true of any Cocoa app that uses the system’s built-in rich text support). But we apparently don’t surface that style attribute via AppleScript. (I just tried properties of style of attribute run 1 of cell… and got a record with just the font attribute). So I’m afraid there might not be a way to do what you want.

1 Like

Thanks for looking into this. I sort of guessed/feared this but as I’m not especially proficient with applescript, I thought it would be better to ask.

Hopefully, these two items wouldn’t be too challenging to implement. Is there anything I’d need to do to get such a request into the pipeline?

Send an email to our Support Humans at omnioutliner@omnigroup.com. They’ll be sure to add your feature request to our database.

1 Like

This script places the title and url of the current tab to the clipboard. I tried pasting it into a row in OO when the text editor is active, i.e. the cursor is blinking versus the selection color of the row, and it works as you want.

If the current row is selected versus being edited, a paste from the clipboard places the title and URL as separate rows.

Thanks. I’ll check it out today.

Looks like there’s still no way to easily add hyperlinks to selected words?

Internally, hyperlinks are stored as style attributes (which is true of any Cocoa app that uses the system’s built-in rich text support). But we apparently don’t surface that style attribute via AppleScript. (I just tried properties of style of attribute run 1 of cell… and got a record with just the font attribute). So I’m afraid there might not be a way to do what you want

Its just that you are using the wrong attribute. The correct object to use to address the hyperlink attribute of the first word of the first row is:

attribute "link" of style of word 1 of rich text of Cell "Topic" of row 1

Using this syntax its possible to set the hyperlink of any character, word or paragraph.

Frederiko

3 Likes

This also works:

value of attribute "link" of style of attribute run 1 of rich text of cell "Topic"

My mistake was in assuming that properties of «style» would return a record that contained the link. It doesn’t, which I consider a bug.

2 Likes

Thanks Frederiko and KyleS.

I sort of mixed together your syntax as follows:

set value of attribute "link" of style of word 1 of rich text of cell id "jFqHUGd_Dg1" of child 3 of document 1 to "http://www.wikipedia.org/"

It then changed the first word of line 3 from rich text to a link to the URL above. So, this is a success and pretty much exactly what I was hoping for. Thanks for your help.

Now I can update my script which should save me a couple of minutes and much aggravation each time I use it.

Thanks again,
Ken

Just in case someone might find this useful, here is the more or less finished script. I have it set to work with Omniweb but it could be adapted to work with any browser that offers up page titles and URLs for its tabs. Apparently I’m not able to upload the OO4 sample I’ve set up. Prerequisites are below.

Any suggestions on improving the script would be appreciated.

Also, if anyone knows how to do a simple search and replace of text… I’d like to be able to add something to the script that would allow me to modify the page titles. For example, to remove the “, the free encyclopedia” at the end of each line in the example doc, or to replace one string with another. Thanks.

This is the code as used, based on the suggestions above:

set value of attribute "link" of style of text of topic cell of child iCtr of selected row of d1 to item iCtr of addressList

Here is the entire script. Prerequisites are an OW6 window with various tabs, and an open OO4 document with a row with “References” as its topic. Whatever children are there will be replaced with one row for each tab in the OW6 window.

tell application "OmniWeb"
	
	--get addresses, titles, and count of tabs and 
	--put addresses and titles into list
	set titleList to title of tabs of browser 1
	set addressList to address of tabs of browser 1
	set tabCount to count of tabs of browser 1
	set comboList to {}
	
	--Combine page titles into single list
	set aCount to 0
	repeat tabCount times
		set aCount to aCount + 1
		set comboList to comboList & (item aCount of titleList)
	end repeat
end tell

tell application "OmniOutliner"
	
	--set up doc and select 'References'
	set d1 to document 1
	unhoist d1
	
	set refRow to (rows whose topic is "References") of d1
	select refRow
	set rowCount to count of children of selected row of d1
	
	--Set topics of children to web data
	set clCount to (count of items of comboList)
	if clCount > rowCount then
		set rowDiff to clCount - rowCount
		select refRow
		repeat rowDiff times
			--properties of selected row of d1
			set refRowID to id of selected row of d1
			make new row at row id refRowID of d1
		end repeat
	end if
	
	set iCtr to 0
	try
		repeat with eachItem in comboList
			set iCtr to iCtr + 1
			set topic of child iCtr of selected row of d1 to eachItem
			set value of attribute "link" of style of text of topic cell of child iCtr of selected row of d1 to item iCtr of addressList
		end repeat
	end try
	
	--Delete extra reference rows
	select refRow
	expandAll selected row of d1
	
	set rowCount to count of children of selected row of d1
	set theDiff to rowCount - clCount
	
	repeat theDiff times
		delete last child of selected row of d1
	end repeat
	activate
	
end tell

Results look something like this, depending upon the doc’s formatting…

Thanks for posting this! It inspired me to devise this version for Safari, with more succinct code for OO:

tell application "Safari"
	tell window 1
		set topics to tabs's name
		set urls to tabs's URL
	end tell
end tell

tell application "OmniOutliner"
	try
		tell document 1
			set refRow to first row whose topic is "References"
			repeat with i from 1 to count topics
				set newChild to make new row at refRow
				tell newChild
					set topic to topics's item i
					set topic cell's text's style's attribute "link"'s value to urls's item i
				end tell
			end repeat
			set refRow's expanded to true
		end tell
	end try
end tell
1 Like

Hi SGIII,

Thanks for posting your version. It’s much better than mine, clearer and definitely more succinct. I learned a lot from studying it. I didn’t know a line could hold so many apostrophes. Often I bash my head against the syntax for a long time before getting something to work and usually, this means pedantic and heavy code. Before long, I’m too exhausted to continue on. That’s why I had the whole refRowID stuff, for example. This really gave me something to work with. I’m glad that I re-posted my ‘final’ script.

I do want the feature of replacing all the child rows rather than appending to them so I added this loop above your loop in the OO section.

	repeat (count children of refRow) times
		delete last child of refRow
	end repeat

Thanks again,
Ken

Hi Ken,

Your idea here has been really useful for organizing web-based research.

I’ve struggled with AppleScript’s verbosity too. I saw someone else use apostrophes and thought, “I didn’t know you could do that!” They’ve really made a difference.

It turns out with OO I didn’t need to use:

set refRow to first row whose topic is "References"

This works just as well:

set refRow to row named "References"

And to delete the children, a loop works well, but this also works:

delete refRow's children

Pretty happy so far with how easy it is to automate some things with OO, such as your references links.

SG

1 Like

Well, you’re definitely better at this than I am. I used the ‘set refRow to (rows whose topic is “References”) of d1’ to grab the References row because I couldn’t make it work any other way. You’ve shown me two improved ways to do it. I’m glad to learn that I don’t need to loop the row deletion either.

I won’t call it easy yet but I’m glad that the capability is there for this kind of automation. I’m the kind of person who wants to automate something if I have to do it just once (forget about needing multiple times). When I’m going to be doing it on an routine basis, it grinds on me if I can’t automate it somehow. Being able to forget about everything except which tabs I have open makes it a pleasure to use OO.

In other posts, I’ve seen people make requests for AS examples and that kind of encouraged me to post my original question as well as the first functional script. Anyway, I’m glad to hear that the idea has some use for someone else. I greatly appreciate your contributions, as well as those of other posters above who pushed things along.

Well I guess it’s time for another follow-up on this thread. Inspired by the what I’ve recently learnt, I’ve been striving to solve one of the other issues I mentioned above, namely to edit the text that makes up the page titles within the ‘references’ section of the OO4 document, e.g. reducing something like this, “Current events - Wikipedia, the free encyclopedia” to this, “Current events - Wikipedia”.

With the above ideas combined with something I found on the Mac OS X Automation site, and from within the Applescript Language Guide (Calling Handlers in a tell Statement, pg. 91), I was able to put the following script together. It starts with DevonAgent but that can easily be adapted to work with OmniWeb or Safari.

-- Notes - References DA v4
-- Takes titles and URLs from browser 
-- Puts line under References with links

tell application "DEVONagent"
	tell window 1
		set topics to tabs's name
		set urls to tabs's URL
	end tell
end tell

tell application "OmniOutliner"
	unhoist document 1
	activate
	try
		tell document 1
			--Target "References"
			set refRow to row named "References"
			select refRow
			
			--Delete any children of References so that the final outcome will always match the open tabs
			delete refRow's children
			
			--New child rows with links from browser tabs
			repeat with i from 1 to count topics
				set newChild to make new row at refRow
				tell newChild
					set topic to topics's item i
					set topic cell's text's style's attribute "link"'s value to urls's item i
				end tell
			end repeat
			
			set refRow's expanded to true
			set cCount to count of children of refRow
			
			--Text fixes. Each loop handles multiple instances of one text change.
			repeat with i from 1 to cCount
				set kRow to child i of refRow
				set kText to text of topic of kRow
				replace_chars(kText, ", the free encyclopedia", "") of me
				set badText to result
				set text of topic of kRow to badText
			end repeat
			repeat with i from 1 to cCount
				set kRow to child i of refRow
				set kText to text of topic of kRow
				replace_chars(kText, " | Defending your rights in the digital world", "") of me
				set badText to result
				set text of topic of kRow to badText
			end repeat
			
		end tell
	end try
end tell
--Search replace function
on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

I’m not sure what the most difficult part was but I sort of had to take a flyer on the whole ‘of me’ part. It makes sense when I think about it but as a solution, it wasn’t obvious to me.

Anyway, maybe this will be useful to someone. Thanks again to those who gave me insights into how applescript works with omnioutliner. If you have any suggestions for improvements, they’d be greatly appreciated.