Need a little help with applescript for selecting text

Hi all, sorry if this is not the correct forum category for this.

I grabbed one of Christian 2004 scripts, replicated it below.

It works great when there is selected text in the record, but if I want to add a line to make sure some text is selected, I get the error “The variable this_selection is not defined.”

What am I doing wrong?

tx for helping!

-- Script Format > Body Normal
-- Created by Christian Grunenberg on Sat May 15 2004.
-- Copyright (c) 2004-2005. All rights reserved.

tell application id "DNtp"
	set this_selection to ""
	try
		set this_selection to the selected text of think window 1
		if this_selection is "" then error "Please select some text."
		tell selected text of think window 1
			set properties to {alignment:justified, font:"Open Sans", size:14, color:{5000, 5000, 5000}, background:{65535, 65535, 65535}}
		end tell
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell

The source of your script would be more useful than the one of my ancient script :wink: In addition, what kind of document do you use?

Hi Christian!

It is the script I’m using :slight_smile:
By document, you mean the record in DT - it is an .rtf

What confuses me:

If I don’t include the lines

set this_selection to “”
if this_selection is “” then error “Please select some text.”

and just tell DNtp to set the properties of the selected text, then all works fine (of course, because of the “try” if there’s no selected text it just fails silently)

But this script doesn’t add new line, therefore how do you do it?

The latest version of the script that ships with DEVONthink is actually this one:

-- Script Format > Body Normal
-- Created by Christian Grunenberg on Sat May 15 2004.
-- Copyright (c) 2004-2014. All rights reserved.

tell application id "DNtp"
	try
		tell selected text of think window 1
			set properties to {alignment:justified, font:"Times", size:12, color:{5000, 5000, 5000}, background:{65535, 65535, 65535}}
		end tell
	end try
end tell

Ah OK - what I wanted was to display a dialog if there’s no selected text
Kind of like you do in your scripts to notify that no records were selected

which why I thought this would work

set this_selection to the selected text of think window 1
		if this_selection is "" then error "Please select some text."

Christian, I believe I understand why I am making a logical error already in the 2 lines I posted - if there’s no selection, then the var this_selection was not defined, correct? Even if I previously set it to “”.

So how do I write AS to tell me no text has been selected in the record?

tx!

Add this after end tell and before end try

on error
display alert "No text is selected!"
1 Like

Jim, that does it!
I owe you a bottle of Porto.

Mike

Thanks! This morning I just need stiff, stiff coffee :slight_smile:

Are you guys getting the river/bomb?

Not here in Michigan :slight_smile:

Ah, my mistake I thought you (still?) were in - was it Idaho?

Official company HQ is Coeur d’Alene, Idaho but we’re global (but still very small :slight_smile: ).

1 Like

You’re the smallest bestest global company

2 Likes

August 31, 2008.

Date when I first bought a license to DEVONthink. Still in love with it.

Wow! Long before my time.

1 Like

not knowing you all. but this is how I picture the company is/runs…

You are from Germany Originally and a decade or so ago you moved to the US to over see the HQ on behalf of Eric Böhnisch-Volkmann and Cristian is the “lead” person in Germany. Although Eric and Cristian still live in Germany, they sort of call out the moves all the way from there yet the HQ is in the US. is it safe to say Eric is the big guy and Cristian is the right hand and you are the left lol and also there is a “junior” person behind the scene, the dude that is in charge of fixing little bugs here and there, what’s his name? dude that fixed the Sorter dock tab

Also picturing you joined the company in early 2000s just like Cristian.

All of this, I might be way off on my assumptions lol

Hmm… nice picture but not really close to reality :wink: I live in the Czech Republic now and founded the company together with Eric, Jim joined as later but he’s not German despite his name :blush:

3 Likes

There is no junior person in the company. Our other developer is Alan, who is very talented and capable, working on parts of both DEVONthink and DEVONthink To Go.

PS: I am US born and raised, though my heritage is (mostly) German. My great grandparents came to the States from Northern Germany.

3 Likes