AppleScript to Select text in Row and Manipulate it


#1

Hey all,

I know that we can make row to be a link by setting a topic’s style’s attribute link’s value to a valid URL. The question is: is it applicable not to the whole row but to the selected text within this row? And how to select this text using AppleScript?

The purpose is to make multiple links in one row. I can do it manually, but would like to automate it.


#2

I haven’t found a way to work with a selection of text in this manner. I don’t think it is or ever will be accessible. Depending upon your text though, there may be things you can do as you can add links to a portion of a topic’s text.

You work with text based on its position, e.g. ‘characters 5 thru 10 of topic’. If there is some structure to your text, e.g. you want the text beginning two characters after a colon, then you could use something like, ‘(offset of “:” in textString) + 2’.

set o2 to (offset of ":" in t2) + 2		
set character's style's attribute "link"'s value of characters o2 thru -1 of topic of leaf cc of c1 to item cc of urlList

I use it to add links to the text after a colon for every topic in a group, with the links coming from the open tabs in Safari.


#3

Thank you, I’ll give it a shot, but currently I don’t yet understand how to make multiple links in one row using offset. For now, I still see the only way as to make RTF link using textutil with shell script and just paste it in OO. Seems nothing native to OO is able to script it easier…


#4

FWIW, I don’t think that it’s an OO thing. It’s an Apple thing. But, I concede that I’m not sure about that. In the meanwhile, what I proposed isn’t really for freeform link adding. It’s more for adding links to words in a list. So it may not be useful for your purposes.

I just threw this together as my own usage only requires one link per line, which is much simpler to do. It should work though.

Prerequisites:
Safari open with tabs open (in order) for every link you want to add.
OO open with appropriate text. I have tried to upload a sample outline but the system seems broken.

It should look like this though:
New document in OO. Create a topic “Characters”. Add the following four subtopics (or just one to test). The text between the ‘#’ and the ‘:’ will get a link to tab 1 of Safari (e.g. best picture’) the text beginning two characters after the colon will get a link to tab 2 of Safari (e.g. Clint Eastwood).
Then those first two tabs will close and it will go to the next line, and so on.

#Best Picture: Clint Eastwood
#Best Director: Clint Eastwood
#Best Supporting Actor: Gene Hackman
#Best Film Editing: Joel Cox

tell application "Safari" to set urlList to URL of every tab of window 1
count of urlList
tell application "OmniOutliner"
	set d1 to document 1
	set c1 to row named "Characters" of d1

	repeat with cc from 1 to ((count of urlList) / 2)
		tell application "Safari" to set shortList to URL of every tab of window 1
	
		set t1 to topic of (leaf cc of c1)
		set t2 to text of t1
		
--use # and : to designate text for linking
		set a1 to (offset of "#" in t2) + 1
		set a2 to (offset of ":" in t2) - 1
		set n1 to (offset of ":" in t2) + 2
	
		clear attribute "link" of style of characters of topic of leaf cc of c1
		set character's style's attribute "link"'s value of characters a1 thru a2 of topic of leaf cc of c1 to item 1 of shortList
		set character's style's attribute "link"'s value of characters n1 thru -1 of topic of leaf cc of c1 to item 2 of shortList
	
		tell application "Safari" to close tab 1 of window 1
		tell application "Safari" to close tab 1 of window 1
	
	end repeat
end tell