Copy incremental tags from text and paste to document

Perhaps what I’m trying to do is too convoluted, but I hope someone can help/offer advice.

I’m working with a series of texts, all of which contain inline tags (there contain the symbols </> with any number from 0-9 and up before the “/”) and these need to appear in the final document (docx) or the document will be corrupt and won’t open.

I already have a process in place that will copy the entire original sentence (on a sentence by sentence basis) to a plain text file, I just can’t think of how I can then lift only the tags and then using a keyboard short cut, paste these in the order in which they appear in the original sentence into the reworked sentence.

Here is an example:

Hier <0/>ist<1/> ein Satz auf <2/>deutsch<3/>.

The finished sentence needs to contain these tags, in the same order, but not necessarily in the same exact places. For instance I could have:

This is <0/><1/> a sentence <2/><3/> in German.

I can’t seem, however, to even get AppleScript to grab the contents of the file that contains the given sentence. Here is what I’ve tried so far. It runs fine, but when I try to paste the contents into a text file, nothing is returned:

try	
	tell application "Finder" to set theFile to item "~/Library/Preferences/OmegaT/script/source.txt"
	open for access theFile
	set fileContents to (read theFile)
	set the clipboard to fileContents
	close access theFile
end try

I’ve tried all sorts of permutations of the above, but so far nothing. Not a good start considering I then need to be able to pull the tags out of what I capture.

Can anyone help further?

Thanks,

Bowjest

I doubt, that it runs at all.

item [POSIX path of something] in Finder throws an error

By the way, either you can read a file directly without open for access

set theFile to ((path to library folder from user domain as text) & "Preferences:OmegaT:script:source.txt")
set fileContents to (read (theFile as alias))
set the clipboard to fileContents

or, using open for access use the returned file reference number (fileRef) rather than the path

set theFile to ((path to library folder from user domain as text) & "Preferences:OmegaT:script:source.txt")
try
	set fileRef to open for access file theFile
	set fileContents to (read fileRef)
	set the clipboard to fileContents
	close access fileRef
on error
	close access file theFile
end try

Thanks for the swift reply, Stefan.

As far as the filtering of the </> elements goes, can AppleScript look for such a thing and then process each instance in turn in a sentence or am I wanting too much from AS?

I’m not familiar enough with the syntax to get it to do anything like this, I’m afraid.

Regards,

Bowjest

I think I’m going about this the wrong way.

I had an idea last night that would enable me to print “</>” on the line at the cursor point and then move the cursor back just before the “/” so that the required number can be added:

tell application "System Events"
	tell application "OmegaT" to activate
	set tag_ to ("<t/>") as string
	tell process "OmegaT" to keystroke tag_
	key code 123 -- Left
	key code 123 -- Left
end tell

But then I thought, what if entry box could be added so that a range of numbers can be inputted and then, using some sort of while loop perhaps, have the above done, BUT:

  1. adding each number from the range (i.e. <0/>

  2. Pausing the script so that the cursor can be moved to a new point and then continuing adding the tag and number by hitting “return”

Here is what I have so far, but I can’t get “echo” to work in spite of looking at quite a few examples via Google:

set displayString to "Please input a number"
set defaultAnswer to 0
repeat
	set response to display dialog displayString default answer defaultAnswer
	try
		set theNumber to (text returned of response) as number
		exit repeat
	on error errstr
		set displayString to errstr & return & "Please try again."
		set defaultAnswer to text returned of response
	end try
end repeat

tell application "System Events"
	tell application "OmegaT" to activate
	do shell script "echo" & theNumber
end tell

The error I get is:

“error “System Events got an error: sh: echo0: command not found” number 127”

Of course, what I ultimately need is for it to “echo” < & theNumber & / & > with a while statement (something like “while range of numbers is less than highest integer, repeat the above?”)

I’ll need to do a lot more reading on while loops, but just need to get the “echo” to work first.

Thanks,

Bowjest

I’ve gotten a bit further with this. I can now get it to display the number selected at the start of the script within a tag:

set displayString to "Please input a number"
set defaultAnswer to 0
repeat
	set response to display dialog displayString default answer defaultAnswer
	try
		set theNumber to (text returned of response) as number
		exit repeat
	on error errstr
		set displayString to errstr & return & "Please try again."
		set defaultAnswer to text returned of response
	end try
end repeat

tell application "System Events"
	tell application "OmegaT" to activate
	set theTag to "<" & theNumber & "/" & ">"
	keystroke theTag
	
end tell

Can anyone advise how I can set up a while loop so I can set a range of numbers at the start of the script and have the first number inserted in the tag, then the script pauses until I hit return, at which point it adds the next number in the range in a tag, pauses, etc. until the entire range has been covered?

Thanks,

Bowjest

You need a space after that echo command to make it work. The error is that the zero is mashed up towards the echo command, and that makes the shell look for the command echo0, which it can’t find, and throws an error.

This is a basic loop construct, the numbers being sequential, and we’ll use the last number in your range as the exit condition, that is, the loop will be executed including your last number. You are then free to use the index variable inside your code.


repeat with i from 0 to endrange

” do your stuff with every i here
end repeat

Thanks, McUsr.

I’m going to need to think more about how I want to implement this. I think maybe what I want is too difficult to achieve.

Just to confirm (should you have any advice), I’d like to either be able to read all the instances of tags from a named file (these tags could start with any single digit number and have as little as one or as many as 7-8 in a line) and insert these sequentially based on where I place my mouse cursor (pausing between each insertion until I hit return, and then adding the next tag)

Or

I choose a starting number and ending number and these are sequentially inserted in a tag format (<1/>, <4/>, <0/>, etc.), pausing between insertion until I hit return

Or

I just stick with my very simple script that allows me to insert an empty tag (</>), but then move the cursor back two spaces so I can just insert the required number.

I actually “jazzed up” the last option by putting in a 1 second delay so that after I trigger the script with Quicksilver, I can insert the number and then the cursor moves back to the end of the tag so I can then ctrl±> to the next insertion point.

That’s about the best I’ve been able to achieve on my own. :slight_smile:

Regards,

Bowjest

Hello!

Would it help to have a dialogbox giving you the option of which tag to paste into your document at the cursor positon.

You would install the script on a key in Quicksilver, the choose from list box appears in front of your document, you choose from 0 to 9, hit enter, and the tag is inserted into the front document?

Thanks, McUsr, that’s a really good suggestion.

I’ve managed to put together a multi-clickable list of tags, but it fails when it comes to inserting them in the text. Here’s what I have:

set tagList to {"<0/>", "<1/>", "<2/>", "<3/>", "<4/>", "<5/>", "<6/>", "<7/>", "<8/>", "<9/>"}

set theChoices to choose from list tagList with multiple selections allowed

tell application "System Events"
	tell application "OmegaT" to activate
	-- tell process "OmegaT" to keystroke theChoices
	do shell script "echo " & theChoices
end tell

Error: “System Events got an error: sh: -c: line 0: syntax error near unexpected token newline' sh: -c: line 0: echo <1/>'” number 2

I’m assuming it’s viewing the “/” in the tag as a new line character. Do I need to escape this somehow?

Thanks for your help.

Bowjest

I’ll see if I can whip up something. :slight_smile:

But I thought you were inserting the tags, when in word or text-edit or some other word processing app?

Could you please answer that?

Thanks. I’m sorry to put you to so much trouble.

The tags are being inserted in a translation project manager called OmegaT. It needs to have the inline tags included so that things like words in bold and italics are displayed correctly. Without them, the text will be corrupt and the resulting docx file won’t open when exported.

At present there is no automated way to include these tags. You either have to copy and paste them by hand (tedious if there is a great deal of formatting in a doc) or there is an option to remove them completely, but then only the paragraphs and font sizes are preserved; bold, italics, etc. is then removed.

This is why my original idea was to put the cursor in the first point of insertion, have a script that would identify and pull out the tags and then use a Quicksilver shortcut to insert the first tag, pause, move cursor, hit enter, insert next tag, pause, move cursor, etc., but I think that is probably too complicated.

Currently, I’m using Quicksilver and the script I outlined before that just inserts the tag “shell” at the cursor point and then I insert the correct number manually. Not bad at all, but I’m sure it could be better - enter your idea about the item list.

Many thanks,

Bowjest

Hello!

There hasn’t been any trouble at all! :slight_smile:

But you do edit the text like you would do in a normal text editor. So I think we have a solution.

My scenario:

You sit in a text editing window, and enter the tags manually where it is plausible to add them.

My solution:

You place the cursor where you want to add the tag.

You hit your shortcut key. and gets the related tag pasted into the text editing window at that position.

Are my assumptions correct?

Thanks, I appreciate it. :smiley:

Yes, that’s right. OmegaT presents text on a line-by-line basis in an editor window. The source text appears just above the cursor where you then type in your translation (your target text).

When there are tags, you either manually enter them (copy/paste or type out by hand).

If a text is heavily formatted for whatever reason, a line can have as many as 8-9 tags (perhaps more), so you can see why an automated solution would be welcome. :slight_smile:

Would a screenshot help? I’d be happy to send you one.

Many thanks,

Bowjest

I got it! :slight_smile:

I’ll be back in about 6 hours, as I am in the middle of something, so I test it before I hand it over, and makes sure it works properly!

When the solution works, I would like you to post the solution on some omega mailinglist, with a short description, so others with the same problem can find it, if that isn’t to much to ask for.

Thanks, McUsr. That’s really super kind of you.

And of course, I’d be very happy to post your scripts to the OmegaT list for others to use. I’m sure others would find them extremely useful. I can probably get the site moderator to add them to the list of useful downloads on the OmT site itself!

I look forward to hearing how your tests go.

All the best,

Bowjest

Try this! :slight_smile:

Come back if it doesn’t work as expected!


property scriptTitle : "TagInserter"
set tagl to {{leadingnumber(0) & "<0/>"}, {leadingnumber(1) & "<1/>"}, {leadingnumber(2) & "<2/>"}, {leadingnumber(3) & "<3/>"}, {leadingnumber(4) & "<4/>"}, {leadingnumber(5) & "<5/>"}, {leadingnumber(6) & "<6/>"}, {leadingnumber(7) & "<7/>"}, {leadingnumber(8) & "<8/>"}, {leadingnumber(9) & "<9/>"}}


set res to choosesomething("Choose tag to insert", tagl, item 1 of tagl)

if res = false then return
set the clipboard to (characters 4 thru -1 of (res as text)) as text as record

tell application "System Events" to tell application process "OmegaT"
	set frontmost to true
	keystroke "v" using {command down, option down, shift down}
end tell

on leadingnumber(n)
	return text 1 thru 3 of (n & "   " as text)
end leadingnumber

on choosesomething(aPrompt, listOfSomething, defaultSomething)
	local theChoice
	tell application "SystemUIServer"
		activate
		set theChoice to choose from list listOfSomething with prompt aPrompt with title my scriptTitle default items defaultSomething without multiple selections allowed and empty selection allowed
	end tell
	return theChoice
end choosesomething

Ok, this is really weird.

Using your script, OmegaT doesn’t seem to see them as tags, but rather as characters, so instead of seeing a finished sentence of:

This is a sentence.

I get:

This is a sentence<0/>.

Don’t know why that is. That’s really very strange.

Hello!

I think that is because the text isn’t pure ascii, but contains formatting information in addition to the tags.

I think I’ll make another part of the script, which is run the first time, where you “collect” tags from 0 to 9 from the omegaT document, which you then can paste back by the program later.

Because if you get the tag from the omegaT document, and then paste back, then it will work, won’t it?

Could you please try that?

My idea is that you have a document with every tag, you copy them to clipboard by cmd C, run the script, and the script will store them sequentially, with a message about which tag being stored.
when every tag is stored, the script will enter production and lets you paste the stored tags.

What do you think about that?

When you “Register” the tags from 0-9 you will have to run the script from the script menu, as quicksilver doesn’t save the state of the script after it is run.

The second you go in and edit the script afterwards,you will loose everything

I recommend, having a document with the 10 tags, copy tag 0 to the clipboard, run the script, copy tag 1, run the script, until you have all 10 tags.

Yes, that’s the normal way of handling them.

Do you mean copy the entire sentence or each tag in the sentence individually?

If you mean the first option, that would be great. There is already a file that copies and stores the source and target sentence for each segment a translator works with so that a certain degree of text manipulation can be done.

If you mean copying the tags individually, I think that would be far slower than entering them by hand.

Regards,

Bowjest

Hello I mean copying the tags with formatting information once as the script starts up.

Then you have to run the script from the script menu and copy the tags <0/> thru <9/> with the formatting up front, run the script, until you have “recorded” the tags, then the script enters production mode

After that, you run the script from quicksilver and paste it in, and hopefully that will work.

I have used this with bold text in TextEdit, and it retains the bold formatting, the problem with the script above was that I coerced the clipboard to the datatype text, I have now changed that to a record, which then should as I understand it, adapt to the formatting already in the text in this special case. So the quick fix, is to try the script above, edited with my new understanding.

If that doesn’t work, try the script below. :slight_smile:


property scriptTitle : "TagInserter"

property hasRecorded : false
property tags : {missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value}


if not hasRecorded then
	set foundmissing to false
	set i to 0
	repeat with atag in tags
		if contents of atag is missing value then
			set foundmissing to true
			try
				display dialog "Tag nr " & i & " will be inserted into the script if you hit OK" with title scriptTitle default button 2 cancel button 1
			on error
				error number -128
			end try
			set contents of atag to (the clipboard as record)
			exit repeat
		else
			set i to i + 1
		end if
	end repeat
	
	
	if foundmissing = true and i = 9 then
		set my hasRecorded to true
		return 0
	else
		return 0
	end if
	
end if

if hasRecorded is true then
	
	
	set tagl to {{leadingnumber(0) & "<0/>"}, {leadingnumber(1) & "<1/>"}, {leadingnumber(2) & "<2/>"}, {leadingnumber(3) & "<3/>"}, {leadingnumber(4) & "<4/>"}, {leadingnumber(5) & "<5/>"}, {leadingnumber(6) & "<6/>"}, {leadingnumber(7) & "<7/>"}, {leadingnumber(8) & "<8/>"}, {leadingnumber(9) & "<9/>"}}
	
	
	set res to choosesomething("Choose tag to insert", tagl, item 1 of tagl)
	
	if res = false then return
	set the clipboard to item (((characters 1 thru 2 of (res as text)) as text as integer) + 1) of tags
	
	tell application "System Events" to tell application process "OmegaT"
		set its frontmost to true
		--		activate
		keystroke "v" using {command down}
	end tell
	
end if
on leadingnumber(n)
	return text 1 thru 3 of (n & "   " as text)
end leadingnumber

on choosesomething(aPrompt, listOfSomething, defaultSomething)
	local theChoice
	tell application "SystemUIServer"
		activate
		set theChoice to choose from list listOfSomething with prompt aPrompt with title my scriptTitle default items defaultSomething without multiple selections allowed and empty selection allowed
	end tell
	return theChoice
end choosesomething