Tex-Edit... I'm not coercing something.

I have a list of phrases (“TitleCaseWords”) that I want tagged title case (Like This For Example).

But the code below won’t do it. Keep getting
“Tex-Edit Plus got an error: Can’t make some data into the expected type.”

But I can’t figure out where I’m tripping.


tell application "Tex-Edit Plus"
	tell window 1
		repeat with Thetext in TitleCaseWords
			set Thetext to Thetext as text
			set numwords to number of words in Thetext
			repeat with x from 1 to numwords
				change case of (word x of Thetext) into word caps
			end repeat
		end repeat
	end tell
end tell

Hello.

I’m just waiting for a flame - editor war, having read the most . My few are “Real men uses Tex-Edit plus!”. :lol:

But the intention is not always equal to the result. Nifty but I haven’t paid it yet so I am a little bit reluctant to using it though I am not finished with the trial period.

You kicked me in the right direction: :slight_smile:

The snippet might not win any beauty contest, but it works. -I figured that a selection might work by looking at some of all the example AppleScripts. And Yes, I’ll pay for it this week!
Here you are: my very first (but def. not last) Tex-Edit plus script:


property TitleCaseWords : {"tell", "me", "about", "it"}
tell application "Tex-Edit Plus"
	tell window 1
		select its contents
		set wc to count of its every word of its selection
		repeat with i from 1 to wc
			if its word i is in TitleCaseWords then
				select its word i
				tell its selection
					change case into word caps
				end tell
			end if
		end repeat
	end tell
end tell

Best Regards

McUsr

Thanks,

But… I want it to search for phrases (“Jack Sprat Would Eat No Fat”), not words.

So searching 1 word at a time won’t do.

Or am I missing something?

I guess you are not :slight_smile:

But it wasn’t easy to guess what you wanted in the first place :).

I’ll see if I can make up something.

What I’ll basically try to do is to save the selection and or insertion point,
Find every phrase that match, and when that phrase is matched; do the caps thing and finally restore any selection and insertion point. I will start at Dougs Tex-Edit+ Scripts which contains a
huge repository of Tex-Edit plus scripts, where some addresses searching and replacing.

Best Regards

McUsr

Here you are.
Beware that this is totally and very shamelessy stolen from the script Delete My Line or Paragraph by Doug Adams.

It should however do what you want, you have even some nifty search options in there, for case sensitive searches etc.


” Meshed together by me McUsr, but really made by Doug Adams http://dougscripts.com/texedit/
global searchStr, buttonPr, iPt, sing, plur, z, selectionMod
property optionChosen : "None"
property modeChosen : ""
property myResult : ""

tell application "Tex-Edit Plus"
	set searchStr to (search string as string) --copy in te's search string
	set iPt to offset of insertion point before selection of window 1 -- get current cursor position
	
	-- get search string w/ M Butcher's options:
	repeat -- cool repeat loop
		display dialog "Current search" & return & "option chosen:" & "  " & optionChosen & return & return & "First enter text to search for, then select Options." default answer (search string as string) buttons {"Options", "Cancel", "OK"} default button "OK"
		set {answerChosen, searchStr} to {button returned, text returned} of result --get dialog button returned and store text to find
		if (searchStr is not "") then exit repeat -- no blanks, Cancel overrides
	end repeat
	
	if answerChosen is "Options" then
		--because the optionChosen variable can be re-set, we need to create a list of the remaining options for the buttons in the dialog box:
		set optionList to {"None", "Whole Words", "Case Sensitive", "Words & Case"}
		set dialogList to {}
		repeat with itemNo from 1 to 4
			if item itemNo of optionList ≠ optionChosen then set dialogList to dialogList & item itemNo of optionList
		end repeat
		display dialog "Choose option:" buttons dialogList
		set optionChosen to button returned of result --store value of new option chosen
	end if
	
	select insertion point before character 1 of window 1 -- send cursor Home
	
	repeat
		my doSearch(optionChosen)
		if myResult contains false then
			exit repeat
		else
			tell its selection
				change case into word caps
			end tell
		end if
	end repeat

	select insertion point after character iPt of window 1 -- send cursor Back to original pos.
end tell

on doSearch(opt)
	tell application "Tex-Edit Plus"
		if opt is "None" then
			search window 1 looking for searchStr with searching from cursor
		else if opt is "Whole Words" then
			search window 1 looking for searchStr with searching from cursor and whole words matching
		else if opt is "Case Sensitive" then
			search window 1 looking for searchStr with searching from cursor and cases matching
		else if opt is "Words & Case" then
			search window 1 looking for searchStr with searching from cursor, cases matching and whole words matching
		end if
		copy result to myResult
	end tell
end doSearch

Hopes this works for you

Best Regards

McUsr

A silent client is a satisfied one :slight_smile:

Best Regards

McUsr

Sorry, couldn’t get back til now. Thank you for all your work.

I tried to mod your script, to eliminate the dialogs and use a preset list of strings, and ended up with this:

global searchStr
property myResult : “”

tell application “Tex-Edit Plus”
set searchStr to {“I like dogs”, “I like cats”, “I like nothing”, “I like people”} as list
select insertion point before character 1 of window 1 – send cursor Home
repeat with srch from 1 to count of items in searchStr
search window 1 looking for (item srch of searchStr as text) with searching from cursor
if myResult contains false then
exit repeat
else
tell its selection
change case into word caps
end tell
end if
end repeat
end tell


[automatically tagged for color by Convert Script to Markup Code]

A question I had: would there be a way to add strings to my list, on the fly (via a dialog)?
I guess I’m thinking code that would actually edit the script’s list of strings.
I’ve never done anything like that, so I apologize if its an obvious question.

Hello yes.

I should be very possible to have a dialog to add strings to your list “on the fly” before, or just add some strings at the moment. I will come back to you with a solution for that in short time. ( Short time means within some 12 hours or so today :slight_smile: )

I will try to create a little “list manager” that lets you add, delete and edit.
It will be created with AppleScript’s “native” gui elements so don’t expect the most fancy thing.
But your list will be persistent!

Best Regards

McUsr

One thing about this script ” which of course may be what you want ” is that the searches are carried out in the order of the search strings in the list and continue from find to find in the text. This means that: 1) the search strings in the list have to be in the same order that they appear in the text, otherwise some of the phrases will be missed, and: 2) only one instance of each phrase (if it’s not skipped) is capitalised.

If you want to capitalise every instance of every search phrase, a simpler approach would be to use capitalised strings in the search list and use them to replace the phrases in the text.

tell application "Tex-Edit Plus"
	set searchStr to {"I Like Dogs", "I Like Cats", "I Like Nothing", "I Like People"}
	repeat with srch from 1 to count of items in searchStr
		set thisPhrase to item srch of searchStr
		replace window 1 looking for thisPhrase replacing with thisPhrase without cases matching
	end repeat
end tell

There’s no need to tag scripts for colour on this BBS. You can put [applescript] . [/applescript] tags at the beginning and end of your code, which will allow others to open it in their own script editors with a single click. (There’s a button for this in the posting dialog.) Also, since not everyone uses the same colours, imposing your own doesn’t help them when they try to read what you’ve done.

Hello again.

I have a cool idea for you and scripts like alike that one wants to configure from time to time, not being burdened with an after a period of time superfluous dialog with some question about wether one wants to change some properties now.

The idea is to use Stefan’s checkModifierKeys when the script is run. If you choose to execute the script by pressing for instance the command key down when you click on the script so it executes, then you will be given the opportunity to change your list of words to capitalize. If you want to use the script with some keyboard short cut utility, then you can make two short cuts one with and one without the optional keystroke. The solution should then still work.

Here are some instructions for installing the utility if you have problems with that.

The utility will be downloaded to your “Downloads” folder.

Open the “Downloads” folder upon completion of the download in a finder window.

Open terminal then enter “cd /usr/local/bin ; open .” then press enter

You should now have gotten a finder window pointing to /usr/local/bin
Drag the checkModifierKeys into that folder from the downloads folder and provide your administrator password if necessary.

Then :slight_smile:
Wait until my solution are ready which should now be within the next four hours.
You can state in the meantime wether you want it like this, if you managed to install it, and what modifier key you
want to use.

If you want to know more about the checkModifierKeys then you can read this thread.

Best Regards

McUsr

Thank you Nigel.

Hello.

New Editon!
Capitalization of words in Tex-Edit Plus documents with changeable wordlist. :cool:

If you press down the control key while activating this script in some way; then the List Maintaining Mode “kicks in”.

You can then:

¢ Show the current items in the list
¢ Add items to the list
¢ Delete Items in the list
¢ Edit items in the list
¢ Capitalize the document
¢ Save the list without Capitalizing the Document

This script uses Stefan’s checkKeyModifier utility. :slight_smile:

This trick works only from within Tex-Edit Plus’s own script menu Not from within the usual Script Menu. This is important as this can make your Script Menu Script run in four different states.

Edit
The truth is that the modifier key combinations cmd, control and cmd + control are detected from the regular “vanlilla” Script Menu. Thanks to Yvan Koenig for making me retest it.

I guess however that this dualistic behavior will work if you use two shortcut keys for a script in your fav. short-cut key manager, one with modifier key and one without. Good luck. :slight_smile:

The list maintaining handler should be useful in its own right for those not already having made/acquired such a handler already.


property l : {"Display List", "Add Item", "Delete Item", "Edit Item", "Capitalize", "Save List and Quit"}
property SearchStr : {}
if SearchStr is {} then
	-- populate it with factory settings ....
	set SearchStr to {"I Like Dogs", "I Like Cats", "I Like Nothing", "I Like People"}
end if
if (do shell script "/usr/local/bin/checkModifierKeys control") is "1" then
	tell me
		activate
		set madeChanges to false
		repeat
			set choice to (choose from list l with prompt "Words to Capitalize Maintainance ." OK button name {"Ok"})
			if choice is false then
				if madeChanges is true then
					try
						display dialog "Sure you want to cancel your editing -- changes will be lost" buttons {"Yes", "Save and Quit"} cancel button {"Yes"} default button {"Save and Quit"}
						set choice to {"Save List and Quit"}
					on error
						error number -128
					end try
				else
					error number -128
				end if
			end if
			
			if choice is {"Capitalize"} then exit repeat
			if choice is {"Save List and Quit"} then return 0
			set SearchStr to my maintainList(choice, SearchStr, a reference to madeChanges)
		end repeat
	end tell
end if

tell application "Tex-Edit Plus"
	activate
	repeat with srch from 1 to count of items in SearchStr
		set thisPhrase to item srch of SearchStr
		replace window 1 looking for thisPhrase replacing with thisPhrase without cases matching
	end repeat
end tell


on maintainList(whatToDo, wordList, didChange)
	local itemToDelete, theResult, dummy
	if whatToDo is {"Delete Item"} then
		repeat
			tell me
				activate
				set itemToDelete to (choose from list wordList with prompt "Choose Item to DELETE" OK button name {"Ok"})
			end tell
			if itemToDelete is not false then
				set ctr to 1
				set theCount to count of wordList
				repeat with anElm in wordList
					if contents of anElm is equal to itemToDelete as text then
						if ctr = 1 then
							set wordList to items 2 thru -1 of wordList
						else if ctr is not theCount then
							set wordList to items 1 thru (ctr - 1) of wordList & items (ctr + 1) thru -1 of wordList
						else
							set wordList to items 1 thru -2 of wordList
						end if
						set contents of didChange to true
						exit repeat
					else
						set ctr to ctr + 1
					end if
				end repeat
			else
				return wordList
			end if
		end repeat
	else if whatToDo is {"Add Item"} then
		repeat
			tell me
				activate
				try
					set theResult to text returned of (display dialog "Enter the item to ADD" default answer "" buttons {"Cancel", "Ok"})
				on error
					exit repeat
				end try
			end tell
			if not theResult is "" then
				set contents of didChange to true
				set end of wordList to theResult
			end if
		end repeat
		return wordList
	else if whatToDo is {"Edit Item"} then
		repeat
			tell me
				activate
				set itemToEdit to (choose from list wordList with prompt "Choose Item to EDIT" OK button name {"Ok"})
			end tell
			if itemToEdit is not false then
				set ctr to 1
				set theCount to count of wordList
				repeat with anElm in wordList
					if contents of anElm is equal to itemToEdit as text then
						exit repeat
					else
						set ctr to ctr + 1
					end if
				end repeat
				try
					tell me
						activate
						set theResult to text returned of (display dialog ("Edit " & itemToEdit as text) & " to something else " default answer itemToEdit as text buttons {"Cancel", "Ok"})
					end tell
				on error
					exit repeat
				end try
				if not theResult is "" then
					set contents of didChange to true
					set item ctr of wordList to theResult
				end if
			else
				return wordList
			end if
		end repeat
	else if whatToDo is {"Display List"} then
		tell me
			activate
			set dummy to (choose from list wordList with prompt "The list is currently holding:" OK button name {"Ok"})
		end tell
		return wordList
	end if
end maintainList

Best Regards

McUsr

Wow.

I d/l’ed Stefan’s utility.

looking at your code…

I don’t see where you pass any new entries to SearchStr (the list of strings)

Is it in this:
set SearchStr to my maintainList(choice, SearchStr, a reference to madeChanges)

?

Thankyou, Thankyou, Thankyou in any case

Hello,
I have removed a fault of mine in the code above, the error was that I didn’t return the list after displaying it, effectively truncating it.

Yes, install it into Tex-Edit’s script menu, then execute the script while holding ctrl down.
Then if everything is alright, you’ll see kind of a dialog.

Best Regards

McUsr

Hello.

I’m here until you have it working. :slight_smile:

I guess my question still stands… can you point out where in your code that you add any new strings to SearchStr?

That would be the goal, right? Or …

There


  set SearchStr to my maintainList(choice, SearchStr, a reference to madeChanges)

More precisely: below here


 else if whatToDo is {"Add Item"} then

Does it work for you?:smiley:

Best Regards

McUsr

No, but more important to me is keeping up with what you’re doing.

I’m able to add a string through the dialogue, but the new string doesn’t seem to be saved in the script itself.
(It does show up when I say Display List, which makes me ask how/if the new entries are being passed to SearchStr.

I have to get back to “work”, but I’m anxious to follow up on this…

You may have to change the path to checkModifierKeys

It should be saved within the script; proof: it shows up the next time you run the script, but it is “invisible to you”
but actually stored in the SearchStr property until you edit the script.

I suggest you try it on a document with said added words and watch what happens. :slight_smile:

I’m having a plan of adding items for writing out and reading in a list of words from/to disk. interested? :cool:

Best Regards

McUsr